small_ticket
small_ticket

Reputation: 2030

Spring Async ThreadPoolTaskScheduler not initialized

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

java.lang.IllegalStateException: ThreadPoolTaskScheduler not initialized

error, when I try to run the method marked as Async. The following is the configuration for 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.

Upvotes: 40

Views: 32995

Answers (1)

Yaroslav Stavnichiy
Yaroslav Stavnichiy

Reputation: 21446

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

See full working test case here.

Upvotes: 76

Related Questions