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

回眸只為那壹抹淺笑 提交于 2020-01-05 03:32:03

问题


Now I have the following declaration:

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

and it doesn't work:

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

Please, help to make it working


回答1:


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.

So a Monday in the first 7 days of the month should generate what you are after.

"0 0 12 1-7 * MON"

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




回答2:


This may work for you

* 6 * 12/1 1

For description check here

https://cronexpressiondescriptor.azurewebsites.net




回答3:


the expression is wrong use @Scheduled(cron = "0 0 0 ? * 2#1 *")

Refer the below for more info on this

https://stackoverflow.com/a/26147143/3724760



来源:https://stackoverflow.com/questions/51669973/how-to-fire-the-job-on-first-monday-of-month-using-cron-expresssion-in-spring-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!