hm1
hm1

Reputation: 186

Spring @scheduled cron expression for every 1st Monday of the month

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

Answers (5)

Navid H
Navid H

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

Mạnh Quyết Nguyễn
Mạnh Quyết Nguyễn

Reputation: 18235

AFAIK, Spring schedule does not support full unix-style cron format.

CronSequenceGenerator

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

Jeff W
Jeff W

Reputation: 1

@Scheduled(cron = "0 0 7 1-7 * MON")

Upvotes: 0

dimitrisli
dimitrisli

Reputation: 21391

0 0 10 ? * 2#1
  • 0 -> 0th second \
  • 0 -> 0th minute |-> 10:00am
  • 10 -> 10th hour /
  • 2#1 -> 1st Monday of each month

Upvotes: 1

Liping Huang
Liping Huang

Reputation: 4476

You can use this expression

 @Scheduled(cron = "0 0 10 ? 1/1 MON#1 *")

Upvotes: 0

Related Questions