Close

Spring - Introduction to @Scheduled Annotation

[Last Updated: Oct 29, 2025]

@Scheduled annotation marks a method to be scheduled.

Definition of Scheduled

(Version: spring-framework 6.1.8)
package org.springframework.scheduling.annotation;
   ........
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
@Reflective
public @interface Scheduled {
    String CRON_DISABLED = ScheduledTaskRegistrar.CRON_DISABLED; 1
    String cron() default ""; 2
    String zone() default ""; 3
    long fixedDelay() default -1; 4
    String fixedDelayString() default ""; 5
    long fixedRate() default -1; 6
    String fixedRateString() default ""; 7
    long initialDelay() default -1; 8
    String initialDelayString() default ""; 9
    TimeUnit timeUnit() default TimeUnit.MILLISECONDS; 10
    String scheduler() default ""; 11
}
1CRON_DISABLED
A special cron expression value that indicates a disabled trigger.
2cron()
A cron-like expression, extending the usual UN*X definition to include triggers on the second, minute, hour, day of month, month, and day of week.
For example, "0 * * * * MON-FRI" means once per minute on weekdays (at the top of the minute - the 0th second). The fields read from left to right are interpreted as follows.
  • second
  • minute
  • hour
  • day of month
  • month
  • day of week
3zone()
A time zone for which the cron expression will be resolved. By default, this attribute is the empty String (i.e. the scheduler's time zone will be used).
4fixedDelay()
Execute the annotated method with a fixed period between the end of the last invocation and the start of the next.
5fixedDelayString()
Execute the annotated method with a fixed period between the end of the last invocation and the start of the next.
The delay is specified as a String value here.

This attribute variant supports Spring-style "${...}" placeholders as well as SpEL expressions.
6fixedRate()
Execute the annotated method with a fixed period between invocations.
7fixedRateString()
Execute the annotated method with a fixed period (specified as String) between invocations.
This attribute variant supports Spring-style "${...}" placeholders as well as SpEL expressions.
8initialDelay()
Number of units of time to delay before the first execution of a #fixedRate or #fixedDelay task.
9initialDelayString()
Number of units of time to delay before the first execution of a #fixedRate or #fixedDelay task. br> Here the initial delay is specified as a String value.

This attribute variant supports Spring-style "${...}" placeholders as well as SpEL expressions.
10timeUnit()
The TimeUnit to use for #fixedDelay, #fixedDelayString, #fixedRate, #fixedRateString, #initialDelay, and #initialDelayString.

This attribute is ignored for cron expressions and for java.time.Duration values supplied via #fixedDelayString, #fixedRateString, or #initialDelayString.
11scheduler()
A qualifier for determining a scheduler to run this scheduled method on.
May be used to determine the target scheduler to be used, matching the qualifier value (or the bean name) of a specific org.springframework.scheduling.TaskScheduler or java.util.concurrent.ScheduledExecutorService bean definition.

Difference between fixedDelay and fixedRated

fixedDelay
waits for the previous execution to complete PLUS the delay period before starting the next execution.

fixedRate
executes at fixed intervals regardless of previous execution completion.

How to use @Schedule?

For periodic tasks, exactly one of the cron(), fixedDelay(), or fixedRate() attributes must be specified, and additionally an optional initialDelay(). For a one-time task, it is sufficient to just specify an initialDelay().

@Schedule annotated Method

The annotated method must not accept arguments. It will typically have a void return type; if not, the returned value will be ignored when called through the scheduler.

@EnableScheduling annotation

This annotation enables Spring's scheduled task execution capability.
This annotation is used on @Configuration classes as follows:

@Configuration
 @EnableScheduling
 public class AppConfig {
 ...
 }

Immediate Scheduling after Spring context initialization

ScheduledAnnotationBeanPostProcessor is responsible for detecting @Scheduled methods and registering them with a TaskScheduler.
This BeanPostProcessor is automatically registered when we use @EnableScheduling on Spring's configuration class.
This process occurs during the bean initialization phase of the Spring context. Once registered, the TaskScheduler will begin managing the execution of these tasks according to their defined schedule.

See Also