Spring schedule - last day of month not working

假如想象 提交于 2019-12-24 05:21:25

问题


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

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

org.springframework.boot.SpringApplication:Application startup failed java.lang.IllegalStateException: Encountered invalid @Scheduled method 'monthEndSchedule': For input string: "L"

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

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

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

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


回答1:


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

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

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

Code will be like this -

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


来源:https://stackoverflow.com/questions/54415868/spring-schedule-last-day-of-month-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!