spring boot中设置异步请求默认使用的线程池

参考:
Spring Boot Asynchronous Request Processing Task Executor Configuration

摘录:

Finally found Spring MVC - Configuring Asynchronous Request Processing

To use other implementation of TaskExecutor we can extend our configuration class from WebMvcConfigurerAdapter or we can use it as bean. For example in a boot application:


@SpringBootApplication
public class AsyncConfigExample{
    @Bean
    WebMvcConfigurer configurer(){
        return new WebMvcConfigurerAdapter(){
            @Override
            public void configureAsyncSupport (AsyncSupportConfigurer configurer) {
                ThreadPoolTaskExecutor t = new ThreadPoolTaskExecutor();
                t.setCorePoolSize(10);
                t.setMaxPoolSize(100);
                t.setQueueCapacity(50);
                t.setAllowCoreThreadTimeOut(true);
                t.setKeepAliveSeconds(120);
                t.initialize();
                configurer.setTaskExecutor(t);
            }
        };
    }
    public static void main (String[] args) {
        SpringApplication.run(AsyncConfigExample.class, args);
    }
}

Spring MVC - Configuring Asynchronous Request Processing

的内容如下:

Enabling native asynchronous support
Spring Async mechanism is built on native servlet asynchronous specification. That means 'asyncSupported' must be set to true on the Servlet level i.e. DispatcherServlet.

If you are using JavaConfig along with @EnableWebMvc or Spring boot with @SpringBootApplication then this flag is set to true by default. We have not been enabling it explicitly in our previous examples.

If you are using some other way of configuration and things don't work for you please check out spring ref here.



Using a custom TaskExecutor
In cases where Spring takes care of thread management (instead of we create threads ourselves) such as where handler method returns Callable<?> or StreamingResponseBody, Spring uses SimpleAsyncTaskExecutor by default.

To use other implementation of TaskExecutor we can extend our configuration class from WebMvcConfigurerAdapter or we can use it as bean. For example in a boot application:

@SpringBootApplication
public class AsyncConfigExample{
    @Bean
    WebMvcConfigurer configurer(){
        return new WebMvcConfigurerAdapter(){
            @Override
            public void configureAsyncSupport (AsyncSupportConfigurer configurer) {
                ThreadPoolTaskExecutor t = new ThreadPoolTaskExecutor();
                t.setCorePoolSize(10);
                t.setMaxPoolSize(100);
                t.setQueueCapacity(50);
                t.setAllowCoreThreadTimeOut(true);
                t.setKeepAliveSeconds(120);
                t.initialize();
                configurer.setTaskExecutor(t);
            }
        };
    }
    public static void main (String[] args) {
        SpringApplication.run(AsyncConfigExample.class, args);
    }
}


Wrapping Callable in WebAsyncTask
In cases where we return Callable implementation from the handler methods, We can wrap it in a WebAsyncTask which allows to set timeout and a custom task executor per callable basis:


@Controller
public class MyWebController3 {

    @RequestMapping("/test3")
    public
    @ResponseBody
    WebAsyncTask<String> handleRequest (HttpServletRequest r) {
        System.out.println("asyncSupported: " + r.isAsyncSupported());
        System.out.println(Thread.currentThread().getName());

        Callable<String> callable = () -> {
            System.out.println(Thread.currentThread().getName());
            return "WebAsyncTask test";
        };

        ConcurrentTaskExecutor t = new ConcurrentTaskExecutor(
                  Executors.newFixedThreadPool(1));
        return new WebAsyncTask<>(10000L, t, callable);
    }
}
Please check out complete examples in the project browser below.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值