每个月的第一个星期一的 Spring @scheduled cron 表达式 [英] Spring @scheduled cron expression for every 1st Monday of the month
问题描述
我正在尝试根据 Spring cron 表达式按固定时间表执行我的代码.我希望代码在每个月的第一个星期一上午 10:00 执行.
I'm trying to have my code execute on a fixed schedule, based on a Spring cron expression. I would like the code to be executed on every first Monday of the Month at 10:00 am.
@Scheduled(cron = "")
public void sendEmail() {
// ...
}
当我写:
@Scheduled(cron = "0 0 12 ? * MON#1")
protected synchronized void execute() {...}
应用程序在启动时打印以下错误:
Application prints following error on startup:
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
推荐答案
我在这里回答了一个类似的问题如何在春季@Scheduled 中使用 cron 表达式在每月的第一个星期一解雇工作?
I have answered a similar question here How to fire the job on first monday of month using cron expresssion in spring @Scheduled?
该模式是一个由六个单独的空格分隔的字段的列表:代表秒、分、小时、日、月、工作日.月份和工作日名称可以作为英文名称的前三个字母给出.https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/support/CronSequenceGenerator.html
The pattern is a list of six single space-separated fields: representing second, minute, hour, day, month, weekday. Month and weekday names can be given as the first three letters of the English names. https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/support/CronSequenceGenerator.html
你可以使用这个表达式.一个月的前 7 天只有一个星期一.
You can use this expression for that. There is only one Monday in the first 7 days of a month.
"0 0 10 1-7 * MON"
这篇关于每个月的第一个星期一的 Spring @scheduled cron 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!