Hello,
We believe there is an error with the use of cron expressions. We currently encounter an error when using crons expressions for job triggering. we want our job to run every 45 seconds, so we use the following expression */45 * * * * * like the one in the documentation which allows a job to run every 10 seconds (https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/support/CronExpression.html). The job does not run every 45 seconds but according to the following dates : HH:MM:45 and HH:MM:00
This is how we use the cron expression :
@Scheduled(cron = "${myapp.jobs.update-cron}")
public void update() {
// Code here
}
With myapp.jobs.update-cron defined in the application.properties file :
myapp.jobs.update-cron=*/45 * * * * *
We wrote the following main to test the next executions :
public static void main(String[] args) {
CronExpression cronExpression = CronExpression.parse("*/45 * * * * *");
LocalDateTime date = LocalDateTime.now();
for (int i = 0; i < 50; i++) {
date = cronExpression.next(date);
System.out.println(date);
}
}
And this is the result :
2022-06-29T16:20:45
2022-06-29T16:21
2022-06-29T16:21:45
2022-06-29T16:22
2022-06-29T16:22:45
2022-06-29T16:23
2022-06-29T16:23:45
2022-06-29T16:24
2022-06-29T16:24:45
2022-06-29T16:25
2022-06-29T16:25:45
2022-06-29T16:26
2022-06-29T16:26:45
2022-06-29T16:27
2022-06-29T16:27:45
2022-06-29T16:28
2022-06-29T16:28:45
2022-06-29T16:29
2022-06-29T16:29:45
2022-06-29T16:30
2022-06-29T16:30:45
2022-06-29T16:31
2022-06-29T16:31:45
2022-06-29T16:32
2022-06-29T16:32:45
2022-06-29T16:33
2022-06-29T16:33:45
2022-06-29T16:34
2022-06-29T16:34:45
2022-06-29T16:35
2022-06-29T16:35:45
2022-06-29T16:36
2022-06-29T16:36:45
2022-06-29T16:37
2022-06-29T16:37:45
2022-06-29T16:38
2022-06-29T16:38:45
2022-06-29T16:39
2022-06-29T16:39:45
2022-06-29T16:40
2022-06-29T16:40:45
2022-06-29T16:41
2022-06-29T16:41:45
2022-06-29T16:42
2022-06-29T16:42:45
2022-06-29T16:43
2022-06-29T16:43:45
2022-06-29T16:44
2022-06-29T16:44:45
2022-06-29T16:45
Spring boot version : 2.6.9 Java : Java 11
Thanks in advance
Comment From: wilkinsona
Your expression is incorrect. It should be 45 * * * * *. You can use a tool like this one to check your cron expressions.