@Scheduled marks a Spring bean method to run automatically on a schedule: at a fixed rate, with a fixed delay after the previous run, or on a cron expression. Spring registers the method with a task scheduler when you add @EnableScheduling to a configuration class. The method must return void and take no arguments.
It is the simplest way to run periodic work such as cleanup jobs, cache refreshes or report generation inside one application instance.
Add @EnableScheduling once, then annotate methods on Spring-managed beans.
The annotation only works on beans created by the container. Calling the method yourself, or putting it on an object created with new, schedules nothing.
| Attribute | Behaviour | Typical use |
|---|---|---|
fixedRate | Starts every N milliseconds, measured from the previous start | Polling, heartbeats |
fixedDelay | Waits N milliseconds after the previous run finishes | Jobs that must not overlap |
cron | Runs at times matching a cron expression | Daily or weekly jobs |
Each mode has a ...String variant (fixedRateString, fixedDelayString) that accepts a property placeholder or an ISO-8601 duration, so the interval can be configured externally.
Spring cron expressions have six fields: second, minute, hour, day of month, month, day of week.
Without zone, the server's default time zone is used, which makes behaviour change between machines. Always set a zone for business-hour jobs. Spring also supports macros such as @daily and @hourly, and the special value - (Scheduled.CRON_DISABLED) to switch a job off through configuration.
By default Spring uses one scheduler thread. If one task runs long, other scheduled tasks wait. In Spring Boot you can enlarge the pool with a property:
With Spring Boot 3.2 or later, setting spring.threads.virtual.enabled=true on Java 21 or newer makes Boot's scheduler use virtual threads. Alternatively, define your own TaskScheduler bean such as a ThreadPoolTaskScheduler.
If a scheduled method throws, the error is logged and later runs still happen for fixed-rate and fixed-delay tasks. Catch exceptions yourself when you need retries or alerts.
A single task never overlaps with itself on the default scheduler, because the next run waits. If you add @Async, runs can overlap, so use fixedDelay or guard the method.
@Scheduled is local to each JVM. If you run three replicas, the job runs three times. To make a job run once across instances, use a distributed lock such as ShedLock, a database lock, or a dedicated scheduler like Quartz in clustered mode. Kubernetes CronJob resources are another option for heavy batch work.
Do not wait for the clock in tests. Keep the logic in a normal method and call it directly, or use Awaitility for an integration check:
For tests where scheduling is not wanted, put @EnableScheduling in a profile-specific configuration class.
Usually @EnableScheduling is missing, the class is not a Spring bean, the method has parameters or a non-void return type, or the scheduler thread is blocked by another long task.
No. They must have no arguments. Read configuration from fields or injected beans instead.
fixedRate measures between starts, fixedDelay measures from the end of one run to the start of the next.
No. Schedules are in memory. After a restart the timers start again, so a missed run is not made up.
@Scheduled runs bean methods periodically using fixedRate, fixedDelay or cron. Enable it with @EnableScheduling, set a time zone on cron jobs, enlarge the scheduler pool when tasks are long, and add a distributed lock when you run several instances.