简体   繁体   中英

Scheduling task using Spring Scheduler

I am using cron expression for the last working day of the month like this:

@Scheduled(cron = "0 0 8 LW * ?")

But after running this I got:

java.lang.IllegalStateException: Encountered invalid @Scheduled method 'fetchEmployeesDetailsAndSendNotification': For input string: "LW"

although the cron expression is valid.

Why am I getting this exception, and how can I fix it?

It would seem that your pattern is incorrect. The quartz scheduler format is not exactly the same as the Linux crontab format.

While quartz allows the definition of LW. The spring scheduler format (which you are using via the @Scheduled annotation) does not.

See the javadoc for Spring's CronSequenceGenerator which references the linux man page for the correct crontab patterns

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. Try this:

@Scheduled(cron = "0 0 8 28-31 * ?")
public void yourMethod() {
    Calendar calendar = Calendar.getInstance();
    if (calendar.get(Calendar.DATE) == calendar.getActualMaximum(Calendar.DATE)) {
        // do something...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM