Friday, June 30, 2017

Quartz Trigger – possible to disable?

While working on a project that was using org.quartz.SimpleTrigger version 1.8.5, I found it hard to find documentation online pertaining to how you could set a quartz trigger to either 1. never run or 2. run once. Looking at the org.quartz.SimpleTrigger API page, it was clear that repeatCount could be used somehow.  I tried the following examples to confirm my findings.

For example:
1. If repeatCount is not explicitly set, it will be set to a default of SimpleTrigger.REPEAT_INDEFINITELY. The trigger will run forever with the configurations that you set for startTime, repeatInterval, etc. The two below examples are equivalent:

SimpleTrigger trigger = new SimpleTrigger();
trigger.setName(“dummyTriggerName”);
trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
trigger.setRepeatInterval(30000);

SimpleTrigger trigger = new SimpleTrigger();
trigger.setName(“dummyTriggerName”);
trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
trigger.setRepeatInterval(30000);

You could also use the value of -1 in place of SimpleTrigger.REPEAT_INDEFINITELY.

2. If you want the trigger to only run once, then set repeatCount to 0.
SimpleTrigger trigger = new SimpleTrigger();
trigger.setName(“dummyTriggerName”);
trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
trigger.setRepeatCount(0);
trigger.setRepeatInterval(30000);

3. There is no way to have the trigger never run.

In the project I was working on, the SimpleTrigger was used inside a COTS product custom class, configured using Spring. I didn’t have control over the custom class or spring file to disable the trigger from running without changing the OOB product. I ended up configuring the repeatCount property to 0 via an exposed property file so the trigger would only run once.

4. The other values to set repeatCount are to schedule the trigger on any misfire situation. Those values are explained below and from the API link http://quartz-scheduler.org/api/previous_versions/1.8.5/org/quartz/SimpleTrigger.html:

// Compiled from SimpleTrigger.java (version 1.5 : 49.0, super bit)
public class org.quartz.SimpleTrigger extends org.quartz.Trigger {

// Field descriptor #67 I
public static final int MISFIRE_INSTRUCTION_FIRE_NOW = 1;

// Field descriptor #67 I
public static final int MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT = 2;

// Field descriptor #67 I
public static final int MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT = 3;

// Field descriptor #67 I
public static final int MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT = 4;

// Field descriptor #67 I
public static final int MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_EXISTING_COUNT = 5;

// Field descriptor #67 I
public static final int REPEAT_INDEFINITELY = -1;

No comments:

Post a Comment

I appreciate your time in leaving a comment!