Reputation: 186
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() {
// ...
}
When I write:
@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
Upvotes: 4
Views: 9709
Reputation: 451
I have answered a similar question here How to fire the job on first monday of month using cron expresssion in spring @Scheduled?
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
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"
Upvotes: 7
Reputation: 18235
AFAIK, Spring schedule does not support full unix-style cron format.
In your case, you can try to switch to Quartz instead of innate scheduling.
Your Trigger should be configured to run with the cron expression: 0 0 10 ? * 2#1
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("triggerIdentity")
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 10 ? * 2#1"))
.build();
Upvotes: 0
Reputation: 21391
0 0 10 ? * 2#1
Upvotes: 1
Reputation: 4476
You can use this expression
@Scheduled(cron = "0 0 10 ? 1/1 MON#1 *")
Upvotes: 0