From c4aa8948da8bc78aa7560a7596b1898961aea0cc Mon Sep 17 00:00:00 2001 From: Manyanda Chitimbo Date: Thu, 5 May 2022 23:29:48 +0200 Subject: [PATCH] fix(quartz): avoid NPE when scheduled method identity has been changed/renamed Fixes https://github.com/quarkusio/quarkus/issues/24931 --- .../io/quarkus/quartz/runtime/QuartzScheduler.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) 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 ec68f58ae0025..22e65c5c55b3f 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 @@ -277,8 +277,8 @@ public QuartzScheduler(SchedulerContext context, QuartzSupport quartzSupport, Sc org.quartz.Trigger trigger = triggerBuilder.build(); org.quartz.Trigger oldTrigger = scheduler.getTrigger(trigger.getKey()); if (oldTrigger != null) { - scheduler.rescheduleJob(trigger.getKey(), - triggerBuilder.startAt(oldTrigger.getNextFireTime()).build()); + trigger = triggerBuilder.startAt(oldTrigger.getNextFireTime()).build(); + scheduler.rescheduleJob(trigger.getKey(), trigger); LOGGER.debugf("Rescheduled business method %s with config %s", method.getMethodDescription(), scheduled); } else if (!scheduler.checkExists(jobDetail.getKey())) { @@ -292,7 +292,8 @@ public QuartzScheduler(SchedulerContext context, QuartzSupport quartzSupport, Sc oldTrigger = scheduler.getTrigger(new TriggerKey(identity + "_trigger", Scheduler.class.getName())); if (oldTrigger != null) { scheduler.deleteJob(jobDetail.getKey()); - scheduler.scheduleJob(jobDetail, triggerBuilder.startAt(oldTrigger.getNextFireTime()).build()); + trigger = triggerBuilder.startAt(oldTrigger.getNextFireTime()).build(); + scheduler.scheduleJob(jobDetail, trigger); LOGGER.debugf( "Rescheduled business method %s with config %s due to Trigger '%s' record being renamed after removal of '_trigger' suffix", method.getMethodDescription(), @@ -558,7 +559,7 @@ static class InvokerJob implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { - if (trigger.invoker != null) { // could be null from previous runs + if (trigger != null && trigger.invoker != null) { // could be null from previous runs if (trigger.invoker.isBlocking()) { try { trigger.invoker.invoke(new QuartzScheduledExecution(trigger, jobExecutionContext)); @@ -579,6 +580,11 @@ public void handle(Void event) { } }); } + } else { + String jobName = jobExecutionContext.getJobDetail().getKey().getName(); + LOGGER.warnf("Unable to find corresponding Quartz trigger for job %s. " + + "Update your Quartz table by removing all phantom jobs or make sure that there is a " + + "Scheduled method with the identity matching the job's name", jobName); } } }