Skip to content

Commit

Permalink
Quartz misfire policy - Better error messages
Browse files Browse the repository at this point in the history
Two changes here:
- always handle of the cases in the enum so that we get proper warnings
  if a value is not taken into account
- use the actual values that we document as valid values in the error
  messages
  • Loading branch information
gsmet committed Mar 19, 2022
1 parent d517d62 commit c31f8bc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.quarkus.quartz.runtime;

import java.util.EnumSet;
import java.util.Locale;

public enum QuartzMisfirePolicy {
SMART_POLICY,
IGNORE_MISFIRE_POLICY,
Expand All @@ -8,5 +11,20 @@ public enum QuartzMisfirePolicy {
SIMPLE_TRIGGER_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT,
SIMPLE_TRIGGER_RESCHEDULE_NEXT_WITH_REMAINING_COUNT,
SIMPLE_TRIGGER_RESCHEDULE_NEXT_WITH_EXISTING_COUNT,
CRON_TRIGGER_DO_NOTHING
CRON_TRIGGER_DO_NOTHING;

String dashedName() {
return this.name().toLowerCase(Locale.ROOT).replace('_', '-');
}

static EnumSet<QuartzMisfirePolicy> validCronValues() {
return EnumSet.of(SMART_POLICY, IGNORE_MISFIRE_POLICY, FIRE_NOW, CRON_TRIGGER_DO_NOTHING);
}

static EnumSet<QuartzMisfirePolicy> validSimpleValues() {
return EnumSet.of(SMART_POLICY, IGNORE_MISFIRE_POLICY, FIRE_NOW,
SIMPLE_TRIGGER_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT,
SIMPLE_TRIGGER_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT, SIMPLE_TRIGGER_RESCHEDULE_NEXT_WITH_EXISTING_COUNT,
SIMPLE_TRIGGER_RESCHEDULE_NEXT_WITH_REMAINING_COUNT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Objects;
import java.util.OptionalLong;
import java.util.Properties;
import java.util.stream.Collectors;

import javax.annotation.PreDestroy;
import javax.annotation.Priority;
Expand Down Expand Up @@ -178,6 +179,9 @@ public QuartzScheduler(SchedulerContext context, QuartzSupport quartzSupport, Sc
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
if (perJobConfig != null) {
switch (perJobConfig.misfirePolicy) {
case SMART_POLICY:
// this is the default, doing nothing
break;
case IGNORE_MISFIRE_POLICY:
cronScheduleBuilder.withMisfireHandlingInstructionIgnoreMisfires();
break;
Expand All @@ -191,10 +195,13 @@ public QuartzScheduler(SchedulerContext context, QuartzSupport quartzSupport, Sc
case SIMPLE_TRIGGER_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT:
case SIMPLE_TRIGGER_RESCHEDULE_NEXT_WITH_EXISTING_COUNT:
case SIMPLE_TRIGGER_RESCHEDULE_NEXT_WITH_REMAINING_COUNT:
throw new IllegalArgumentException("configured cron job " + identity
+ " with invalid misfire policy " + perJobConfig.misfirePolicy +
"\nvalid options are: IGNORE_MISFIRE_POLICY, FIRE_NOW, CRON_TRIGGER_DO_NOTHING");

throw new IllegalArgumentException("Cron job " + identity
+ " configured with invalid misfire policy "
+ perJobConfig.misfirePolicy.dashedName() +
"\nValid options are: "
+ QuartzMisfirePolicy.validCronValues().stream()
.map(QuartzMisfirePolicy::dashedName)
.collect(Collectors.joining(", ")));
}
}
scheduleBuilder = cronScheduleBuilder;
Expand All @@ -208,6 +215,9 @@ public QuartzScheduler(SchedulerContext context, QuartzSupport quartzSupport, Sc
.repeatForever();
if (perJobConfig != null) {
switch (perJobConfig.misfirePolicy) {
case SMART_POLICY:
// this is the default, doing nothing
break;
case IGNORE_MISFIRE_POLICY:
simpleScheduleBuilder.withMisfireHandlingInstructionIgnoreMisfires();
break;
Expand All @@ -227,9 +237,13 @@ public QuartzScheduler(SchedulerContext context, QuartzSupport quartzSupport, Sc
simpleScheduleBuilder.withMisfireHandlingInstructionNextWithRemainingCount();
break;
case CRON_TRIGGER_DO_NOTHING:
throw new IllegalArgumentException("configured simple job " + identity
+ " with invalid misfire policy CRON_TRIGGER_DO_NOTHING" +
"\nvalid options are: IGNORE_MISFIRE_POLICY, FIRE_NOW, SIMPLE_TRIGGER_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT, SIMPLE_TRIGGER_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT, SIMPLE_TRIGGER_RESCHEDULE_NEXT_WITH_EXISTING_COUNT and SIMPLE_TRIGGER_RESCHEDULE_NEXT_WITH_REMAINING_COUNT");
throw new IllegalArgumentException("Simple job " + identity
+ " configured with invalid misfire policy "
+ perJobConfig.misfirePolicy.dashedName() +
"\nValid options are: "
+ QuartzMisfirePolicy.validSimpleValues().stream()
.map(QuartzMisfirePolicy::dashedName)
.collect(Collectors.joining(", ")));
}
}
scheduleBuilder = simpleScheduleBuilder;
Expand Down

0 comments on commit c31f8bc

Please sign in to comment.