本篇文章給大家分享的是有關(guān)怎么在Spring Boot中使用WebAsyncTask異步返回結(jié)果,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、興業(yè)網(wǎng)絡(luò)推廣、小程序設(shè)計(jì)、興業(yè)網(wǎng)絡(luò)營銷、興業(yè)企業(yè)策劃、興業(yè)品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供興業(yè)建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址:www.chinadenli.net
示例代碼如下:
@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); /模擬長時(shí)間任務(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ù)邏輯的線程不是同一個(gè),達(dá)到了我們的目的。
然后我做了一個(gè)并發(fā)測(cè)試,發(fā)現(xiàn)不停的在創(chuàng)建MvcAsync1這個(gè)線程,我就在想,難道沒有用線程池?
通過閱讀源碼才發(fā)現(xiàn)果真如此,WebAsyncManager是Spring MVC管理async processing的中心類。
默認(rèn)是使用SimpleAsyncTaskExecutor,這個(gè)會(huì)為每次請(qǐng)求創(chuàng)建一個(gè)新的線程
private AsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(this.getClass().getSimpleName());
如果說任務(wù)指定了executor,就用任務(wù)指定的,沒有就用默認(rèn)的SimpleAsyncTaskExecutor
AsyncTaskExecutor executor = webAsyncTask.getExecutor();
if (executor != null) {
this.taskExecutor = executor;
}我們可以配置async 的線程池,不需要為每個(gè)任務(wù)單獨(dú)指定
通過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;
@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開頭的了,而且也不會(huì)一直創(chuàng)建新的線程
可以看到輸出結(jié)果如下:
/login被調(diào)用 thread id is : http-nio-8084-exec-1 執(zhí)行成功 thread id is : YJH1
以上就是怎么在Spring Boot中使用WebAsyncTask異步返回結(jié)果,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
名稱欄目:怎么在SpringBoot中使用WebAsyncTask異步返回結(jié)果
文章網(wǎng)址:http://www.chinadenli.net/article34/pisjse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)、ChatGPT、品牌網(wǎng)站設(shè)計(jì)、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)