Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with @Schedule with Quartz features in CronExpression #26289

Closed
alvaro-nogueira opened this issue Dec 16, 2020 · 3 comments
Closed

Problem with @Schedule with Quartz features in CronExpression #26289

alvaro-nogueira opened this issue Dec 16, 2020 · 3 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@alvaro-nogueira
Copy link

alvaro-nogueira commented Dec 16, 2020

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")

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 16, 2020
@arunpsg
Copy link

arunpsg commented Dec 30, 2020

@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.

@sbrannen
Copy link
Member

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 5c11767.

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?

@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 30, 2020
@sbrannen sbrannen added this to the 5.3.3 milestone Dec 30, 2020
@poutsma poutsma self-assigned this Jan 4, 2021
@poutsma poutsma added type: enhancement A general enhancement and removed type: documentation A documentation task labels Jan 7, 2021
@poutsma poutsma closed this as completed in d387d9a Jan 7, 2021
@poutsma
Copy link
Contributor

poutsma commented Jan 7, 2021

Instead of documenting the missing support for lists with Quartz expressions, I decided to add it instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

5 participants