Close

Spring - Introduction to @Scheduled Annotation

[Last Updated: Oct 29, 2025]

@Scheduled annotation marks a method to be scheduled.

Definition of Scheduled

Version: 7.0.4
 package org.springframework.scheduling.annotation;
 @Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 @Repeatable(Schedules.class)
 @Reflective
 public @interface Scheduled {
     String cron() default ""; 1
     String zone() default ""; 2
     long fixedRate() default -1; 3
     String fixedRateString() default ""; 4
     long fixedDelay() default -1; 5
     String fixedDelayString() default ""; 6
     long initialDelay() default -1; 7
     String initialDelayString() default ""; 8
     TimeUnit timeUnit() default TimeUnit.MILLISECONDS; 9
     String scheduler() default ""; 10
 }
1A 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.
2A time zone for which the cron expression will be resolved. (Since 4.0)
3Execute the annotated method with a fixed period between invocations.
4Execute the annotated method with a fixed period between invocations. (Since 3.2.2)
5Execute the annotated method with a fixed period between the end of the last invocation and the start of the next.
6Execute the annotated method with a fixed period between the end of the last invocation and the start of the next. (Since 3.2.2)
7Number of units of time to delay before the first execution of a #fixedRate or #fixedDelay task. (Since 3.2)
8Number of units of time to delay before the first execution of a #fixedRate or #fixedDelay task. (Since 3.2.2)
9The TimeUnit to use for #fixedDelay, #fixedDelayString, #fixedRate, #fixedRateString, #initialDelay, and #initialDelayString. (Since 5.3.10)
10A qualifier for determining a scheduler to run this scheduled method on. (Since 6.1)

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

Join