Controller层使用了Callable需要配置WebMvcConfigurer支持异步并发

问题:

2018-09-19 00:43:58.434 WARN 10104 --- [ XNIO-2 task-28] o.s.w.c.request.async.WebAsyncManager : !!! An Executor is required to handle java.util.concurrent.Callable return values. Please, configure a TaskExecutor in the MVC config under "async support". The SimpleAsyncTaskExecutor currently in use is not suitable under load.

原因:

Controller层使用了Callable返回结果,上下文没有配置异步线程池引起的。

解决方案:

@Configuration
@EnableAsync
public class AsyncConfig  implements AsyncConfigurer {

    @Bean
    protected WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
                configurer.setTaskExecutor(getTaskExecutor());
            }
        };
    }

    @Bean
    protected ConcurrentTaskExecutor getTaskExecutor() {
        return new ConcurrentTaskExecutor(Executors.newFixedThreadPool(5));
    }
}

Spring 配置在这方面有点令人困惑,因为它需要单独配置 MVC 异步支持,即使用返回 Callable 的控制器处理程序方法,以及使用 @Async 注释的任何 Spring bean 方法。要正确配置它,您可以应用类似下面的配置,记住 AsyncTaskExecutor 配置可能需要修改。

附带说明一下,您可能会想简单地使用 @Async 注释您的控制器处理程序方法。这只会产生预期的效果 - 释放 Web 服务器线程 - 触发和忘记操作(此观察基于 Spring Boot 2.1.2,他们可能会在未来解决这个问题)。如果您想利用 Servlet 3.0 异步处理的强大功能,您真的必须使用 Callables 并使用 WebMvcConfigurer 配置它们。

原文 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值