Spring Boot – How to Configure Scheduler with Initial Delay

This article explains how to configure Schedule with initial delay. Initial delay is a period of time that Scheduler is waiting for before the configured task is executed. InitialDelay is a element in @Scheduled annotation that is added to a method.

You can use initialDelay parameter along with fixedRate and fixedDelay in @scheduled annotation to delay the first execution of the task that is specified in number of milliseconds.

There are two ways to configure initial delay.



Configure Initial Delay Value

The initial delay value is configured in @Schedued. annotation. A valid integer value can be set to the initial delay value. If the value is set to a negative, then the value is assumed to be zero. By default, the initial delay value is zero.

The example below shows how to configure initial delay value in @Scheduled annotation.

package com.yawintutor;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class SchedulerInitialDelay {
   private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
   
   @Scheduled(fixedDelay = 3000, initialDelay = 5000)
   public void task() {
      System.out.println("Scheduler (initialDelay = 5000) task with duration : " + sdf.format(new Date()));
   }
}


Output

The following is the output of the above program

Scheduler (initialDelay = 5000) task with duration : 2019-12-03 23:09:22.982
Scheduler (initialDelay = 5000) task with duration : 2019-12-03 23:09:25.986
Scheduler (initialDelay = 5000) task with duration : 2019-12-03 23:09:28.987
Scheduler (initialDelay = 5000) task with duration : 2019-12-03 23:09:31.988
Scheduler (initialDelay = 5000) task with duration : 2019-12-03 23:09:34.993


Configure Initial Delay String Value

The initial delay value can be configured as string using initialDelayString element in the @Scheduled annotation. The following example shows how to configure initial delay value as string.

package com.yawintutor;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class SchedulerInitialDelayString {
   private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
   
   @Scheduled(fixedDelay = 3000, initialDelayString = "5000")
   public void task() {
      System.out.println("Scheduler (initialDelayString = 5000) task with duration : " + sdf.format(new Date()));
   }
}


Output

Scheduler (initialDelayString = 5000) task with duration : 2019-12-03 23:13:23.056
Scheduler (initialDelayString = 5000) task with duration : 2019-12-03 23:13:26.061
Scheduler (initialDelayString = 5000) task with duration : 2019-12-03 23:13:29.065
Scheduler (initialDelayString = 5000) task with duration : 2019-12-03 23:13:32.068
Scheduler (initialDelayString = 5000) task with duration : 2019-12-03 23:13:35.070


Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *