In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "sample Analysis of Java Servlet Asynchronous request opening", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and learn the article "sample Analysis of Java Servlet Asynchronous request opening".
1. Background
In the process of studying the implementation of long polling, there are asynchronous requests using Servlet3.
2. Servlet synchronization request
Take the Tomcat server as an example:
Http request arrives at Tomcat
Tomcat fetches threads from the thread pool to process requests to Tomcat
Parse the request Http to HttpServletRequest
Distribute to the specific Servlet to handle the corresponding business
Return processed data through HttpServletResponse
Normally, the request model is the same as the above model, all requests are handed over to the thread pool of the Tomcat server, and the whole action is processed before it is released back to the thread pool.
There is a problem if the later business processing time is longer. Then the thread processing the request will be occupied all the time. As more and more requests are made, more and more threads will be occupied. Until all threads in the thread pool are exhausted. The subsequent entry has been blocked waiting for the thread to process.
When the user does not care about the submitted return, the business processing thread pool can be defined. After the front-end request is submitted, the Tomcat thread will submit the processing to the business thread pool and return immediately. This is the case with asynchronous tasks (@ Async) in Spring.
3. Servlet asynchronous request
Also take the Tomcat service as an example:
Parse the request Http to HttpServletRequest
Distribute to the specific Servlet processing, submit the business to the custom business thread pool, and the Tomcat thread is released immediately.
When the business thread finishes executing the task, it will transfer the result to the Tomcat thread pool.
Return processed data through HttpServletResponse
Introduce the overall process of asynchronous Servlet3:
Using asynchronous Servelt,Tomcat threads only handles request parsing actions, and all time-consuming business operations are handed over to the business thread pool, so Tomcat threads can handle more requests than synchronous requests. Although the business is handed over to the business process, the front end is still waiting for the result to return (synchronously waiting for it to return).
Asynchronous processing, the front end will synchronously wait for the result to return. Many people will think that asynchronous requests will return faster. In fact, it is not due to the existence of thread switching asynchronously. All return times will be slower than synchronized.
Although it does not reduce the corresponding time, it still has other obvious advantages:
Can handle a higher number of concurrent connections and improve the overall throughput of the system
Request parsing is completely separated from business processing, and the responsibility is single.
Custom business thread pool, which can be easily monitored, degraded, etc.
Different thread pools can be customized according to different businesses. They are isolated from each other and do not have to influence each other.
4. How to use Asynchronous Servlet
Using asynchronous Servlet requires only three steps:
HttpServletRequest#startAsync () gets the AsyncContext asynchronous context
Use a custom business thread pool to handle business
AsyncContext#getResponse () returns the processing result to the front end, and then calls AsyncContext#complete ()
5. Implementation examples in Spring
The code is shown below:
Enable asynchronous Servlet
Simulate business execution
Return the result to the front end
As mentioned earlier, the front end has been waiting synchronously. Let's verify it by running the code. The result is as follows:
Code address: github.com/mxsm/spring...
Attached: asynchronous object listener
Listen when an asynchronous object completes, times out, errors, or starts
/ / get the asynchronous context object AsyncContext ac=req.startAsync () Ac.addListener (new AsyncListener () {@ Override public void onComplete (AsyncEvent asyncEvent) throws IOException {} @ Override public void onTimeout (AsyncEvent asyncEvent) throws IOException {} @ Override public void onError (AsyncEvent asyncEvent) throws IOException {} @ Override Public void onStartAsync (AsyncEvent asyncEvent) throws IOException {}}) The above is all the content of the article "sample Analysis opened by Java Servlet Asynchronous request". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.