gstackoverflow
gstackoverflow

Reputation: 36984

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

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

Upvotes: 5

Views: 5625

Answers (3)

Swarit Agarwal
Swarit Agarwal

Reputation: 2648

This may work for you

* 6 * 12/1 1

For description check here

https://cronexpressiondescriptor.azurewebsites.net

Upvotes: 0

Navid H
Navid H

Reputation: 451

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

Upvotes: 6

kvk95
kvk95

Reputation: 138

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

Upvotes: -1

Related Questions