我正在使用Spring Boot应用程序。我有一个返回Controller的rest控制器。
@GetMapping("/fb-roles")
@Timed
public Callable<List<FbRole>> getAllFbRoles() {
log.debug("REST request to get all FbRoles");
return (() -> { return fbRoleRepository.findAll(); });
}
ThreadPoolTaskExecutor的配置如下:
@Configuration
@EnableAsync
@EnableScheduling
public class AsyncConfiguration implements AsyncConfigurer {
private final Logger log = LoggerFactory.getLogger(AsyncConfiguration.class);
private final JHipsterProperties jHipsterProperties;
public AsyncConfiguration(JHipsterProperties jHipsterProperties) {
this.jHipsterProperties = jHipsterProperties;
}
@Override
@Bean(name = "taskExecutor")
public Executor getAsyncExecutor() {
log.debug("Creating Async Task Executor");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(jHipsterProperties.getAsync().getCorePoolSize());
executor.setMaxPoolSize(jHipsterProperties.getAsync().getMaxPoolSize());
executor.setQueueCapacity(jHipsterProperties.getAsync().getQueueCapacity());
executor.setThreadNamePrefix("fb-quiz-Executor-");
return new ExceptionHandlingAsyncTaskExecutor(executor);
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new SimpleAsyncUncaughtExceptionHandler();
}
}
2018-09-19 00:43:58.434警告10104 --- [XNIO-2 task-28] o.s.w.c.request.async.WebAsyncManager:!!!需要执行程序来处理java.util.concurrent.Callable返回值。请在“异步支持”下的MVC配置中配置TaskExecutor。当前使用的SimpleAsyncTaskExecutor在负载下不适合。
但是在访问api服务器时会产生以下警告
Spring配置在这方面有点令人困惑,因为它需要单独的配置来支持MVC异步,即使用返回Callable
的Controller处理程序方法以及任何以@Async
注释的Spring bean方法。要正确配置两者,您可以应用以下配置,请注意AsyncTaskExecutor
配置可能需要修改:
@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));
}
}
附带说明,您可能会想用@Async
简单地注释Controller处理程序方法。这只会在火灾和遗忘操作上产生预期的效果-释放Web服务器线程-(此观察基于Spring Boot 2.1.2,将来可能会解决此问题)。如果要利用Servlet 3.0异步处理的功能,则确实必须使用Callables
并将其配置为WebMvcConfigurer
。
给出警告和您的Callable
方法。
似乎Spring无法识别您刚刚设置的Executor
bean在您的配置类中。
您可能需要注释您的方法并指定执行者bean名称,所以
@GetMapping("/fb-roles")
@Timed
@Async("taskExecutor")
public Callable<List<FbRole>> getAllFbRoles() {
log.debug("REST request to get all FbRoles");
return (() -> { return fbRoleRepository.findAll(); });
}
希望这会有所帮助
从警告“请在。当前正在使用的SimpleAsyncTaskExecutor在负载下不适合。”
我想知道您是否使用spring mvc吗?使用MVC,以下几个链接可能会有所帮助:
Configuring mvc async task executor in springboot application
mvc-servlet.xml:
<mvc:annotation-driven>
<mvc:async-support default-timeout="30000" task-executor="taskExecutor"
</mvc:annotation-driven>
AsyncConfig.java
@Configuration @EnableAsync public class AsyncConfig implements AsyncConfigurer { @Bean public AsyncTaskExecutor taskExecutor() { return new ConcurrentTaskExecutor(Executors.newCachedThreadPool()); } }