在Spring Boot中(Spring MVC)下請求默認都是同步的,一個請求過去到結束都是由一個線程負責的,很多時候為了能夠提高吞吐量,需要將一些操作異步化,除了一些耗時的業務邏輯可以異步化,我們的查詢接口也是可以做到異步執行。
一個請求到服務上,是用的web容器的線程接收的,比如線程http-nio-8084-exec-1
我們可以使用WebAsyncTask將這個請求分發給一個新的線程去執行,http-nio-8084-exec-1可以去接收其他請求的處理。一旦WebAsyncTask返回數據有了,就會被再次調用并且處理,以異步產生的方式,向請求端返回值。
示例代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@RequestMapping (value= "/login" , method = RequestMethod.GET) public WebAsyncTask<ModelAndView> longTimeTask(){ System.out.println( "/login被調用 thread id is : " + Thread.currentThread().getName()); Callable<ModelAndView> callable = new Callable<ModelAndView>() { public ModelAndView call() throws Exception { Thread.sleep( 1000 ); /模擬長時間任務 ModelAndView mav = new ModelAndView( "login/index" ); System.out.println( "執行成功 thread id is : " + Thread.currentThread().getName()); return mav; } }; return new WebAsyncTask<ModelAndView>(callable); } |
可以看到輸出結果如下:
/login被調用 thread id is : http-nio-8084-exec-1
執行成功 thread id is : MvcAsync1
在執行業務邏輯之前的線程和具體處理業務邏輯的線程不是同一個,達到了我們的目的。
然后我做了一個并發測試,發現不停的在創建MvcAsync1這個線程,我就在想,難道沒有用線程池?
通過閱讀源碼才發現果真如此,WebAsyncManager是Spring MVC管理async processing的中心類。
默認是使用SimpleAsyncTaskExecutor,這個會為每次請求創建一個新的線程
1
|
private AsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor( this .getClass().getSimpleName()); |
如果說任務指定了executor,就用任務指定的,沒有就用默認的SimpleAsyncTaskExecutor
1
2
3
4
|
AsyncTaskExecutor executor = webAsyncTask.getExecutor(); if (executor != null ) { this .taskExecutor = executor; } |
我們可以配置async 的線程池,不需要為每個任務單獨指定
通過configurer.setTaskExecutor(threadPoolTaskExecutor());來指定
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
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; @Configuration public 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; } } |
配置完之后就可以看到輸出的線程名稱是YJH開頭的了,而且也不會一直創建新的線程
可以看到輸出結果如下:
1
2
|
/login被調用 thread id is : http-nio- 8084 -exec- 1 執行成功 thread id is : YJH1 |
總結
以上所述是小編給大家介紹的Spring Boot 使用WebAsyncTask異步返回結果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://www.jianshu.com/p/21ff7a329a3e