Run a timer task on a specific day (1st of every month) using Spring

我的未来我决定 提交于 2020-01-13 10:26:48

问题


We have a requirement of running a job on 1st of every month (time: 00:00:00 AM) exactly.
We are using Spring framework's ScheduledTimerTask to schedule jobs using delay and period properties. This class doesn't support running a job on specific date.

Can somebody suggest, how we can solve that problem using Spring and Java technology?


回答1:


If you don't have to run this job on a single node in a cluster you can use Spring Task, see: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html

@Scheduled(cron="0 0 0 1 1/1 *")
public void doSomething() {
    // something that should execute on 1st day every month @ 00:00
}

For generating cron expressions try cronmaker.com

Please be aware that if you use this code in a cluster it will run on all nodes.




回答2:


You can use Quartz Scheduler. It integrates great with Spring.

http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger

http://www.quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/tutorial-lesson-06



来源:https://stackoverflow.com/questions/25501503/run-a-timer-task-on-a-specific-day-1st-of-every-month-using-spring

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