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


使用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;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM