Wednesday, December 15, 2010

Servlet 3.0 Asynchronous Processing

Came across a great post on Servlet 3.0 asynchronous processing at http://www.javaworld.com/javaworld/jw-02-2009/jw-02-servlet3.html?page=1

Following are some notes based on the post.

Important thing to note is that it is not a fire and forget mechanism, as in put a request on a queue and return the response right away. The web client  has to wait however long the request processing take place.

It is a way to free up request handling server threads from long running code. Instead the work in offloaded to be handled by manually created thread(s) by application code.

Client response stream can be open for extended period of time, and server can write to response to implement "push" model.

 

 

 

 



Following are some of the important methods from ServletRequest API from Servlet 3.0 .

AsyncContext getAsyncContext()
Gets the AsyncContext that was created or reinitialized by the most recent invocation of startAsync() or startAsync(ServletRequest,ServletResponse) on this request.

boolean     isAsyncStarted() Checks if this request has been put into asynchronous mode.
boolean  isAsyncSupported() Checks if this request supports asynchronous operation.
AsyncContext     startAsync() Puts this request into asynchronous mode, and initializes its AsyncContext with the original (unwrapped) ServletRequest and ServletResponse objects.
AsyncContext     startAsync(ServletRequest servletRequest, ServletResponse servletResponse)
Puts this request into asynchronous mode, and initializes its AsyncContext with the given request and response objects.











No comments: