gpt4 book ai didi

java - Spring Boot 中的计划作业

转载 作者:行者123 更新时间:2023-11-30 10:14:40 25 4
gpt4 key购买 nike

我在应用程序属性文件中设置了一个 cron 表达式,如下所示:

report.monthlyScheduleTime=0 10 07 1W * ?

并且注释如下,

@Scheduled(cron = "${report.monthlyScheduleTime}", zone="${report.scheduleTimeZone}")

但是当运行应用程序时,出现以下异常。

Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'ReportJob': For input string: "1W".

Spring Boot 似乎不接受 cron 表达式中定义的 1W。知道为什么吗?

最佳答案

Spring Boot seems to not accept 1W defined in the cron expression. Any idea why?

1W 特定于 Quartz,但您并未使用 Quartz。


根据 Quartz documentation,您的 cron 表达式 (0 10 07 1W * ?) 是正确的:

The ‘W’ is used to specify the weekday (Monday-Friday) nearest the given day. As an example, if you were to specify “15W” as the value for the day-of-month field, the meaning is: “the nearest weekday to the 15th of the month”.

但是 1W 模式是 Quartz 特有的。

@Scheduled ,您正在使用 Spring 自己的调度支持。这允许基于 Crontab pattern 的表达式但不支持 1W

所以你要么需要实际使用 Quartz,要么修改你的 cron 表达式。如果你想使用 Quartz,在 Spring documentation on scheduling ,“使用 Quartz 调度程序”部分描述了 Spring 对 Quartz 的支持。但是,如果要修改cron表达式,可以引用CronSequenceGenerator .

关于java - Spring Boot 中的计划作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50800788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com