What do different Quartz TriggerState enum states represent and how do they get set?
I have a Spring 3.1 Quartz 2.0.1 utility used to run scheduled jobs. My jobs only repeat a fixed number of times. I expect that my TriggerState to go to COMPLETE when all the jobs are done. However, I see it go from NORMAL to NONE and I never see COMPLETE. I couldn't find much documentation on TriggerState behavior.
Relevant Java code:
SchedulerFactory schedFactory = new StdSchedulerFactory();
Scheduler scheduler = = schedFactory.getScheduler();
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
while (System.in.available() == 0) {
// Get trigger state
TriggerState triggerState = scheduler.getTriggerState(triggerKey);
if ((triggerState == TriggerState.COMPLETE)) {
break;
}
// Sleep for 100 msec
Thread.sleep(SCHEDULER_SLEEP_TIME);
}
Job configuration in Spring xml:
<bean id="mockJobDetail"
class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass"
value="mypackage.MockJob"/>
</bean>
<bean id="simpleMockJobTrigger"
class="org.quartz.impl.triggers.SimpleTriggerImpl" >
<property name="name" value="simpleMockJobTrigger" />
<!-- run every 50 milliseconds and start immediately, run 3 times -->
<property name="repeatInterval" value="50" />
<property name="startTime">
<bean factory-bean="dateFormat" factory-method="parse">
<constructor-arg value="2012-01-01" />
</bean>
</property>
<property name="repeatCount" value="2" />
</bean>
From looking at the code, it looks like the TriggerState goes to COMPLETE
state and is then immediately removed from all tracking hash maps. That is why scheduler.getTriggerState(triggerKey)
returns TriggerState.NONE
-- this state is returned when the trigger corresponding to the triggerKey
does not exist.
So, to check the completion of a trigger, it is possible to do:
if ((triggerState == TriggerState.COMPLETE) || (triggerState == TriggerState.NONE)) {
// do stuff }
OR
to use a custom TriggerListener
that does something when the trigger is complete.