13th _ asynchronous Processing

Source: Internet
Author: User
13.1 Overview

The computer memory is limited. Servlet/JSP Container designers are aware of this, so they provide some configuration settings to ensure that the container can run normally on the host machine. For example, in Tomcat 7, the maximum number of threads for processing incoming requests is 200. If it is a multi-processor server, you can safely increase the number of threads, but we recommend that you use this default value as much as possible.

Servlet or filter occupies the request processing thread until it completes the task. If it takes a long time to complete the task, the number of concurrent users will exceed the number of threads, and the container will experience the risk of exceeding the thread. In this case, Tomcat will stack the excess requests in an internal server socket (the Processing Methods of other containers may be different ). If more requests continue, they will be rejected until resources can process the requests.

Asynchronous processing can help you save container threads. This feature is suitable for long-running operations. Its job is to wait for the task to complete and release the request processing thread so that another request can use this thread. Note that asynchronous support is only applicable to long-running tasks, and you want to let users know the task execution result. If you only have tasks that run for a long time but do not need to know the processing result, you only need to provide a runnable to the executor and return immediately. For example, if you need to generate a report and send the report via email after the guard is ready, asynchronous processing is not suitable. On the contrary, if you need to generate a report and display it to users after the report is complete, you can use Asynchronous processing.

 

13.2 write asynchronous Servlet and filter

The annotation types of webservlet and webfilter can contain the new asyncsupport attribute. To write Servlet and filter that supports asynchronous processing, the asyncsupported attribute must be set to true:

@ Webservlet (asyncsupported = true ...)

@ Webfilter (asyncsupported = true ...)

Another configuration in the configuration file

<Servlet>

<Servlet-Name> asyncservlet </servlet-Name>

<Servlet-class> servlet. myasyncservlet </servlet-class>

</Servlet>

13th _ asynchronous Processing

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: info-contact@alibabacloud.com 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.