Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 274
gstackoverflow Avatar asked Aug 03 '18 09:08

gstackoverflow


People also ask

What does cron 0 * * * * * mean?

*/5 * * * * Execute a cron job every 5 minutes. 0 * * * * Execute a cron job every hour.

How do I run a cron job at a specific time?

Cron jobs use the 24 hour clock. Like minutes you can specify a specific hour such as 4 for 4 AM, or a wildcard for every hour, which would be * , every 3 hours which would be */3 or specific hours such as 3,7,14,21 . You can also set it to run hourly but only for office hours by using a range, in this case 8-17 .

How do I schedule a cron job in last month?

The trick is to specify a command that checks if the next day is the first day of the month. If so, it means the current day is the last of that month and the cron job can then execute.

How do I set a cron schedule?

Cron jobs are scheduled at recurring intervals, specified using a format based on unix-cron. You can define a schedule so that your job runs multiple times a day, or runs on specific days and months. (Although we no longer recommend its use, the legacy App Engine cron syntax is still supported for existing jobs.)


1 Answers

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

like image 85
Navid H Avatar answered Sep 28 '22 17:09

Navid H