Spring Boot uses WebAsyncTask to return results asynchronously. springwebasynctask
Requests in Spring Boot (Spring MVC) are synchronized by default. A request is handled by a thread from the past to the end. In many cases, to improve the throughput, some operations need to be asynchronous. In addition to time-consuming business logic, our query interface can also be asynchronously executed.
A request to the service is received by the web Container thread, such as the thread http-nio-8084-exec-1
We can use WebAsyncTask to distribute this request to a new thread for execution, and the http-nio-8084-exec-1 can receive the processing of other requests. Once WebAsyncTask returns data, it will be called and processed again, and returned to the request end in an asynchronous way.
The sample code is as follows:
@ RequestMapping (value = "/login", method = RequestMethod. GET) public WebAsyncTask <ModelAndView> longTimeTask () {System. out. println ("/login called thread id is:" + Thread. currentThread (). getName (); Callable <ModelAndView> callable = new Callable <ModelAndView> () {public ModelAndView call () throws Exception {Thread. sleep (1000);/simulate a long-time task ModelAndView mav = new ModelAndView ("login/index"); System. out. println ("successful thread id is:" + Thread. currentThread (). getName (); return mav ;}; return new WebAsyncTask <ModelAndView> (callable );}
The output result is as follows:
/Login is called thread id is: http-nio-8084-exec-1
Thread id is MvcAsync1
The thread before executing the business logic is not the same as the thread for processing the business logic, achieving our goal.
Then I did a concurrency test and found that the MvcAsync1 thread was constantly being created. I was wondering, isn't the thread pool used?
Only by reading the source code can we find that WebAsyncManager is the central class for Spring MVC to manage async processing.
SimpleAsyncTaskExecutor is used by default, which creates a new thread for each request.
private AsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(this.getClass().getSimpleName());
If the executor is specified for a task, the default SimpleAsyncTaskExecutor is used if the task is not specified.
AsyncTaskExecutor executor = webAsyncTask.getExecutor();if (executor != null) { this.taskExecutor = executor;}
We can configure the async thread pool, without the need to specify
Use er. setTaskExecutor (threadPoolTaskExecutor (); to specify
import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import org.springframework.web.context.request.async.TimeoutCallableProcessingInterceptor;import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;@Configurationpublic class WebMvcConfig extends WebMvcConfigurationSupport { @Override public void configureAsyncSupport(final AsyncSupportConfigurer configurer) { configurer.setDefaultTimeout(60 * 1000L); configurer.registerCallableInterceptors(timeoutInterceptor()); configurer.setTaskExecutor(threadPoolTaskExecutor()); } @Bean public TimeoutCallableProcessingInterceptor timeoutInterceptor() { return new TimeoutCallableProcessingInterceptor(); } @Bean public ThreadPoolTaskExecutor threadPoolTaskExecutor() { ThreadPoolTaskExecutor t = new ThreadPoolTaskExecutor(); t.setCorePoolSize(10); t.setMaxPoolSize(50); t.setThreadNamePrefix("YJH"); return t; }}
After the configuration, we can see that the output thread name starts with YJH, and no new thread will be created all the time.
The output result is as follows:
/Login is called thread id is: http-nio-8084-exec-1 execution successful thread id is: YJH1
Summary
The above section describes how Spring Boot uses WebAsyncTask to return results asynchronously. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!