Skip to content

Commit fe4b57c

Browse files
committed
Fixed 'globalJobListeners'/'globalTriggerListeners' to work with Quartz 2.0 & 2.1 as well
Issue: SPR-11362 (cherry picked from commit c719c70)
1 parent 2a3b29f commit fe4b57c

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerAccessor.java

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,6 +54,7 @@
5454
* <p>Compatible with Quartz 1.5+ as well as Quartz 2.0-2.2, as of Spring 3.2.
5555
*
5656
* @author Juergen Hoeller
57+
* @author Stephane Nicoll
5758
* @since 2.5.6
5859
*/
5960
public abstract class SchedulerAccessor implements ResourceLoaderAware {
@@ -63,6 +64,7 @@ public abstract class SchedulerAccessor implements ResourceLoaderAware {
6364
private static Class<?> triggerKeyClass;
6465

6566
static {
67+
// Quartz 2.0 job/trigger key available?
6668
try {
6769
jobKeyClass = Class.forName("org.quartz.JobKey");
6870
triggerKeyClass = Class.forName("org.quartz.TriggerKey");
@@ -258,7 +260,7 @@ protected void registerJobsAndTriggers() throws SchedulerException {
258260
clh.initialize();
259261
try {
260262
// Quartz 1.8 or higher?
261-
Class dataProcessorClass = getClass().getClassLoader().loadClass("org.quartz.xml.XMLSchedulingDataProcessor");
263+
Class<?> dataProcessorClass = getClass().getClassLoader().loadClass("org.quartz.xml.XMLSchedulingDataProcessor");
262264
logger.debug("Using Quartz 1.8 XMLSchedulingDataProcessor");
263265
Object dataProcessor = dataProcessorClass.getConstructor(ClassLoadHelper.class).newInstance(clh);
264266
Method processFileAndScheduleJobs = dataProcessorClass.getMethod("processFileAndScheduleJobs", String.class, Scheduler.class);
@@ -268,7 +270,7 @@ protected void registerJobsAndTriggers() throws SchedulerException {
268270
}
269271
catch (ClassNotFoundException ex) {
270272
// Quartz 1.6
271-
Class dataProcessorClass = getClass().getClassLoader().loadClass("org.quartz.xml.JobSchedulingDataProcessor");
273+
Class<?> dataProcessorClass = getClass().getClassLoader().loadClass("org.quartz.xml.JobSchedulingDataProcessor");
272274
logger.debug("Using Quartz 1.6 JobSchedulingDataProcessor");
273275
Object dataProcessor = dataProcessorClass.getConstructor(ClassLoadHelper.class, boolean.class, boolean.class).newInstance(clh, true, true);
274276
Method processFileAndScheduleJobs = dataProcessorClass.getMethod("processFileAndScheduleJobs", String.class, Scheduler.class, boolean.class);
@@ -396,7 +398,8 @@ private JobDetail findJobDetail(Trigger trigger) {
396398
}
397399
else {
398400
try {
399-
Map<?, ?> jobDataMap = (Map<?, ?>) ReflectionUtils.invokeMethod(Trigger.class.getMethod("getJobDataMap"), trigger);
401+
Map<?, ?> jobDataMap =
402+
(Map<?, ?>) ReflectionUtils.invokeMethod(Trigger.class.getMethod("getJobDataMap"), trigger);
400403
return (JobDetail) jobDataMap.remove(JobDetailAwareTrigger.JOB_DETAIL_KEY);
401404
}
402405
catch (NoSuchMethodException ex) {
@@ -473,19 +476,33 @@ protected void registerListeners() throws SchedulerException {
473476
target = getScheduler();
474477
quartz2 = false;
475478
}
479+
Class<?> targetClass = target.getClass();
476480

477481
try {
478482
if (this.schedulerListeners != null) {
479-
Method addSchedulerListener = target.getClass().getMethod("addSchedulerListener", SchedulerListener.class);
483+
Method addSchedulerListener = targetClass.getMethod("addSchedulerListener", SchedulerListener.class);
480484
for (SchedulerListener listener : this.schedulerListeners) {
481485
ReflectionUtils.invokeMethod(addSchedulerListener, target, listener);
482486
}
483487
}
484488
if (this.globalJobListeners != null) {
485-
Method addJobListener = target.getClass().getMethod(
486-
(quartz2 ? "addJobListener" : "addGlobalJobListener"), JobListener.class);
489+
Method addJobListener;
490+
if (quartz2) {
491+
// addJobListener(JobListener) only introduced as late as Quartz 2.2, so we need
492+
// to fall back to the Quartz 2.0/2.1 compatible variant with an empty matchers List
493+
addJobListener = targetClass.getMethod("addJobListener", JobListener.class, List.class);
494+
}
495+
else {
496+
addJobListener = targetClass.getMethod("addGlobalJobListener", JobListener.class);
497+
}
487498
for (JobListener listener : this.globalJobListeners) {
488-
ReflectionUtils.invokeMethod(addJobListener, target, listener);
499+
if (quartz2) {
500+
List<?> emptyMatchers = new LinkedList<Object>();
501+
ReflectionUtils.invokeMethod(addJobListener, target, listener, emptyMatchers);
502+
}
503+
else {
504+
ReflectionUtils.invokeMethod(addJobListener, target, listener);
505+
}
489506
}
490507
}
491508
if (this.jobListeners != null) {
@@ -498,10 +515,23 @@ protected void registerListeners() throws SchedulerException {
498515
}
499516
}
500517
if (this.globalTriggerListeners != null) {
501-
Method addTriggerListener = target.getClass().getMethod(
502-
(quartz2 ? "addTriggerListener" : "addGlobalTriggerListener"), TriggerListener.class);
518+
Method addTriggerListener;
519+
if (quartz2) {
520+
// addTriggerListener(TriggerListener) only introduced as late as Quartz 2.2, so we need
521+
// to fall back to the Quartz 2.0/2.1 compatible variant with an empty matchers List
522+
addTriggerListener = targetClass.getMethod("addTriggerListener", TriggerListener.class, List.class);
523+
}
524+
else {
525+
addTriggerListener = targetClass.getMethod("addGlobalTriggerListener", TriggerListener.class);
526+
}
503527
for (TriggerListener listener : this.globalTriggerListeners) {
504-
ReflectionUtils.invokeMethod(addTriggerListener, target, listener);
528+
if (quartz2) {
529+
List<?> emptyMatchers = new LinkedList<Object>();
530+
ReflectionUtils.invokeMethod(addTriggerListener, target, listener, emptyMatchers);
531+
}
532+
else {
533+
ReflectionUtils.invokeMethod(addTriggerListener, target, listener);
534+
}
505535
}
506536
}
507537
if (this.triggerListeners != null) {

0 commit comments

Comments
 (0)