@EnableScheduling switches on Spring's support for @Scheduled methods. Without it, @Scheduled annotations are silently ignored. With it, Spring registers a post-processor that finds those methods and runs them on a task scheduler according to a fixed rate, fixed delay or cron expression.
Scheduled tasks do not run in parallel by default: Spring Boot's default scheduler uses a single thread, so one long task can delay all the others.
@EnableScheduling does@EnableScheduling doesThe annotation imports SchedulingConfiguration, which registers a ScheduledAnnotationBeanPostProcessor. After the context starts, that processor looks for @Scheduled methods on every bean, creates the matching trigger and hands it to a TaskScheduler. If you did not define a scheduler, Spring looks for one by type or by the name taskScheduler and falls back to a local single-threaded executor.
In Spring Boot, TaskSchedulingAutoConfiguration creates a ThreadPoolTaskScheduler once @EnableScheduling is present. Its pool size is controlled with spring.task.scheduling.pool.size and defaults to 1.
Put the annotation on any configuration class, or on the main class:
Then annotate methods. A scheduled method must return void (any return value is ignored) and take no parameters:
| Attribute | Meaning | Use when |
|---|---|---|
fixedRate | Start times are a fixed interval apart, measured from the previous start | Regular heartbeat or polling |
fixedDelay | Wait a fixed time after the previous run finishes | Runs must never overlap |
cron | Six-field expression: second, minute, hour, day of month, month, day of week | Calendar-based jobs |
initialDelay | Delay before the first run | Let the application warm up |
With a single scheduler thread, fixedRate runs do not overlap either; a slow run simply makes the next one start late. Times can also be written as ISO-8601 durations, for example fixedDelayString = "PT30S".
Spring cron expressions have six fields, not the five of Unix cron. 0 */10 * * * * means every ten minutes at second zero. If you omit zone, the server's time zone is used, so set it explicitly for jobs tied to a business day.
Keep schedules out of the code with placeholders so they can differ per environment:
The special value - (app.cleanup.cron: "-") disables a cron-based task, which is handy for turning a job off in one environment.
Because the default pool has one thread, increase it when you have several independent jobs:
On Java 21 or later with Spring Boot 3.2 or newer, you can set spring.threads.virtual.enabled=true to run scheduled tasks on virtual threads. In that mode Boot uses a SimpleAsyncTaskScheduler, so tasks start on their own threads. To run a single method asynchronously without changing the scheduler, add @EnableAsync and @Async to it; this is a separate feature from scheduling.
@EnableScheduling is per JVM. If you run three copies of the service, a cron job fires three times. Options include:
CronJob.@Scheduled does nothing and no error appears.Thread.sleep or blocking I/O in a task with a one-thread pool, which delays every other job.@Scheduled on a bean that is created lazily or in a class not managed by Spring.@EnableScheduling make tasks asynchronous?No. Tasks run on the scheduler's thread pool, which has one thread by default, so they run one after another. Increase the pool size or use virtual threads for parallelism.
@EnableScheduling?On any @Configuration class, often a dedicated SchedulingConfig. Putting it on a separate class makes it easy to exclude in tests.
fixedRate and fixedDelay?fixedRate measures the interval between start times, while fixedDelay measures the time from the end of one run to the start of the next.
Yes. Inject TaskScheduler and call schedule(runnable, trigger), or use SchedulingConfigurer to register tasks programmatically.
@EnableScheduling activates @Scheduled processing. Spring Boot then provides a scheduler with a single thread unless you configure more. Choose between fixedRate, fixedDelay and cron deliberately, externalise schedules, set a time zone, and use a lock or a dedicated job runner when the application runs on more than one instance.