From 2b122816afa141c291fd269225366913d9b5d096 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 1 Dec 2011 12:56:47 +0000 Subject: [PATCH] fixed QuartzJobBean to work with Quartz 2.0/2.1 as well (SPR-8889) --- .../scheduling/quartz/QuartzJobBean.java | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/org.springframework.context.support/src/main/java/org/springframework/scheduling/quartz/QuartzJobBean.java b/org.springframework.context.support/src/main/java/org/springframework/scheduling/quartz/QuartzJobBean.java index 0288224140..7c0ad0e005 100644 --- a/org.springframework.context.support/src/main/java/org/springframework/scheduling/quartz/QuartzJobBean.java +++ b/org.springframework.context.support/src/main/java/org/springframework/scheduling/quartz/QuartzJobBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +16,19 @@ package org.springframework.scheduling.quartz; +import java.lang.reflect.Method; +import java.util.Map; + import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; +import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.springframework.beans.BeanWrapper; import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.PropertyAccessorFactory; +import org.springframework.util.ReflectionUtils; /** * Simple implementation of the Quartz Job interface, applying the @@ -67,6 +72,22 @@ import org.springframework.beans.PropertyAccessorFactory; */ public abstract class QuartzJobBean implements Job { + private static final Method getSchedulerMethod; + + private static final Method getMergedJobDataMapMethod; + + + static { + try { + getSchedulerMethod = JobExecutionContext.class.getMethod("getScheduler"); + getMergedJobDataMapMethod = JobExecutionContext.class.getMethod("getMergedJobDataMap"); + } + catch (NoSuchMethodException ex) { + throw new IllegalStateException("Incompatible Quartz API: " + ex); + } + } + + /** * This implementation applies the passed-in job data map as bean property * values, and delegates to executeInternal afterwards. @@ -74,10 +95,14 @@ public abstract class QuartzJobBean implements Job { */ public final void execute(JobExecutionContext context) throws JobExecutionException { try { + // Reflectively adapting to differences between Quartz 1.x and Quartz 2.0... + Scheduler scheduler = (Scheduler) ReflectionUtils.invokeMethod(getSchedulerMethod, context); + Map mergedJobDataMap = (Map) ReflectionUtils.invokeMethod(getMergedJobDataMapMethod, context); + BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this); MutablePropertyValues pvs = new MutablePropertyValues(); - pvs.addPropertyValues(context.getScheduler().getContext()); - pvs.addPropertyValues(context.getMergedJobDataMap()); + pvs.addPropertyValues(scheduler.getContext()); + pvs.addPropertyValues(mergedJobDataMap); bw.setPropertyValues(pvs, true); } catch (SchedulerException ex) {