1. 程式人生 > >No task executor bean found for async processing: no bean of type TaskExecut

No task executor bean found for async processing: no bean of type TaskExecut

run cal 拒絕 沒有 試驗 user lsi fort con

使用springcloud,添加異步方法後,調用異步成功,但有個 No task executor bean found for async processing: no bean of type TaskExecut 的異常拋出。

經過試驗,確定是因為沒有配置異步線程池導致的。即使未配置,異步任務任然是成功的,可能使用的默認設置。

解決辦法是添加一個配置類,代碼如下所示:

@Configuration
public class AsyncConfig {
    @Bean
    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor 
= new ThreadPoolTaskExecutor(); executor.setCorePoolSize(5); // 設置核心線程數 executor.setMaxPoolSize(10); // 設置最大線程數 executor.setQueueCapacity(20); // 設置隊列容量 executor.setKeepAliveSeconds(60); // 設置線程活躍時間(秒) executor.setThreadNamePrefix("user-rpt-"); //
設置默認線程名稱 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());  // 設置拒絕策略 executor.setWaitForTasksToCompleteOnShutdown(true); // 等待所有任務結束後再關閉線程池 return executor; } }

No task executor bean found for async processing: no bean of type TaskExecut