如何在 spring @Scheduled 中使用 cron 表达式在每个月的第一个星期一触发作业?

How to fire the job on first monday of month using cron expresssion in spring @Scheduled?

现在我有以下声明:

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

它不起作用:

at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.5.9.RELEASE.jar:1.5.9.RELEASE]
Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'execute': For input string: "2#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

请帮忙让它正常工作

该模式是六个单独的 space 分隔字段的列表:表示
第二,
分钟,
小时,
天,
月,
工作日。
月份和星期名可以是英文名的前三个字母。

因此,每月前 7 天的星期一应该会产生您想要的结果。

"0 0 12 1-7 * 星期一"

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/support/CronSequenceGenerator.html

表达式错误使用@Scheduled(cron = "0 0 0 ? * 2#1 *")

有关此的更多信息,请参阅以下内容

这可能对你有用

* 6 * 12/1 1

在此处查看说明

https://cronexpressiondescriptor.azurewebsites.net