java.lang.IllegalStateException : Encountered invalid @Scheduled method 'execute' : For input string: "1#1"

标签 java spring spring-boot cron scheduled-tasks

我有以下方法声明:

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

但是我的应用程序启动失败,我在日志中看到以下错误:
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

如何纠正?

最佳答案

当 Spring boot 无法识别 cron 表达式时会发生错误。应该是由于 MON#1部分。
Spring documentationThis post可能有助于在 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

从帖子中(包括秒数):

At 12:00 p.m. (noon) every day: 0 0 12 * * ?

Every five minutes starting at 1 p.m. and ending at 1:55 p.m. and then starting at 6 p.m. and ending at 6:55 p.m., every day: 0 0/5 13,18 * * ?

Every minute starting at 1 p.m. and ending at 1:05 p.m., every day: 0 0-5 13 * * ?

At 1:15 p.m. and 1:45 p.m. every Tuesday in the month of June: 0 15,45 13 ? 6 Tue

At 9:30 a.m. every Monday, Tuesday, Wednesday, Thursday and Friday: 0 30 9 ? * MON-FRI

At 9:30 a.m. on the 15th day of every month: 0 30 9 15 * ?

At 6 p.m. on the last day of every month: 0 0 18 L * ?

At 6 p.m. on the third to last day of every month: 0 0 18 L-3 * ?

At 10:30 a.m. on the last Thursday of every month: 0 30 10 ? * 5L

At 10 a.m. on the third Monday of every month: 0 0 10 ? * 2#3

At 12 midnight on every day for five days starting on the 10th day of the month: 0 0 0 10/5 * ?

关于java.lang.IllegalStateException : Encountered invalid @Scheduled method 'execute' : For input string: "1#1",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51658643/

相关文章:

java - 假装和未声明的 404 Not Found 可抛出异常

java - arrayListName.sort(null) 是做什么的?

java - 无法访问句柄事件中的按钮/文本区域

java - Spring框架XML配置与Annotation配置执行流程的区别

java - 如何将 HttpServletRequest 转换为 OAuth2Request 对象?

java - Spring Boot Rest Controller 通用 POST 类型

spring - Spring Webflux 安全中的角色层次结构

java - Jmeter 没有获得我的 csv 文件中正确的行数

spring - 禁用 SpringSecurity 的 SavedRequest 存储逻辑

java - 如何使用 Spring MVC/Spring Boot 编写适当的全局错误处理程序