首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果同时使用@EnableAsync和@EnableWebSocketMessageBroker,Spring boot 2.2.6不会启动

如果同时使用@EnableAsync和@EnableWebSocketMessageBroker,Spring boot 2.2.6不会启动
EN

Stack Overflow用户
提问于 2020-07-09 01:26:47
回答 1查看 607关注 0票数 0

我正在尝试使用Web和@Async任务。

所以我创建了两个配置类:

代码语言:javascript
运行
复制
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig {
}

@Configuration
@EnableAsync
public class AsyncConfiguration {
}

我还有一个注入AsyncTaskExecutor的服务

代码语言:javascript
运行
复制
@Service
public class MyService {

    @Autowired
    private AsyncTaskExecutor asyncTaskExecutor;
}

在此之后,我的应用程序无法启动,并报告以下错误:

代码语言:javascript
运行
复制
***************************
APPLICATION FAILED TO START
***************************

Description:

Field asyncTaskExecutor in mypackage.MyService required a bean of type 'org.springframework.core.task.AsyncTaskExecutor' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)

The following candidates were found but could not be injected:
    - Bean method 'applicationTaskExecutor' in 'TaskExecutionAutoConfiguration' not loaded because @ConditionalOnMissingBean (types: java.util.concurrent.Executor; SearchStrategy: all) found beans of type 'java.util.concurrent.Executor' clientInboundChannelExecutor, clientOutboundChannelExecutor, brokerChannelExecutor
    - Bean method 'taskScheduler' in 'TaskSchedulingAutoConfiguration' not loaded because @ConditionalOnBean (names: org.springframework.context.annotation.internalScheduledAnnotationProcessor; SearchStrategy: all) did not find any beans named org.springframework.context.annotation.internalScheduledAnnotationProcessor


Action:

Consider revisiting the entries above or defining a bean of type 'org.springframework.core.task.AsyncTaskExecutor' in your configuration.

我该如何解决这个问题呢?

EN

回答 1

Stack Overflow用户

发布于 2020-07-09 01:49:32

适用于我的变通方法:

代码语言:javascript
运行
复制
import java.util.concurrent.Executor;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableAsync
public class AsyncConfiguration implements AsyncConfigurer {

    @Bean
    public AsyncTaskExecutor asyncTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(1);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(11);
        executor.setThreadNamePrefix("AsyncExecutor-");
        executor.initialize();
        return executor;
    }

    @Override
    public Executor getAsyncExecutor() {
        return asyncTaskExecutor();
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new SimpleAsyncUncaughtExceptionHandler();
    }

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62800313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档