简体   繁体   English

使用Spring Scheduler安排任务

[英]Scheduling task using Spring Scheduler

I am using cron expression for the last working day of the month like this: 我在这样的月份的最后一个工作日使用cron表达式:

@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. 尽管cron表达式有效。

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. 石英调度程序格式与Linux crontab格式不完全相同。

While quartz allows the definition of LW. 而石英允许定义LW。 The spring scheduler format (which you are using via the @Scheduled annotation) does not. Spring Scheduler格式(您通过@Scheduled批注使用的格式)没有。

See the javadoc for Spring's CronSequenceGenerator which references the linux man page for the correct crontab patterns 请参阅Spring的CronSequenceGenerator的javadoc,该文档参考了linux手册页以获取正确的crontab模式

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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