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