溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在Spring Boot中使用WebAsyncTask異步返回結(jié)果

發(fā)布時(shí)間:2021-03-29 16:52:04 來源:億速云 閱讀:381 作者:Leah 欄目:編程語言

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

示例代碼如下:

@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ā)測試,發(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)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI