I am running with Spring Boot v2.4.1 and Spring v5.3.2
With I try to run with @Scheduled(cron = "0 0 10 ? * TUE#1")
works fine, as discussed in #22436.
But when I try to run with @Scheduled(cron = "0 0 10 ? * TUE#1,TUE#3,TUE#5")
it gives the error below.
My code example is as follows:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
@SpringBootApplication
@EnableScheduling
public class TestScheduledApplication{
public static void main(String[] args) {
SpringApplication.run(TestScheduledApplication.class, args);
}
@Scheduled(cron = "0 0 10 ? * TUE#1,TUE#3,TUE#5")
public void testScheduled(){
System.out.println("hello world");
}
}
Error:
Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'testScheduled': For input string: "2#1,2#3,2" in cron expression "0 0 10 ? * TUE#1,TUE#3,TUE#5"
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.processScheduled(ScheduledAnnotationBeanPostProcessor.java:511)
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$null$1(ScheduledAnnotationBeanPostProcessor.java:374)
at java.lang.Iterable.forEach(Iterable.java:75)
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$postProcessAfterInitialization$2(ScheduledAnnotationBeanPostProcessor.java:374)
at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684)
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:373)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:444)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:609)
Is this a real issue or is not suported at all?
PS: As workaround I'm creating 3 separated schedules:
@Scheduled(cron = "0 0 10 ? * TUE#1"), @Scheduled(cron = "0 0 10 ? * TUE#3"), @Scheduled(cron = "0 0 10 ? * TUE#5")
Comment From: arunpsg
@alvaro-nogueira I believe this cannot be achieved with normal cron expression.
Specifying multiple nth day with cron expression is not supported.
You can validate your cron expression here --> http://www.cronmaker.com.
An alternative approach is to use Triggers or as you have mentioned you need to use multiple schedulers.
Hope this helps.
This is not an issue with @Schedule
or Spring.
Comment From: sbrannen
As @arunpsg aluded to, specifying a list of nth day of the week
values is not supported.
However, Spring's CronExpression
parsing infrastructure does support lists of days of the week.
Actually, it appears that lists are supported for all entries in a cron expression as long as the entry does not use a Quartz-specific feature or a macro.
I've introduced tests for the status quo in 5c1176786c9f0d02aa3db00ba9c5dba19a526ac2.
The documentation in the reference manual for supported Cron syntax states the following.
Commas (
,
) are used to separate items of a list.
That statement is misleading since comma-separated lists are not supported when using Quartz features like L
or #
or when using macros.
@poutsma, what do you think about repurposing this issue to improve the documentation (reference manual and Javadoc) to be more explicit about when comma-separated lists are supported?
Comment From: poutsma
Instead of documenting the missing support for lists with Quartz expressions, I decided to add it instead.