Spring Websocket TaskExecutor-beans替换“ applicationTaskExecutor” -bean

时间:2019-10-07 12:32:59

标签: spring-boot

我有一个使用@Async-Tasks的Spring Boot 2.1.8应用程序。所有@Async-任务过去都是由名为ThreadPoolTaskExecutor的自动配置的applicationTaskExecutor-bean执行的。

我做了什么更改? 在类路径中使用spring-boot-starter-websocket并进行@EnableWebSocketMessageBroker配置后,applicationTaskExecutor-bean消失了,并被四个名称为

的bean所取代。
  • clientInboundChannelExecutor
  • clientOutboundChannelExecutor
  • brokerChannelExecutor
  • messageBrokerTaskScheduler

Spring记录到控制台:AnnotationAsyncExecutionInterceptor : More than one TaskExecutor bean found within the context, and none is named 'taskExecutor'. Mark one of them as primary or name it 'taskExecutor' (possibly as an alias) in order to use it for async processing: [clientInboundChannelExecutor, clientOutboundChannelExecutor, brokerChannelExecutor, messageBrokerTaskScheduler]

@Async-任务现在由SimpleAsyncTaskExecutor执行。

问题 为什么所有的豆子不能共存?为什么在配置spring-websockets时Spring不创建applicationTaskExecutor-bean?

1 个答案:

答案 0 :(得分:0)

为@M。注释中提到的Deinum是@ConditionalOnMissingBean [0]中的TaskExecutionAutoConfiguration.java导致的行为

我通过自己创建bean来解决它。

@ConditionalOnClass(ThreadPoolTaskExecutor.class)
@Configuration
public class ApplicationTaskExecutorBeanConfig {

    @Lazy
    @Bean(name = {APPLICATION_TASK_EXECUTOR_BEAN_NAME, DEFAULT_TASK_EXECUTOR_BEAN_NAME})
    public ThreadPoolTaskExecutor applicationTaskExecutor(TaskExecutorBuilder builder) {
        return builder.build();
    }

}

首先,我尝试通过用

注释我的bean工厂方法来选择TaskExecutionAutoConfiguration.java中的bean。
@ConditionalOnMissingBean(name = {APPLICATION_TASK_EXECUTOR_BEAN_NAME, DEFAULT_TASK_EXECUTOR_BEAN_NAME})

但是它没有用,因为我的bean工厂方法比TaskExecutionAutoConfiguration.java中的方法更早被调用。

[0] https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskExecutionAutoConfiguration.java#L78

感谢@M。 Deinum发表您的评论。