From c31f8bce95fb7f7cc2474e9f022795e07ce9d7a5 Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Sat, 19 Mar 2022 14:06:06 +0100 Subject: [PATCH] Quartz misfire policy - Better error messages 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 --- .../quartz/runtime/QuartzMisfirePolicy.java | 20 ++++++++++++- .../quartz/runtime/QuartzScheduler.java | 28 ++++++++++++++----- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/extensions/quartz/runtime/src/main/java/io/quarkus/quartz/runtime/QuartzMisfirePolicy.java b/extensions/quartz/runtime/src/main/java/io/quarkus/quartz/runtime/QuartzMisfirePolicy.java index 2ac280057a23f..95134079b5478 100644 --- a/extensions/quartz/runtime/src/main/java/io/quarkus/quartz/runtime/QuartzMisfirePolicy.java +++ b/extensions/quartz/runtime/src/main/java/io/quarkus/quartz/runtime/QuartzMisfirePolicy.java @@ -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, @@ -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 validCronValues() { + return EnumSet.of(SMART_POLICY, IGNORE_MISFIRE_POLICY, FIRE_NOW, CRON_TRIGGER_DO_NOTHING); + } + + static EnumSet 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); + } } diff --git a/extensions/quartz/runtime/src/main/java/io/quarkus/quartz/runtime/QuartzScheduler.java b/extensions/quartz/runtime/src/main/java/io/quarkus/quartz/runtime/QuartzScheduler.java index 0f07ff956459b..a9b6c961be09e 100644 --- a/extensions/quartz/runtime/src/main/java/io/quarkus/quartz/runtime/QuartzScheduler.java +++ b/extensions/quartz/runtime/src/main/java/io/quarkus/quartz/runtime/QuartzScheduler.java @@ -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; @@ -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; @@ -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; @@ -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; @@ -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;