怎么在SpringBoot中使用WebAsyncTask异步返回结果

本篇文章给大家分享的是有关怎么在Spring Boot中使用WebAsyncTask异步返回结果,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创新互联专注为客户提供全方位的互联网综合服务,包含不限于网站设计、成都网站制作、兴业网络推广、小程序设计、兴业网络营销、兴业企业策划、兴业品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供兴业建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com

示例代码如下:

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

可以看到输出结果如下:

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

以上就是怎么在Spring Boot中使用WebAsyncTask异步返回结果,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。

名称栏目:怎么在SpringBoot中使用WebAsyncTask异步返回结果
文章网址:https://www.cdcxhl.com/article34/pisjse.html

成都网站建设公司_创新互联,为您提供做网站网站建设品牌网站建设ChatGPT品牌网站设计域名注册

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站托管运营