I realized when I set a scheduled method to run every 59 seconds, it runs twice, in the 59th second of its current minute and the first second of the next minute.
The cron that was used: */59 * * * * *
The sample code is so simple but for more clarity:
@Component
public class LogScheduler {
Logger logger = LoggerFactory.getLogger(LogScheduler.class);
@Scheduled(cron = "${scheduler.logging.cron}")
public void logMessage(){
logger.info("some info");
}
}
The output:
2022-05-16 01:55:59.006 INFO 61503 --- [ scheduling-1] d.najmi.performantapp.task.LogScheduler : some info
2022-05-16 01:56:00.004 INFO 61503 --- [ scheduling-1] d.najmi.performantapp.task.LogScheduler : some info
For more information, I am using Spring web flux(netty) version 2.6.7
on an arm64 machine.
Comment From: yutimothy666
set cron to */31 * * * * * also runs twice
Comment From: wilkinsona
Thanks for the report. This appears to be a Spring Framework bug. The behaviour you have described can be reproduced with the following code:
CronTrigger trigger = new CronTrigger("*/59 * * * * *");
SimpleTriggerContext context = new SimpleTriggerContext();
Date nextExecutionTime;
for (int i = 0; i < 10; i++) {
nextExecutionTime = trigger.nextExecutionTime(context);
System.out.println(nextExecutionTime);
context.update(nextExecutionTime, nextExecutionTime, nextExecutionTime);
}
It will produce output similar to the following:
Mon May 16 11:04:59 BST 2022
Mon May 16 11:05:00 BST 2022
Mon May 16 11:05:59 BST 2022
Mon May 16 11:06:00 BST 2022
Mon May 16 11:06:59 BST 2022
Mon May 16 11:07:00 BST 2022
Mon May 16 11:07:59 BST 2022
Mon May 16 11:08:00 BST 2022
Mon May 16 11:08:59 BST 2022
Mon May 16 11:09:00 BST 2022
We'll transfer this to Spring Framework so that they can take a look.
Comment From: poutsma
I realized when I set a scheduled method to run every 59 seconds, it runs twice, in the 59th second of its current minute and the first second of the next minute.
The cron that was used:
*/59 * * * * *
This expression means "run every 59 seconds starting at 0 seconds". So 0
and then 59
are valid second values. Just like */30 * * * * *
would run at 0
and then 30
seconds.
If you want an expression that only runs at 59 seconds, use 59 * * * * *
Comment From: hari427
Similarly issues observed my cron expression is (0 /30 * * * ). scheduled job every 30 mins. My job is running 4 time.. Could you please suggest