Servlet3.0: Introduction Asynccontext

Source: Internet
Author: User

Each request comes to the Web container, and the Web container assigns it a thread that is specifically responsible for the request, and the thread is not freed back to the container until the response is complete. threads consume system resources, and if some requests take a long time to process (such as long-time operations, waiting for a resource), it takes a long time to consume threads, and if such requests are large, many threads are occupied for a longer period of time, which can be a performance burden for the system. It even creates an application's performance bottleneck.  

basically some long processing requests, usually the client does not care about the request to have an immediate response, if possible, let such a request to release the container allocated to the request of the thread, so that the container can have the opportunity to allocate thread resources to other requests, can alleviate the system burden. The request that originally freed the container's allocated thread will be deferred until the processing is completed (for example, the long-time operation is complete and the required resources have been obtained) and the response to the client.

in servlet 3.0, the startasync ( ) method is available on ServletRequest :

Java code
    1. Asynccontext Startasync () throws java.lang.IllegalStateException;
    2. Asynccontext Startasync (ServletRequest servletrequest,
    3. Servletresponse servletresponse)
    4. Throws Java.lang.IllegalStateException

Both of these methods will return the real-crop pieces of the Asynccontext interface, which directly uses the original request and response object to build the Asynccontext, which allows you to pass in the request that you set up and respond to the parcel object. after calling the Startasync () method to get the Asynccontext object, the response is deferred and the thread allocated by the container is freed.  

You can asynccontext by getresponse () method Get request, reply object, The response to the client will be deferred to the call Asynccontext complete () method or

to be able to call ServletRequest's Startasync () using Asynccontext, your Servlet must be able to support non-synchronous processing, and if you use @webservlet to label it, you can set its asyncsupported is true. For example:

Java code
    1. @WebServlet (urlpatterns = "/", asyncsupported = true)
    2. Public class Asyncservlet extends HttpServlet {
    3. ...

If you set the servlet using Web. XML, you can set the <async-supported> tag to true :

XML code
  1. ...
  2. <servlet>
  3. <servlet-name>asyncservlet</servlet-name>
  4. <servlet-class>cc.openhome.asyncservlet</servlet-class>
  5. <async-supported>true</async-supported>
  6. </servlet>
  7. ...

If the servlet will be unsynchronized, and if there is a filter on its front-end, the filter must also indicate that it supports non-synchronous processing, and if @WebFilter is used, it can also be set to asyncsupported true. For example :

Java code
    1. @WebFilter (urlpatterns = "/", asyncsupported = true)
    2. Public class Asyncfilter implements filter{
    3. ...

If you set a filter using Web. XML, you can set the <async-supported> tag to true:

XML code
  1. ...
  2. <filter>
  3. < filter-name>asyncfilter</ filter-name>
  4. < filter-class>cc.openhome.asyncfilter</ filter-class>
  5. <async-supported>true</async-supported>
  6. </ filter >
  7. ...

Below is an example of a non-synchronous processing that, for incoming requests, the servlet obtains its asynccontext and releases the thread allocated by the container, the response is deferred, and a Runnable object is created for these deferred response requests. , and it is queued to a thread pool, the number of threads in the thread pool is fixed, so that those requests that must be processed for a long time are completed in these limited numbers of threads, without having to consume the thread allocated by the container each time the request is made.  

Java code
  1. Package cc.openhome;
  2. import*;
  3. import java.util.concurrent.*;
  4. import javax.servlet.*;
  5. import javax.servlet.annotation.*;
  6. import javax.servlet.http.*;
  7. @WebServlet (name="Asyncservlet", urlpatterns={"/"},
  8. asyncsupported = true)
  9. public class Asyncservlet extends HttpServlet {
  10. //thread pool
  11. private Executorservice Executorservice = Executors.newfixedthreadpool (10);
  12. protected void ProcessRequest (HttpServletRequest request,
  13. HttpServletResponse response)
  14. throws Servletexception, IOException {
  15. Response.setcontenttype ("text/html;   Charset=utf8 ");
  16. Asynccontext CTX = Request.startasync ();
  17. Executorservice.submit (new Asyncrequest (CTX));
  18. }
  19. @Override
  20. protected void doget (HttpServletRequest request,
  21. HttpServletResponse response)
  22. throws Servletexception, IOException {
  23. ProcessRequest (request, response);
  24. }
  25. @Override
  26. protected void DoPost (HttpServletRequest request,
  27. HttpServletResponse response)
  28. throws Servletexception, IOException {
  29. ProcessRequest (request, response);
  30. }
  31. @Override
  32. public Void Destroy () {
  33. Executorservice.shutdown ();
  34. }
  35. }

Asyncrequest is a class of practice runnable that simulates a long processing period:

Java code
    1. Package cc.openhome;
    2. import;
    3. import Javax.servlet.AsyncContext;
    4. public class Asyncrequest implements Runnable {
    5. private Asynccontext CTX;
    6. Public asyncrequest (Asynccontext ctx) {
    7. this.ctx = CTX;
    8. }
    9. @Override
    10. public Void Run () {
    11. try {
    12. //Analog long-time processing
    13. Thread.Sleep (10000);
    14. PrintWriter out = Ctx.getresponse (). Getwriter ();
    15. Out.println ("Waiting for ...   XD ");
    16. //This side only really sends out the response
    17. Ctx.complete ();
    18. } catch (Exception e) {
    19. E.printstacktrace ();
    20. }
    21. }
    22. }

Servlet3.0: Introduction Asynccontext

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.