Reference:
http://www.ibm.com/developerworks/cn/java/j-lo-servlet30/
Asynchronous processing attributes can be applied to both servlet and filter components, because the asynchronous processing of the working mode and the normal mode of operation are fundamentally different, so by default, servlets and filters do not turn on the asynchronous processing feature, if you want to use the attribute, You must enable it in the following way:
- For scenarios where servlets and filters are configured using traditional deployment profiles (Web. xml), servlet 3.0 adds <async-supported> sub-labels for <servlet> and <filter> tags The default value of this label is false, to enable asynchronous processing support, set it to true. In the case of a Servlet, the configuration is as follows:
<servlet> <Servlet-name>Demoservlet</Servlet-name> <Servlet-class>Footmark.servlet.Demo servlet</Servlet-class> <async-supported>True</async-supported> </servlet>
- For a servlet or filter configuration using the @WebServlet and @WebFilter provided by Servlet 3.0, both annotations provide the Asyncsupported property, which evaluates to False by default, to enable asynchronous processing support, only You need to set this property to True. Take @WebFilter as an example, configured as follows:
Truepublicclassimplements filter{...}
A simple example of a Servlet that simulates asynchronous processing is as follows:
@WebServlet (urlpatterns = "/demo", asyncsupported =true) Public classAsyncdemoservletextendsHttpServlet {@Override Public voiddoget (httpservletrequest req, HttpServletResponse resp)throwsIOException, servletexception {resp.setcontenttype ("Text/html;charset=utf-8"); PrintWriter out=Resp.getwriter (); Out.println ("Time to enter servlet:" +NewDate () + "."); Out.flush (); //executes a business call in a child thread, and is responsible for outputting the response, the main thread exitsAsynccontext CTX =Req.startasync (); NewThread (NewExecutor (CTX)). Start (); Out.println ("Time to end servlet:" +NewDate () + "."); Out.flush (); }} Public classExecutorImplementsRunnable {PrivateAsynccontext CTX =NULL; PublicExecutor (Asynccontext ctx) { This. CTX =CTX; } Public voidrun () {Try { //wait 10 seconds to simulate the execution of the business methodThread.Sleep (10000); PrintWriter out=ctx.getresponse (). Getwriter (); Out.println ("Time of Business Processing completed:" +NewDate () + "."); Out.flush (); Ctx.complete (); } Catch(Exception e) {e.printstacktrace (); } }}
In addition, Servlet 3.0 provides a listener for asynchronous processing, expressed using the Asynclistener interface. It can monitor the following four types of events:
- When the asynchronous thread starts, call the Asynclistener Onstartasync (AsyncEvent event) method;
- When an asynchronous thread is faulted, call the Asynclistener OnError (AsyncEvent event) method;
- Asynchronous thread execution timed out, call Asynclistener's OnTimeOut (AsyncEvent event) method;
- When the asynchronous execution is complete, call the Asynclistener OnComplete (AsyncEvent event) method;
To register a asynclistener, simply pass the prepared Asynclistener object to the AddListener () method of the Asynccontext object, as follows:
Asynccontext CTX = req.startasync (); Ctx.addlistener (new Asynclistener () { public voidthrows ioexception { // do some cleanup work or other } ... });
New servlet Features: Asynchronous processing