I'm working on upgrading Quartz from 1.8.6 to 2.2.1.
In the previous code, we created a trigger like so:
Trigger trigger = TriggerUtils.makeImmediateTrigger(0, 0);
This was valid syntax previously. However, since Quartz 2, they've moved to a builder-based system instead. Below is what I thought would accomplish the same thing, however, I get the error Repeat Interval Cannot Be Zero
.
Trigger trigger = newTrigger()
.withIdentity(getImmediateTriggerName(jobInfo))
.forJob(job)
.withSchedule(simpleSchedule()
.withIntervalInMilliseconds(0)
.withRepeatCount(-1))
.build();
Obviously this isn't working, so I'm wondering what would be the best way to accomplish the same thing that we were doing previously.
Thanks
It seems you want the trigger to fire only once.
According to the Quartz 1.8 source, calling TriggerUtils.makeImmediateTrigger(0, 0)
creates a SimpleTrigger
with repeat count 0 and repeat interval 0. According to a Quartz 1.8 tutorial, example 1, this creates a trigger that fires only once. This would therefore seem to be what your Quartz 1.8 code is doing.
From the Quartz 2.2 TriggerBuilder API documentation:
[If] you do not invoke withSchedule(..) method, a default schedule of firing once immediately will be used.
So, it would seem that all you need to do is to get rid of the withSchedule
section from your trigger:
Trigger trigger = newTrigger()
.withIdentity(getImmediateTriggerName(jobInfo))
.forJob(job)
.build();
I appreciate that the Quartz 1.8 documentation isn't entirely clear about repeat count. The documentation for the Quartz 2.2 ScheduleBuilder is much clearer on this point: the repeat count doesn't include the first firing. Despite seeing your code attempting to set the repeat count to -1, I can't believe you actually want the trigger to never fire, as triggers are automatically deleted after they have repeated the required number of times, and it would be pointless to create the trigger only to have it immediately deleted without it ever firing. In fact, if you set the repeat interval to a positive number of milliseconds and leave the repeat count at -1, you would get an error about the repeat count being negative.