Spring @Scheduled(cron = "0 0 0 29 2 THU" ) 导致 IllegalArgumentException 在日异常中溢出



我们有一个spring Boot(2.2.6-RELEASE)调度器2024-02-29上运行作业,示例1如下

@Scheduled(cron = "0 0 0 29 2 THU") // A requirement to run a job far away from current time i.e on 2024-02-29
public void scheduleTaskUsingCronExpression() {
final long now = System.currentTimeMillis() / 1000;
System.out.println("schedule tasks using cron jobs - " + now);
}

它曾经工作得很好,但它开始失败2021-07-29这是一个星期四除了以下例外

java.lang.IllegalArgumentException: Overflow in day for expression "0 0 0 29 2 THU"
at org.springframework.scheduling.support.CronSequenceGenerator.findNextDay(CronSequenceGenerator.java:223)
at org.springframework.scheduling.support.CronSequenceGenerator.doNext(CronSequenceGenerator.java:189)
at org.springframework.scheduling.support.CronSequenceGenerator.doNext(CronSequenceGenerator.java:184)
at org.springframework.scheduling.support.CronSequenceGenerator.doNext(CronSequenceGenerator.java:175)
at org.springframework.scheduling.support.CronSequenceGenerator.next(CronSequenceGenerator.java:148)
at org.springframework.scheduling.support.CronTrigger.nextExecutionTime(CronTrigger.java:88)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.schedule(ReschedulingRunnable.java:75)
at org.springframework.scheduling.concurrent.ConcurrentTaskScheduler.schedule(ConcurrentTaskScheduler.java:182)
at org.springframework.scheduling.config.ScheduledTaskRegistrar.scheduleCronTask(ScheduledTaskRegistrar.java:431)
at org.springframework.scheduling.config.ScheduledTaskRegistrar.scheduleTasks(ScheduledTaskRegistrar.java:369)

注意:如果我单独测试了spring boot版本2.5.3运行正常,但是2.2.6.RELEASE我不能在项目中做同样的事情

@Scheduled(cron = "0 0 0 29 2 THU")不是07-29今天是2月29日。

你可以试试@Scheduled(cron = "0 0 0 29 7 ?")

文档地址:https://docs.spring.io/spring-framework/docs/3.0.x/javadoc-api/org/springframework/scheduling/support/CronSequenceGenerator.html

编辑:下面的代码为我工作

@Scheduled(cron = "0 0 0 29 2 ?", zone = "CET")
public void scheduleTaskUsingCronExpression() {
if(Year.now().getValue() == 2024) { //If you want specific year
final long now = System.currentTimeMillis() / 1000;
System.out.println("schedule tasks using cron jobs - " + now);
}
}

所以我做了一些挖掘,我认为这个版本差异的来源是2.5.2使用Spring版本5.3.8,而2.2.6RELEASE使用5.2.5RELEASE,并且在5.3中用于解释Crontab表达式的CronSequenceGenerator被弃用了,因为off (Quote) "并且它有几个已知的问题,Spring团队成员都不愿意解决。

(现在)弃用版本的文档相当平淡,所以我不完全确定错误是什么,因为这是一个非破坏性的变化,他们应该解析相同的值,但如前所述,它显然有几个问题,因为CronSequenceGenerator是基于已弃用的Java.util.date。跳年理论上应该是没有问题的,但跳秒不受支持。它们应该只在6月份发生,今年没有发生,但这就是我的研究带给我的。如果你能把你想要完成的东西贴出来,我们也许能调整你的Cronexpressions

注意:尽管Crontab只指定了5个字段,但是Spring实现使用了6个

最新更新