4

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
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
vk1
  • 166
  • 6
  • 15

5 Answers5

7

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"
Navid H
  • 441
  • 4
  • 8
1
0 0 10 ? * 2#1
  • 0 -> 0th second \
  • 0 -> 0th minute |-> 10:00am
  • 10 -> 10th hour /
  • 2#1 -> 1st Monday of each month
dimitrisli
  • 20,895
  • 12
  • 59
  • 63
0

You can use this expression

 @Scheduled(cron = "0 0 10 ? 1/1 MON#1 *")
Liping Huang
  • 4,378
  • 4
  • 29
  • 46
0

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

Jeff W
  • 1
  • 1
0

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();
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51