java.lang.IllegalStateException:遇到无效的@Scheduled 方法'execute':对于输入字符串:“1#1”

gst*_*low 9 java cron spring scheduled-tasks spring-boot

我有以下方法声明:

@Scheduled(cron = "0 0 12 ? * MON#1")
protected synchronized void execute() {...}
Run Code Online (Sandbox Code Playgroud)

但是我的应用程序启动失败,我在日志中看到以下错误:

Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'execute': For input string: "1#1"
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.processScheduled(ScheduledAnnotationBeanPostProcessor.java:461) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:331) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:423) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1633) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    ... 19 common frames omitted
Run Code Online (Sandbox Code Playgroud)

如何纠正?

Ach*_*ake 6

当 Spring boot 无法识别 cron 表达式时,会发生该错误。应该可能是由于零件 MON#1

Spring 文档这篇文章可能有助于在 Spring boot 中找到正确的 cron 表达式。

文档中的一些示例:

"0 0 * * * *" = the top of every hour of every day.
"*/10 * * * * *" = every ten seconds.
"0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.
"0 0 8,10 * * *" = 8 and 10 o'clock of every day.
"0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30 and 10 o'clock every day.
"0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays
"0 0 0 25 12 ?" = every Christmas Day at midnight
Run Code Online (Sandbox Code Playgroud)

从帖子中(包括秒数):

每天中午12:00: 0 0 12 * * ?

每天从下午 1 点开始到下午 1:55 结束,然后从下午 6 点开始到下午 6:55 结束,每五分钟一次: 0 0/5 13,18 * * ?

每天下午 1 点开始到下午 1:05 结束的每分钟: 0 0-5 13 * * ?

六月每周二下午 1:15 和 1:45: 0 15,45 13 ? 6 Tue

每周一、周二、周三、周四、周五上午 9:30: 0 30 9 ? * MON-FRI

每月15日上午9点30分: 0 30 9 15 * ?

每月最后一天下午 6 点: 0 0 18 L * ?

每月第三天至最后一天下午 6 点: 0 0 18 L-3 * ?

每月最后一个星期四上午 10:30: 0 30 10 ? * 5L

每月第三个星期一上午 10 点: 0 0 10 ? * 2#3

从该月 10 日开始,连续五天,每天午夜 12 点: 0 0 0 10/5 * ?