问题:
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 配置它们。