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