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)
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With