java Spring Async ThreadPoolTask​​Scheduler 未初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41672364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:02:11  来源:igfitidea点击:

Spring Async ThreadPoolTaskScheduler not initialized

javaspringasynchronous

提问by small_ticket

I'm trying to use Async annotation in Spring but I'm getting

我正在尝试在 Spring 中使用 Async 注释,但我得到了

java.lang.IllegalStateException: ThreadPoolTaskScheduler not initialized

error, when I try to run the method marked as Async. The following is the configuration for Async:

错误,当我尝试运行标记为异步的方法时。以下是 Async 的配置:

@EnableScheduling
@EnableAsync
@Configuration 
public class SchedulingConfiguration implements AsyncConfigurer{

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(10);
        return scheduler;
    }

}

and the following is the declaration of async method.

以下是异步方法的声明。

@Async
@Transactional(value = "baseTransactionManager", isolation = Isolation.READ_COMMITTED)
public void foo(Bar bar) {// some code here}

What am I missing in here?

我在这里缺少什么?

Thanks in advance.

提前致谢。

回答by Yaroslav Stavnichiy

You have to explicitly call scheduler.initialize()after setting all properties but before returning the scheduler.

您必须scheduler.initialize()在设置所有属性之后但在返回调度程序之前显式调用。

See full working test case here.

在此处查看完整的工作测试用例。