Using the @Scheduled Annotation

The Scheduled annotation can be added to any method of a bean and you should set either the fixedRate, fixedDelay or cron members.

Remember that the scope of the bean has an impact on behaviour. a @Singleton bean will share state (the fields of the instance) each time the scheduled method is executed, while for a @Prototype bean a new instance is created for each execution.

Scheduling at a Fixed Rate

To schedule a task at a fixed rate, use the fixedRate member. For example:

Fixed Rate Example

  1. @Scheduled(fixedRate = "5m")
  2. void everyFiveMinutes() {
  3. System.out.println("Executing everyFiveMinutes()");
  4. }

Fixed Rate Example

  1. @Scheduled(fixedRate = "5m")
  2. void everyFiveMinutes() {
  3. System.out.println("Executing everyFiveMinutes()")
  4. }

Fixed Rate Example

  1. @Scheduled(fixedRate = "5m")
  2. internal fun everyFiveMinutes() {
  3. println("Executing everyFiveMinutes()")
  4. }

The task above will execute every 5 minutes.

Scheduling with a Fixed Delay

To schedule a task so that it runs 5 minutes after the termination of the previous task use the fixedDelay member. For example:

Fixed Delay Example

  1. @Scheduled(fixedDelay = "5m")
  2. void fiveMinutesAfterLastExecution() {
  3. System.out.println("Executing fiveMinutesAfterLastExecution()");
  4. }

Fixed Delay Example

  1. @Scheduled(fixedDelay = "5m")
  2. void fiveMinutesAfterLastExecution() {
  3. System.out.println("Executing fiveMinutesAfterLastExecution()")
  4. }

Fixed Delay Example

  1. @Scheduled(fixedDelay = "5m")
  2. internal fun fiveMinutesAfterLastExecution() {
  3. println("Executing fiveMinutesAfterLastExecution()")
  4. }

Scheduling a Cron Task

To schedule a Cron task use the cron member:

Cron Example

  1. @Scheduled(cron = "0 15 10 ? * MON" )
  2. void everyMondayAtTenFifteenAm() {
  3. System.out.println("Executing everyMondayAtTenFifteenAm()");
  4. }

Cron Example

  1. @Scheduled(cron = "0 15 10 ? * MON" )
  2. void everyMondayAtTenFifteenAm() {
  3. System.out.println("Executing everyMondayAtTenFifteenAm()")
  4. }

Cron Example

  1. @Scheduled(cron = "0 15 10 ? * MON")
  2. internal fun everyMondayAtTenFifteenAm() {
  3. println("Executing everyMondayAtTenFifteenAm()")
  4. }

The above example will run the task every Monday morning at 10:15AM for the time zone of the server.

Scheduling with only an Initial Delay

To schedule a task so that it runs a single time after the server starts, use the initialDelay member:

Initial Delay Example

  1. @Scheduled(initialDelay = "1m" )
  2. void onceOneMinuteAfterStartup() {
  3. System.out.println("Executing onceOneMinuteAfterStartup()");
  4. }

Initial Delay Example

  1. @Scheduled(initialDelay = "1m" )
  2. void onceOneMinuteAfterStartup() {
  3. System.out.println("Executing onceOneMinuteAfterStartup()")
  4. }

Initial Delay Example

  1. @Scheduled(initialDelay = "1m")
  2. internal fun onceOneMinuteAfterStartup() {
  3. println("Executing onceOneMinuteAfterStartup()")
  4. }

The above example will only run a single time 1 minute after the server starts.