Reputation: 1895
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
Upvotes: 0
Views: 5441
Reputation: 1672
The issue here is very clear. I was able to conclude this statement after I ran through the same issue and resolved it.
Here is the problem.
Trigger trigger = newTrigger()
.withIdentity(getImmediateTriggerName(jobInfo))
.forJob(job)
.withSchedule(simpleSchedule()
.withIntervalInMilliseconds(0)
.withRepeatCount(-1))
.build();
The issue is with .withIntervalInMilliseconds(0) .withRepeatCount(-1) as you cannot specify the Interval to zero. Just use the simpleSchedule() and this will by default trigger it only once.
Use in the following way,
Trigger trigger = newTrigger()
.withIdentity(getImmediateTriggerName(jobInfo))
.forJob(job)
.withSchedule(simpleSchedule())
.build();
Upvotes: 0
Reputation: 1727
You can also start the job with a delay by this trigger
Trigger jobTrigger = TriggerBuilder.newTrigger()
.withIdentity(jobname, groupName)
.startAt(new Date(System.currentTimeMillis() + startInterval * 1000L) )
.build();
This way trigger acts with a delay, but the job does not repeat
In case delay in start is not needed
Trigger jobTrigger = TriggerBuilder.newTrigger()
.withIdentity(jobname, groupName)
.build();
will do
Upvotes: 0
Reputation: 65054
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.
Upvotes: 2