The @Scheduled annotation in Spring marks a method to run automatically on a schedule: at a fixed rate, with a fixed delay after the previous run, or on a cron expression. It replaces hand-written timers for jobs like cleanup, report generation or cache refresh inside a single application instance.
Scheduling is switched on with @EnableScheduling. The annotated method must be void (any return value is ignored) and take no arguments, and it must belong to a Spring bean.
Add @EnableScheduling to a configuration class or to your main application class. Without it, @Scheduled annotations are silently ignored.
The three simple trigger types differ in what they measure from.
| Attribute | Meaning |
|---|---|
fixedRate | Time between the start of one run and the start of the next |
fixedDelay | Time between the end of one run and the start of the next |
initialDelay | Wait before the first run, used together with the two above |
heartbeat starts 2 seconds after startup and then every 5 seconds. syncData waits 10 seconds after each run finishes, so it runs roughly every 13 seconds. Use fixedDelay when runs must never overlap, and fixedRate when you need a steady cadence. By default values are in milliseconds; the timeUnit attribute (Spring 5.3.10 and later) lets you write timeUnit = TimeUnit.SECONDS.
Spring cron expressions have six fields: second, minute, hour, day of month, month and day of week. This differs from Unix cron, which has five and no seconds.
Spring also accepts macros such as @hourly, @daily and @weekly, and special characters like L (last) and # (nth weekday) in the day fields. Always set zone for jobs tied to local time, otherwise the server default zone applies and daylight saving changes may surprise you.
Hard-coded values are hard to change per environment. The string variants read from properties and accept ISO-8601 durations.
To disable a cron job in one environment, set its property to -, the special value that Spring treats as "never run".
spring.task.scheduling.pool.size, or define a TaskScheduler bean, for parallel jobs.spring.threads.virtual.enabled=true on Spring Boot 3.2 and later, the auto-configured scheduler runs jobs on virtual threads.@Scheduled runs on every application instance, so a job in three pods runs three times. Use a distributed lock such as ShedLock, a database-backed scheduler like Quartz, or a Kubernetes CronJob.fixedRate. If a run takes longer than the interval, runs do not overlap on a single thread; the next starts late.fixedRate measures from the start of one run to the start of the next, while fixedDelay measures from the end of one run to the start of the next.
Common causes are a missing @EnableScheduling, a class that is not a Spring bean, a method with parameters, or another long job blocking the single scheduler thread.
No. It must be a no-argument method. Read what you need from fields or services.
Use a distributed lock like ShedLock or an external scheduler, since every instance runs its own copy.
@Scheduled runs a Spring bean method repeatedly using fixedRate, fixedDelay, initialDelay or a six-field cron expression, once @EnableScheduling is active. Externalise schedules through properties, set a time zone for cron jobs, size the scheduler thread pool for concurrent jobs, and use a lock or external scheduler when the application runs in several instances.