最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot 使用WebAsyncTask異步返回結(jié)果

 更新時間:2018年02月02日 15:39:37   作者:尹吉歡  
這篇文章主要介紹了Spring Boot 使用WebAsyncTask異步返回結(jié)果的相關(guān)資料,需要的朋友可以參考下

在Spring Boot中(Spring MVC)下請求默認都是同步的,一個請求過去到結(jié)束都是由一個線程負責(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)生的方式,向請求端返回值。

示例代碼如下:

@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ù)邏輯的線程不是同一個,達到了我們的目的。

然后我做了一個并發(fā)測試,發(fā)現(xiàn)不停的在創(chuàng)建MvcAsync1這個線程,我就在想,難道沒有用線程池?

通過閱讀源碼才發(fā)現(xiàn)果真如此,WebAsyncManager是Spring MVC管理async processing的中心類。

默認是使用SimpleAsyncTaskExecutor,這個會為每次請求創(chuàng)建一個新的線程

private AsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(this.getClass().getSimpleName());

如果說任務(wù)指定了executor,就用任務(wù)指定的,沒有就用默認的SimpleAsyncTaskExecutor

AsyncTaskExecutor executor = webAsyncTask.getExecutor();
if (executor != null) {
  this.taskExecutor = executor;
}

我們可以配置async 的線程池,不需要為每個任務(wù)單獨指定

通過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開頭的了,而且也不會一直創(chuàng)建新的線程

可以看到輸出結(jié)果如下:

/login被調(diào)用 thread id is : http-nio-8084-exec-1
執(zhí)行成功 thread id is : YJH1

總結(jié)

以上所述是小編給大家介紹的Spring Boot 使用WebAsyncTask異步返回結(jié)果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論

紫云| 葵青区| 遂川县| 台东县| 垦利县| 阳谷县| 禄劝| 博野县| 潍坊市| 沁阳市| 大悟县| 阳江市| 雷州市| 左贡县| 大英县| 广汉市| 舞钢市| 仙居县| 定边县| 岳西县| 乐东| 新巴尔虎右旗| 西乌珠穆沁旗| 石首市| 乌拉特前旗| 林西县| 霍山县| 双牌县| 崇阳县| 定日县| 黄平县| 渝中区| 潼关县| 广宗县| 日照市| 西峡县| 四会市| 城固县| 林芝县| 阿荣旗| 华亭县|