GithubHelp home page GithubHelp logo

justm0rph3u5 / async-request-serial-processor- Goto Github PK

View Code? Open in Web Editor NEW

This project forked from samsung/async-request-serial-processor-

0.0 1.0 0.0 191 KB

This is a Java library to serially process asynchronous requests by some request criteria, yet in parallel processing requests which are independent (or not having same request criteria). This is generally required in scenarios where requests are arriving asynchronously and can be processed in parallel with a condition that request with certain cri

License: Apache License 2.0

Java 100.00%

async-request-serial-processor-'s Introduction

async-request-serial-processor

A library to serially process asyncronous request.

This is a Java library to serially process asynchronous requests by some request criteria, yet parallely processing requests which are independent (or not having same request criteria). This is generally required in scenarios where requests are arriving asynchronously and can be processed parrallely with a condition that request with certain criteria (e.g. request userId) to be processed in serially. The measured performance shows good improvement over single threaded processing on a multicore machines.

Use case

Imagine asynchronous requests are coming in a queue and consumer has to process all the requests. Also there is a restriction that request which belongs to a single key (say a user id) must be processed in order they arrived in a queue. In order to fulfill this serial processing requirement, one can choose to have a single thread consumer, which will guarnatee serial processing order. However this will not be efficient since requests which belong to differnent key, can still be processed in parallel. This library is exactly meant for doing it.

Internally it maintains pool of worker thread, and assigngs a worker thred to incoming request, Each working thread will have it own internal queue, where incoming request from a given key will be appened. In order word it creates a stickyness between worker thread and key. The stickyness is only up to limited time, if there is no new request since last request is received, the worker thread is released back to thread pool. The pool will then reassign the worker thread to new incoming request.

Diagram

diagram

Getting Started (From source)

  1. Clone this project
  2. Build jar, mvn clean package
  3. Include async-request-serializer-1.0-SNAPSHOT.jar in your project

API documentation

The primary classes in order to use this library is as follows, currently the library is using Spring framework (we are planning to drop that dependency though):

  1. Create the AsyncRequestSerializer instance, this the entry point for this library.
//replace T with the class representing a return class type.
private AsyncRequestSerializer<T> asyncRequestSerializer =                     
    new AsyncRequestSerializer(new AsyncRequestSerializerConfig.Builder.build());
  1. Extend Work class which has single function public T call(), this will be your actual logic that you want to run.
  2. Submit your job by calling asyncRequestSerializer.submit(String key, Work<T> work), this is non-blocking call and return Future
  3. From returned future object you can get result of your processing.

Sample code

/**
* A sample work class, this is where 
* the actual business logic will go
*/
class MyWork implements Work<MyResponse> {
  public MyResponse call() {
      //your business logic
      ...
      MyResponse myResponse;
      return myResponse;
  }
}

private AsyncRequestSerializer<MyResponse> asyncRequestSerializer = new AsyncRequestSerializer<>(
    new AsyncRequestSerializerConfig
        .Builder()
        .setWorkerThreadPoolSize(8)
        .build());


//create your work class
Work<MyResponse> work = new MyWork();

//Submit the work to async request serailzier, key should a string on which we want to serialize the processing. 
Future<MyResponse> myResponseFuture = asyncRequestSerializer.submit(key, work);  

MyResponse myResponse = myResponseFuture.get();  

async-request-serial-processor-'s People

Contributors

ashwanidausodia avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.