Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalStateException: Encountered invalid @Scheduled method 'execute': For input string: "1#1"

I have the following method declaration:

@Scheduled(cron = "0 0 12 ? * MON#1")
protected synchronized void execute() {...}

But my application startup fails and I see following error in logs:

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

How to correct it?

like image 671
gstackoverflow Avatar asked Aug 02 '18 16:08

gstackoverflow


1 Answers

The error occurs when the cron expression is not recognizable by Spring boot. Should probably due to the MON#1 part.

Spring documentation and This post might be useful to find the correct cron expression in Spring boot.

Some examples from the documentation :

"0 0 * * * *" = the top of every hour of every day.
"*/10 * * * * *" = every ten seconds.
"0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.
"0 0 8,10 * * *" = 8 and 10 o'clock of every day.
"0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30 and 10 o'clock every day.
"0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays
"0 0 0 25 12 ?" = every Christmas Day at midnight

And from the post (with seconds included):

At 12:00 p.m. (noon) every day: 0 0 12 * * ?

Every five minutes starting at 1 p.m. and ending at 1:55 p.m. and then starting at 6 p.m. and ending at 6:55 p.m., every day: 0 0/5 13,18 * * ?

Every minute starting at 1 p.m. and ending at 1:05 p.m., every day: 0 0-5 13 * * ?

At 1:15 p.m. and 1:45 p.m. every Tuesday in the month of June: 0 15,45 13 ? 6 Tue

At 9:30 a.m. every Monday, Tuesday, Wednesday, Thursday and Friday: 0 30 9 ? * MON-FRI

At 9:30 a.m. on the 15th day of every month: 0 30 9 15 * ?

At 6 p.m. on the last day of every month: 0 0 18 L * ?

At 6 p.m. on the third to last day of every month: 0 0 18 L-3 * ?

At 10:30 a.m. on the last Thursday of every month: 0 30 10 ? * 5L

At 10 a.m. on the third Monday of every month: 0 0 10 ? * 2#3

At 12 midnight on every day for five days starting on the 10th day of the month: 0 0 0 10/5 * ?

like image 110
Achala Dissanayake Avatar answered Nov 01 '22 04:11

Achala Dissanayake