The @Scheduled annotation is Spring's declarative way to schedule a task: you put it on a method, and Spring calls that method periodically according to fixedRate, fixedDelay, initialDelay or cron. It exists so you can run background work in the same application without managing threads and timers yourself.
This article focuses on how the scheduling is executed, which matters when you rely on tasks for real work: which thread runs them, how overlap is handled, and how to configure the scheduler.
When @EnableScheduling is present, ScheduledAnnotationBeanPostProcessor scans beans for @Scheduled methods and registers each one with a TaskScheduler. The scheduler then triggers the method on its own worker threads.
Key facts:
void, take no arguments, and be on a Spring-managed bean.@Scheduled annotation produces one independent task. The annotation is repeatable, so one method can have several schedules.ThreadPoolTaskScheduler with a pool size of one by default.Runs at a constant interval measured between run starts.
Waits after each run finishes before starting the next.
Postpones the first run. It is useful when the task depends on caches or connections that need time to warm up.
Fires at calendar times. Spring uses six fields (second, minute, hour, day of month, month, day of week).
The ? character is accepted for "no specific value" in the day fields, so expressions copied from Quartz often work. Quartz's optional seventh year field does not, because Spring rejects it.
| Need | Use |
|---|---|
| Steady cadence, such as sampling every second | fixedRate |
| Never overlap, wait between runs | fixedDelay |
| Wall-clock time such as 02:00 daily | cron with zone |
| Delay first run | add initialDelay |
| Set interval from configuration | fixedRateString, fixedDelayString, or cron = "${...}" |
Values can come from properties, which is how you keep different intervals per environment:
With a single scheduler thread, a long task blocks every other schedule. Increase the pool in Spring Boot:
For plain Spring, define your own scheduler bean:
A larger pool lets different tasks run at the same time, but a single task still never runs concurrently with itself. If a task takes longer than its fixedRate, the next run starts when it finishes.
An exception thrown from a periodic @Scheduled method is logged by the scheduler's error handler, and the schedule continues. Wrap the work in try/catch if you need alerts or retries:
For orderly shutdown in Spring Boot, set spring.task.scheduling.shutdown.await-termination=true so running tasks can finish.
@Scheduled has no persistence and no coordination. If the application restarts, missed runs are lost, and in a cluster every instance runs the task. Use a distributed lock such as ShedLock, or a dedicated scheduler such as Quartz or JobRunr, when you need exactly-once execution, history or retries.
No. Spring does not start a second run of the same annotated method while a previous run is still in progress. With fixedRate the next run begins as soon as the current one ends.
Avoid it. Methods should be non-private on a Spring bean so that proxies and the processor can invoke them reliably.
Do not use @EnableScheduling in test configuration, set the cron to - through a property, or test the method by calling it directly.
@Scheduled registers a method with Spring's TaskScheduler. Pick fixedRate, fixedDelay or cron to match the cadence, externalise values with properties, enlarge the scheduler pool when tasks are slow, and add locking or a job library when you run several instances or need guaranteed execution.