Spring Boot 使用WebAsyncTask非同步返回結果,springwebasynctask
在Spring Boot中(Spring MVC)下請求預設都是同步的,一個請求過去到結束都是由一個線程負責的,很多時候為了能夠提高輸送量,需要將一些操作非同步化,除了一些耗時的商務邏輯可以非同步化,我們的查詢介面也是可以做到非同步執行。
一個請求到服務上,是用的web容器的線程接收的,比如線程http-nio-8084-exec-1
我們可以使用WebAsyncTask將這個請求分發給一個新的線程去執行,http-nio-8084-exec-1可以去接收其他請求的處理。一旦WebAsyncTask返回資料有了,就會被再次調用並且處理,以非同步產生的方式,向請求端傳回值。
範例程式碼如下:
@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,這個會為每次請求建立一個新的線程
private AsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(this.getClass().getSimpleName());
如果說任務指定了executor,就用任務指定的,沒有就用預設的SimpleAsyncTaskExecutor
AsyncTaskExecutor executor = webAsyncTask.getExecutor();if (executor != null) { this.taskExecutor = executor;}
我們可以配置async 的線程池,不需要為每個任務單獨指定
通過configurer.setTaskExecutor(threadPoolTaskExecutor());來指定
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; }}
配置完之後就可以看到輸出的線程名稱是YJH開頭的了,而且也不會一直建立新的線程
可以看到輸出結果如下:
/login被調用 thread id is : http-nio-8084-exec-1執行成功 thread id is : YJH1
總結
以上所述是小編給大家介紹的Spring Boot 使用WebAsyncTask非同步返回結果,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對幫客之家網站的支援!