春季计划-一个月的最后一天不工作 [英] Spring schedule - last day of month not working

查看:122
本文介绍了春季计划-一个月的最后一天不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在每个月的最后一天10:15和每个月的第一个星期日进行春季调度工作-

I wanted to run a spring scheduler job at 'last day of every month at 10:15' and 'First Sunday of every month' -

低于-但是在初始化spring上下文时却出现错误:

I have tried below - but it is giving error while initializing spring context:

org.springframework.boot.SpringApplication:应用程序启动失败
java.lang.IllegalStateException :遇到无效的@Scheduled方法'monthEndSchedule':对于输入字符串: L

@Override
@Scheduled(cron = "0 15 10 L * ?")
public void monthEndSchedule() { 
  //
}

尽管下面的作品在每天凌晨1点运行

Though below works which runs at 'every day 1 am'

@Override
@Scheduled(cron = "0 0 1 * * ?")
public void surveyDailySchedule() {
//
}

我使用的Cron表达式参考: http://www.quartz-sche duler.org/documentation/quartz-2.x/tutorials/crontrigger.html

Cron expression reference I have used : http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/crontrigger.html

推荐答案

Spring Scheduler不会支持 L输入字符串。因此,您需要采取一种解决方法。

Spring Scheduler does not support the "L" input string. So, you need to do a workaround.

首先,为每个月的最后几天(28,29,30,31)调用调度程序。

First, call scheduler for each of the possible last days of months (28,29,30,31).

然后,在功能块内使用if块检查是否为最后日期。如果是,则执行预期的任务。

Then, inside the function block check with an if block whether this is the last date. If it is, then perform the expected task.

代码将如下所示-

@Scheduled(cron = "0 15 10 28-31 * ?")
public void monthEndSchedule() {
    final Calendar c = Calendar.getInstance();
    if (c.get(Calendar.DATE) == c.getActualMaximum(Calendar.DATE)) {
        // do your stuff
    }
}

这篇关于春季计划-一个月的最后一天不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆