问题
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