服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Spring Boot 使用WebAsyncTask异步返回结果

Spring Boot 使用WebAsyncTask异步返回结果

2021-03-30 14:52尹吉欢 Java教程

这篇文章主要介绍了Spring Boot 使用WebAsyncTask异步返回结果的相关资料,需要的朋友可以参考下

Spring Boot中(Spring MVC)下请求默认都是同步的,一个请求过去到结束都是由一个线程负责的,很多时候为了能够提高吞吐量,需要将一些操作异步化,除了一些耗时的业务逻辑可以异步化,我们的查询接口也是可以做到异步执行。

一个请求到服务上,是用的web容器的线程接收的,比如线程http-nio-8084-exec-1

我们可以使用WebAsyncTask将这个请求分发给一个新的线程去执行,http-nio-8084-exec-1可以去接收其他请求的处理。一旦WebAsyncTask返回数据有了,就会被再次调用并且处理,以异步产生的方式,向请求端返回值。

示例代码如下:

?
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被调用 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,这个会为每次请求创建一个新的线程

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

如果说任务指定了executor,就用任务指定的,没有就用默认的SimpleAsyncTaskExecutor

?
1
2
3
4
AsyncTaskExecutor executor = webAsyncTask.getExecutor();
if (executor != null) {
  this.taskExecutor = executor;
}

我们可以配置async 的线程池,不需要为每个任务单独指定

通过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开头的了,而且也不会一直创建新的线程

可以看到输出结果如下:

?
1
2
/login被调用 thread id is : http-nio-8084-exec-1
执行成功 thread id is : YJH1

总结

以上所述是小编给大家介绍的Spring Boot 使用WebAsyncTask异步返回结果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://www.jianshu.com/p/21ff7a329a3e

延伸 · 阅读

精彩推荐