Monday Nov 30, 2009
Monday Nov 30, 2009
One of the significant enhancements made in JSR 315: Java Servlet 3.0, is support for asynchronous processing. With this support, a servlet no longer has to wait for a response from a resource such as a database before its thread can continue processing, that is, the thread is not blocked. Previous to Servlet 3.0, asynchronous support in the Java EE web container was provided in a proprietary way — either through APIs built on top of the Servlet 2.5 API or through non-Servlet APIs or implementations.
In modern web applications there are times when you need to asynchronously handle some part of the request, so that the web container can continue serving other requests in a seamless fashion. One example is a chat room application. In this type of application you want to enable long-lived client connections with a servlet. You don't want a server thread to be blocked for a long period of time serving a request from a single client. You want the servlet to process a request from the client and then free up the server thread as quickly as possible for other work. In other words, you want the request to be processed and a response generated asynchronously.
With the upcoming release of the Servlet 3.0 specification, standard asynchronous support has been added to the Servlet API. This Tech Tip introduces the asynchronous support that has been added to the Servlet API in Java Servlet 3.0 technology. It also includes a sample application that demonstrates those features.
Asynchronous Support Features
The primary features of the asynchronous support in Servlet 3.0 are as follows.
Annotation Attributes
Servlet 3.0 introduces the use of annotations as an alternative to deployment descriptors
for servlet-related configuration in a web application. Two of these annotations are @WebServlet, which defines a servlet,
and @WebFilter, which defines a servlet filter. Both of these annotations include an attribute, asyncSupported.
Setting the asyncSupported attribute to true, declares that the servlet or servlet filter supports asynchronous
processing. For example, the following annotation defines a servlet in a web application and declares that the servlet supports
asynchronous processing:
The asyncSupported attribute is needed to differentiate code written for synchronous processing from that written for use in
an asynchronous context. In fact, for an application to use the asynchronous feature, the entire request processing chain must have
the have this attribute set either through the annotation or in its deployment descriptor. An IllegalStateException will be thrown
if an application tries to start an asynchronous operation and there is a servlet or servlet filter in the request processing chain
that does not support asynchronous processing.
Servlet Request Methods
The support for asynchronous processing also includes new ServletRequest methods, such as
startAsync(servletRequest, servletResponse), startAsync(), and getAsyncContext(). After you set the
asyncSupported attribute in the request processing chain to support asynchronous processing, you call either the
startAsync(servletRequest, servletResponse) or startAsync() method to make an asynchronous request.
Here, for example, is a call that makes an asynchronous request:
The difference between the two startAsync method signatures is that the startAsync() method implicitly uses
the original request and response, while the startAsync(servletRequest, servletResponse) method uses the request and response
objects passed in the method call. The request and response passed in the call can be wrapped by filters or other servlets earlier in the
request processing chain. Notice that the startAsync method returns an AsyncContext object —
see AsyncContext Class for more details. The AsyncContext object is initialized appropriately
with the request and response objects depending on the method used. You must exercise caution when wrapping the response and calling the
no arguments signature of the startAsync() method. You could lose the data if any of data is written to the wrapped
response and not flushed to the underlying response stream.
There are a few more methods in the ServletRequest class that are part of the support for asynchronous processing.
These include isAsyncSupported() and isAsyncStarted() You can use these convenience methods in an application
to determine if asynchronous operations are supported or started on a request.
AsyncContext Class
The AsyncContext class is a new class in Servlet 3.0 that provides the execution context for an asynchronous operation.
The class provides a variety of methods that you can use to get access to the underlying request and response objects.
For example, you can use the AsyncContext.dispatch(), AsyncContext.dispatch(path),
or AsyncContext.dispatch(servletContext, path) method to dispatch the request to the container. Using any of the
dispatch methods, enables the processing to return to the container after the asynchronous operation that was started on the ServletRequest is
completed — for instance, after waiting for a call to a web service to return. These methods dispatch the request back to the container
so you can use frameworks such as JavaServer Pages (JSP) to generate the response.
The dispatch() method, that is, the method with no arguments, forwards the request back to the original URL.
Also, if a call to AsyncContext.dispatch or RequestDispatcher.forward occurs after an asynchronous context
has been initialized, the dispatch() method forwards the request to the path for the AsyncContext
or RequestDispatcher-related requests.
The dispatch(path) method forwards the request to the path relative to the context of the request.
The dispatch(ServletContext, path) method forwards the request to the path relative to the specified context.
For example, here is a call that forwards the request back to the container so that the JSP framework can generate the response.
Another AsyncContext method, complete(), completes the asynchronous operation that was started on the request that
was used to initialize this AsyncContext object. It closes the response that was used to initialize this AsyncContext
object. You can call this method, for example, when the response generated by the asynchronous operation is complete.
The container can also implicitly call this method if the application dispatches the request back to the container using the forward
method with no subsequent startAsync call on the request.
Asynchronous Listener Class
Servlet 3.0 also adds a new listener class for asynchronous processing, AsyncListener. You can use this class in an
application to get notified when asynchronous processing is completed, if a timeout has occurred, an error has occurred, or a subsequent call
to startAsync has occurred. The following code snippet creates a new AsyncListener object and uses it to
get notified when an asynchronous processing operation is completed .
Notice that the parameter passed to the onComplete method is an AsyncEvent object. The
AsyncEvent class is another new class provided as part of the Servlet 3.0 support for asynchronous processing.
It represents an event that gets fired when the asynchronous operation initiated on a ServletRequest
has completed, timed out, or produced an error.
A Simple Application That Uses the Asynchronous Support in Servlet 3.0
Now that you've seen the key features in the Servlet 3.0 support for asynchronous processing, let's look at a more complete example. The following shows the code for a simple web application. In the application, a servlet makes an asynchronous request to a web service, waits for the call to return, and then dispatches the request back to the container to render the result using JSP. Note that the complete code for servlet is not shown.
Sample Application
One of the sample applications provided with the Java EE 6 SDK is a chat application that uses the asynchronous support in Servlet 3.0.
To run the application, do the following:
<javaee_home> is where you installed the Java EE 6 SDK.
<javaee_home>/domains/domain1/autodeploy directory.
Now, you're ready to chat.
You can view the source code for the application in the <javaee_home>/samples/javaee6/web/servlet/async-request-war/src
directory.
Further Reading
For more information, see the following resources:
About the Author
Rajiv Mordani is a senior staff engineer at Sun Microsystems. He was part of the original J2EE platform team and has worked on a wide variety of server-side XML and web technologies, including Jakarta Tomcat and most recently Java Servlet 3.0. Rajiv is currently the specification lead for Servlet 3.0. Read his blog.
Did HttpServletRequest.setAsyncTimeout make into the final spec? I ran the async_request_war sample app in GlassFish v3 and got the following:
[#|2009-12-14T23:06:20.237-0500|WARNING|glassfishv3.0|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=29;_ThreadName=http-thread-pool-8080-(3);|StandardWrapperValve[web.servlet.async_request_war.AjaxCometServlet]: PWC1406: Servlet.service() for servlet web.servlet.async_request_war.AjaxCometServlet threw exception
java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.setAsyncTimeout(J)V
any ideas?
Posted by Eric Ma on December 14, 2009 at 08:11 PM PST #
@Eric the APIs have been updated, please download the latest sample as well and that should work. Sorry about the confusion.
Posted by Rajiv Mordani on December 18, 2009 at 10:45 AM PST #
There is no web.xml in the war?
Posted by Eric Ma on December 18, 2009 at 03:13 PM PST #
thank you for your article...that is a nice one...i often visit this blog and i enjoy reading all posts...
Posted by dizi izle on January 23, 2010 at 02:48 AM PST #