Asynchronous processing in server side and client side is a new feature that is offered by the JAX-RS 2.0. Asynchronous processing in RESTful services may seems a little bit odd or unconventional at the first sight.
- Pluralsight - Building Asynchronous Restful Services With Jersey
- Pluralsight Building Asynchronous Restful Services With Jersey City
- Pluralsight Building Asynchronous Restful Services With Jersey Shore
- Pluralsight Building Asynchronous Restful Services With Jerseys
Asynchronous processing in server side and client side is a new feature that is offered by the JAX-RS 2.0. Asynchronous processing in RESTful services may seems a little bit odd or unconventional at the first sight. By considering what really happens under the hood, all the unknown aspects of RESTful asynchronous processing will be revealed. It will become clearer that async processing model is one of the essential factors in design and implementation of scalable enterprise services.
In synchronous request/response processing model, client connection is accepted and processed in a single I/O thread by the server. Normally a pool of such I/O threads is available at server side. So when a request is received, the server dedicates one of these threads to accept and process the request. The thread blocks until the processing is finished and returned. When the processing is done and response is sent back to the client, the thread can be released and sent back to the pool. At this point the container that serves requests, assume that the request processing is finished and all the associated resources including the connection can be freed.
This model works perfectly if the request processing doesn’t take so much time. As mentioned before, there is a pool of threads that accepts and processes incoming requests. When there is a huge number of requests and processing is heavy and time consuming, at some point we would expect to reach a point that all the threads are busy processing and the pool is empty. At this point there is no more threads available to accept any connection request.
This is the time that asynchronous processing model comes into action. The idea behind asynchronous processing model is to separate connection accepting and request processing operations. Technically speaking it means to allocate two different threads, one to accept the client connection and the other to handle heavy and time consuming operations. In this model, the container dispatched a thread to accept client connection (acceptor), hand over the request to processing (worker) thread and releases the acceptor one. The result is sent back to the client by the worker thread. In this mechanism client’s connection remains open. May not impact on performance so much, such processing model impacts on server’s THROUGHPUT and SCALABILITY a lot.
JAX-RS 2 async API perfectly support the aforementioned model. Consider the following piece of code:
In “AsynchronousResource” class a normal REST resource “asyncresource” is defined. This resource has one method “asyncRestMethod” and is annotated with “@GET” annotation. The “asyncRestMethod” method injects an “AsyncResponse” instance using @Suspended annotation. Both “AsyncResponse” and @Suspended are contained in the JAX-RS async API. By using these parameters, the JAX-RS runtime are told to handle any incoming request asynchronously. One thing that is worth to mention is the VOID as the return type of the “asyncRestMethod” method. The VOID key word is indicating that it is totally normal for an acceptor thread that only accepts the client’s request and not to define any return type. The only responsibility of the acceptor thread is to dispatch the processing request to worker threads. When the processing is finished, “asyncResponse.resume(result)” will return the response to the client.
In the previous code, asynchronous feature of JAVA EE EJB was used. @Asynchronous annotation tells the enterprise bean container to handle this method asynchronously and acts as a worker thread.
Pluralsight - Building Asynchronous Restful Services With Jersey
![With With](/uploads/1/2/6/3/126332034/231570613.jpg)
So the scenario is as follows: JAX-RS runtime dispatches a thread to accept a connection. The connection is accepted and is handed over to a worker thread for background processing. JAX-RS runtime releases the acceptor thread and returns it to the pool. It can then use it to accept more connections.
By default there is no timeout defined for this so called heavy processing. This is where the “TimeoutHandler” event handler comes into action. Consider the following code: