Reputation: 479
Hi i am using the below SpringBoot cron expression to run my scheduled job twice a day. It should run at 10am and 4pm
@Scheduled(cron = "0 0 10,16 * * *")
I started my Springboot application at 3:30pm and it trigged the scheduled job at 4pm but then next day it didn't trigger the job at 10am
Is there any thing wrong with my cron expression above? Please note that this is not the unix cron expression. It takes only 6 params as per SpringBoot docs.
Upvotes: 4
Views: 4070
Reputation: 90427
The cron expression is correct. There should be other problems that cause your cron job does not run as expected.
Internally ,Spring uses CronSequenceGenerator
to generate the next trigger time for a cron expression. To prove that your cron expression is configured correctly, you can try to play with CronSequenceGenerator
.
For example , the following function will simply print out the next 10 trigger time of a cron expression :
public static void printNextTriggerTime(String cronExpression, LocalDateTime currentTime) {
CronSequenceGenerator generator = new CronSequenceGenerator(cronExpression);
Date d = Date.from(currentTime.atZone(ZoneId.systemDefault()).toInstant());
for (int i = 0; i < 10; i++) {
d = generator.next(d);
System.out.println(d);
}
}
And if I input your cron expression and starting time :
printNextTriggerTime("0 0 10,16 * * *", LocalDateTime.of(2019, 8, 20, 15, 30, 0));
It will output:
Tue Aug 20 16:00:00 HKT 2019
Wed Aug 21 10:00:00 HKT 2019
Wed Aug 21 16:00:00 HKT 2019
Thu Aug 22 10:00:00 HKT 2019
Thu Aug 22 16:00:00 HKT 2019
Fri Aug 23 10:00:00 HKT 2019
Fri Aug 23 16:00:00 HKT 2019
Sat Aug 24 10:00:00 HKT 2019
Sat Aug 24 16:00:00 HKT 2019
Sun Aug 25 10:00:00 HKT 2019
Upvotes: 8