Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give minute in fixedDelay & initialDelay in @scheduled spring boot?

I am new for scheduler in spring. I read so many articles on @schedule but in every example they gave time in seconds and milliseconds.

Problem Statement : As per my requirement, after my program start my scheduler will start after 15 minutes (initial Delay ) and then it executes the task every after 5 minutes (FixedRate) . To achieve this how can I give time in minutes is their any best way to achieve this problem ?

Code :

@Configuration
@EnableScheduling
public class ScheduledConfiguration {
    @Scheduled(fixedDelay = 300000, initialDelay = 900000)
    public void scheduleFixedRateWithInitialDelayTask() {

        long now = System.currentTimeMillis() / 1000;
        System.out.println("Fixed rate task with one second initial delay - " + now);
    }
}

By using above program i will achive but I want to avoid 300000 / 900000 milliseconds . Other way

@Scheduled(fixedDelay = 5 * 60 * 1000, initialDelay = 15 * 60 * 1000)
like image 474
vijayk Avatar asked May 12 '20 10:05

vijayk


2 Answers

Although little bit late to the party, nevertheless another approach from Spring

@Scheduled(fixedDelayString = "PT15M", initialDelayString = "PT2H")

Here you can find the details about the syntax.

like image 55
LeO Avatar answered Sep 29 '22 18:09

LeO


Well, both fixedDelay and initialDelay accepts values in milliseconds. So you can either go with:

@Scheduled(fixedDelay = 300000, initialDelay = 900000)

Or:

@Scheduled(fixedDelay = 5 * 60 * 1000, initialDelay = 15 * 60 * 1000)
like image 20
weirdCoder Avatar answered Sep 29 '22 18:09

weirdCoder