quartz spring cron trigger fire immediately

核能气质少年 提交于 2019-12-23 03:51:39

问题


I have a spring application that uses quartz cron trigger. I have given the following for frequency 0 0/20 * * * ?.....once every 20 min. But i want the first one to run immediately. Right now, when I start the application, it runs after 20 min. I was hoping it would run asap and then after 20 min.

Thanks in advance.


回答1:


You don't need CRON expression (and Quartz at all!) to run given code every 20 minutes. Just use fixed rate (Spring built-in):

@Scheduled(fixedRate=20 * 60 * 1000)

That's it! By default first invocation happens immediately, second after 20 minutes. Since Spring 3.2 you can even say initialDelay=10000 to run for the first time after exactly 10 seconds.

If you really want to use Quartz, check out SimpleTrigger.




回答2:


It sounds like you want to use an interval trigger (SimpleTrigger in Quartz can do the job). The CronTrigger wants you to specify the minutes at which to run.

So your trigger schedule says: start at 0 minutes, and run every 20 minutes after that until the hour is over. Then start at 0 again.

But with the SimpleTrigger, you say - start now and run every 20 minutes.

Here is a tutorial on SimpleTrigger: http://quartz-scheduler.org/documentation/quartz-2.x/tutorials/tutorial-lesson-05

Here is a tutorial on CronTrigger: http://quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger



来源:https://stackoverflow.com/questions/14591122/quartz-spring-cron-trigger-fire-immediately

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