Skip to content

Commit

Permalink
Merge pull request #519 from chrisdennis/issue-505
Browse files Browse the repository at this point in the history
Fixes #505 [2.3.x]
  • Loading branch information
chrisdennis authored Oct 23, 2019
2 parents 25c2cbd + b51abe9 commit 8ab547f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
Expand Down Expand Up @@ -62,6 +64,8 @@ public class CronTriggerImpl extends AbstractTrigger<CronTrigger> implements Cro
*/
private static final long serialVersionUID = -8644953146451592766L;

private static final Logger LOGGER = LoggerFactory.getLogger(CronTriggerImpl.class);

protected static final int YEAR_TO_GIVEUP_SCHEDULING_AT = CronExpression.MAX_YEAR;


Expand Down Expand Up @@ -813,12 +817,22 @@ public ScheduleBuilder<CronTrigger> getScheduleBuilder() {

CronScheduleBuilder cb = CronScheduleBuilder.cronSchedule(getCronExpression())
.inTimeZone(getTimeZone());

switch(getMisfireInstruction()) {
case MISFIRE_INSTRUCTION_DO_NOTHING : cb.withMisfireHandlingInstructionDoNothing();
break;
case MISFIRE_INSTRUCTION_FIRE_ONCE_NOW : cb.withMisfireHandlingInstructionFireAndProceed();
break;

int misfireInstruction = getMisfireInstruction();
switch(misfireInstruction) {
case MISFIRE_INSTRUCTION_SMART_POLICY:
break;
case MISFIRE_INSTRUCTION_DO_NOTHING:
cb.withMisfireHandlingInstructionDoNothing();
break;
case MISFIRE_INSTRUCTION_FIRE_ONCE_NOW:
cb.withMisfireHandlingInstructionFireAndProceed();
break;
case MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY:
cb.withMisfireHandlingInstructionIgnoreMisfires();
break;
default:
LOGGER.warn("Unrecognized misfire policy {}. Derived builder will use the default cron trigger behavior (MISFIRE_INSTRUCTION_FIRE_ONCE_NOW)", misfireInstruction);
}

return cb;
Expand Down
40 changes: 40 additions & 0 deletions quartz-core/src/test/java/org/quartz/CronTriggerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

import org.quartz.impl.triggers.CronTriggerImpl;

import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

/**
* Unit test for CronTrigger.
*/
Expand Down Expand Up @@ -125,6 +129,42 @@ public void testMisfireInstructionValidity() throws ParseException {
}
}

public void testMisfireInstructionInDerivedBuilder() throws ParseException {
for (int policy : asList(
Trigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY,
Trigger.MISFIRE_INSTRUCTION_SMART_POLICY,
CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING,
CronTrigger.MISFIRE_INSTRUCTION_FIRE_ONCE_NOW)
) {
CronTriggerImpl trigger = new CronTriggerImpl();
trigger.setCronExpression("0 0 12 * * ?");
trigger.setMisfireInstruction(policy);
assertThat(trigger.getMisfireInstruction(), is(policy));

CronTrigger copy = trigger.getTriggerBuilder().build();
assertThat(copy.getMisfireInstruction(), is(policy));
}
}

public void testUndefinedMisfireInstructionInDerivedBuilder() throws ParseException {
CronTriggerImpl trigger = new CronTriggerImpl() {
@Override
public int getMisfireInstruction() {
return 12345;
}
};
trigger.setCronExpression("0 0 12 * * ?");
try {
trigger.setMisfireInstruction(12345);
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), is("The misfire instruction code is invalid for this type of trigger."));
}

CronTrigger copy = trigger.getTriggerBuilder().build();
assertThat(copy.getMisfireInstruction(), is(Trigger.MISFIRE_INSTRUCTION_SMART_POLICY));
}

// execute with version number to generate a new version's serialized form
public static void main(String[] args) throws Exception {
new CronTriggerTest().writeJobDataFile("2.0");
Expand Down

0 comments on commit 8ab547f

Please sign in to comment.