Java Magazine, Jan/Feb 2016
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2016 37 web apps Support for Asynchronous Processing JAX RS 20 includes a brand new API for asynchronous processing that includes server side as well as clientside counterparts Being asynchronous inherently implies request processing on a different thread than the thread that initiated the request From a client perspective asynchronous processing prevents blocking the request thread because no time is spent waiting for a response from the server Similarly asynchronous processing on the server side involves the suspension of the original request thread and the initiation of request processing on a different thread thereby freeing up the original server side thread to accept other incoming requests The end result of asynchronous processing if it is leveraged correctly is scalability responsiveness and greater throughput Server side asynchronous processing The server side asynchronous programming model is mainly powered by the following API abstractions the @ javax ws rs container Suspended annotation and the javax ws rs container AsyncResponse interface An instance of AsyncResponse can be transparently injected as a method parameter of a JAX RS resource class by annotating it with @ Suspended This instance serves as a callback object to interact with the caller client and perform operations such as response delivery postrequest processing completion request cancellation error propagation and so forth @ GET @ Path id public void search @ Suspended AsyncResponse asyncResp @ PathParam id String id launching search in a new thread new Thread public void run execute search op and resume UserInfo user obtain via search asyncResp resume user start To propagate responses and exceptions use the overloaded versions of the resume method on the AsyncResponse interface to return responses or exceptions back to the client To handle request processing timeouts configure a timeout after which an HTTP 503 error response is returned to the client This can be achieved by configuring a timeout threshold By default a timeout triggers an HTTP 503 error response However this can be overridden by registering a javax ws rs container TimeoutHandler implementation javax ws rs container CompletionCallback represents a callback interface whose implementation can be registered in order to execute business logic after the completion of the request It is possible to terminate request processing by using overloaded versions of the cancel method This results in an HTTP 503 error response being sent to the client Client side asynchronous processing The JAX RS API allows asynchronous request invocations using the Invocation and the AsyncInvoker interfaces The Invocation interface handles asynchronous submission via overloaded versions of the submit method while the AsyncInvoker interface supports asynchronous invocation with dedicated methods get post put and so on for standard HTTP actions GET PUT POST DELETE HEAD TRACE and OPTIONS Once registered an implementation of the Invocation Callback will automatically be executed once the asynchronous request is processed It provides the ability to account for scenarios that cause exceptions or that execute successfully In the event that a Future object is obtained and no callback has been registered manually poll it in order
You must have JavaScript enabled to view digital editions.