DEV Community

Raja Anbazhagan
Raja Anbazhagan

Posted on

The @Scheduled annotation may be deceiving you..!

Is your @Scheduled method with fixedRate is not firing exactly at the time interval configured? Here I try to explain this behaviour and how you can fix it.

It all has to do with the default ThreadPoolTaskScheduler that is provided by Spring Boot’s auto-configuration. This Executor’s thread pool size is 1 by default. Every method that uses @Scheduled annotation will have to use this single thread.

Spring has deliberately done this so that there won’t be two runs of the same method happening in parallel. To achieve parallelism, we need to do three things.

1) Enable Async Support by annotating the Spring Boot main class with @EnableAsync.
2) Mark The @Scheduled method with @Async.
3) Increase the default number of threads available using spring.task.scheduling.pool.size property.

You can find detailed explanation about this behaviour at the post about how The @Scheduled annotation may be deceiving you..!

Top comments (0)