org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean Maven / Gradle / Ivy
/*
* Copyright 2002-2007 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.scheduling.quartz;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.StatefulJob;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.support.ArgumentConvertingMethodInvoker;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.MethodInvoker;
/**
* {@link org.springframework.beans.factory.FactoryBean} that exposes a
* {@link org.quartz.JobDetail} object which delegates job execution to a
* specified (static or non-static) method. Avoids the need for implementing
* a one-line Quartz Job that just invokes an existing service method on a
* Spring-managed target bean.
*
* Inherits common configuration properties from the {@link MethodInvoker}
* base class, such as {@link #setTargetObject "targetObject"} and
* {@link #setTargetMethod "targetMethod"}, adding support for lookup of the target
* bean by name through the {@link #setTargetBeanName "targetBeanName"} property
* (as alternative to specifying a "targetObject" directly, allowing for
* non-singleton target objects).
*
*
Supports both concurrently running jobs and non-currently running
* jobs through the "concurrent" property. Jobs created by this
* MethodInvokingJobDetailFactoryBean are by default volatile and durable
* (according to Quartz terminology).
*
*
NOTE: JobDetails created via this FactoryBean are not
* serializable and thus not suitable for persistent job stores.
* You need to implement your own Quartz Job as a thin wrapper for each case
* where you want a persistent job to delegate to a specific service method.
*
* @author Juergen Hoeller
* @author Alef Arendsen
* @since 18.02.2004
* @see #setTargetBeanName
* @see #setTargetObject
* @see #setTargetMethod
* @see #setConcurrent
*/
public class MethodInvokingJobDetailFactoryBean extends ArgumentConvertingMethodInvoker
implements FactoryBean, BeanNameAware, BeanClassLoaderAware, BeanFactoryAware, InitializingBean {
/**
* Determine whether the old Quartz 1.5 JobExecutionException constructor
* with an Exception argument is present. If yes, we'll use it;
* else we'll use Quartz 1.6's constructor with a Throwable argument.
*/
private static final Constructor oldJobExecutionExceptionConstructor =
ClassUtils.getConstructorIfAvailable(JobExecutionException.class,
new Class[] {String.class, Exception.class, boolean.class});
private String name;
private String group = Scheduler.DEFAULT_GROUP;
private boolean concurrent = true;
private String targetBeanName;
private String[] jobListenerNames;
private String beanName;
private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
private BeanFactory beanFactory;
private JobDetail jobDetail;
/**
* Set the name of the job.
*
Default is the bean name of this FactoryBean.
* @see org.quartz.JobDetail#setName
*/
public void setName(String name) {
this.name = name;
}
/**
* Set the group of the job.
*
Default is the default group of the Scheduler.
* @see org.quartz.JobDetail#setGroup
* @see org.quartz.Scheduler#DEFAULT_GROUP
*/
public void setGroup(String group) {
this.group = group;
}
/**
* Specify whether or not multiple jobs should be run in a concurrent
* fashion. The behavior when one does not want concurrent jobs to be
* executed is realized through adding the {@link StatefulJob} interface.
* More information on stateful versus stateless jobs can be found
* here.
*
The default setting is to run jobs concurrently.
*/
public void setConcurrent(boolean concurrent) {
this.concurrent = concurrent;
}
/**
* Set the name of the target bean in the Spring BeanFactory.
*
This is an alternative to specifying {@link #setTargetObject "targetObject"},
* allowing for non-singleton beans to be invoked. Note that specified
* "targetObject" and {@link #setTargetClass "targetClass"} values will
* override the corresponding effect of this "targetBeanName" setting
* (i.e. statically pre-define the bean type or even the bean object).
*/
public void setTargetBeanName(String targetBeanName) {
this.targetBeanName = targetBeanName;
}
/**
* Set a list of JobListener names for this job, referring to
* non-global JobListeners registered with the Scheduler.
*
A JobListener name always refers to the name returned
* by the JobListener implementation.
* @see SchedulerFactoryBean#setJobListeners
* @see org.quartz.JobListener#getName
*/
public void setJobListenerNames(String[] names) {
this.jobListenerNames = names;
}
public void setBeanName(String beanName) {
this.beanName = beanName;
}
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
}
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
protected Class resolveClassName(String className) throws ClassNotFoundException {
return ClassUtils.forName(className, this.beanClassLoader);
}
public void afterPropertiesSet() throws ClassNotFoundException, NoSuchMethodException {
prepare();
// Use specific name if given, else fall back to bean name.
String name = (this.name != null ? this.name : this.beanName);
// Consider the concurrent flag to choose between stateful and stateless job.
Class jobClass = (this.concurrent ? (Class) MethodInvokingJob.class : StatefulMethodInvokingJob.class);
// Build JobDetail instance.
this.jobDetail = new JobDetail(name, this.group, jobClass);
this.jobDetail.getJobDataMap().put("methodInvoker", this);
this.jobDetail.setVolatility(true);
this.jobDetail.setDurability(true);
// Register job listener names.
if (this.jobListenerNames != null) {
for (int i = 0; i < this.jobListenerNames.length; i++) {
this.jobDetail.addJobListener(this.jobListenerNames[i]);
}
}
postProcessJobDetail(this.jobDetail);
}
/**
* Callback for post-processing the JobDetail to be exposed by this FactoryBean.
*
The default implementation is empty. Can be overridden in subclasses.
* @param jobDetail the JobDetail prepared by this FactoryBean
*/
protected void postProcessJobDetail(JobDetail jobDetail) {
}
/**
* Overridden to support the {@link #setTargetBeanName "targetBeanName"} feature.
*/
public Class getTargetClass() {
Class targetClass = super.getTargetClass();
if (targetClass == null && this.targetBeanName != null) {
Assert.state(this.beanFactory != null, "BeanFactory must be set when using 'targetBeanName'");
targetClass = this.beanFactory.getType(this.targetBeanName);
}
return targetClass;
}
/**
* Overridden to support the {@link #setTargetBeanName "targetBeanName"} feature.
*/
public Object getTargetObject() {
Object targetObject = super.getTargetObject();
if (targetObject == null && this.targetBeanName != null) {
Assert.state(this.beanFactory != null, "BeanFactory must be set when using 'targetBeanName'");
targetObject = this.beanFactory.getBean(this.targetBeanName);
}
return targetObject;
}
public Object getObject() {
return this.jobDetail;
}
public Class getObjectType() {
return JobDetail.class;
}
public boolean isSingleton() {
return true;
}
/**
* Quartz Job implementation that invokes a specified method.
* Automatically applied by MethodInvokingJobDetailFactoryBean.
*/
public static class MethodInvokingJob extends QuartzJobBean {
protected static final Log logger = LogFactory.getLog(MethodInvokingJob.class);
private MethodInvoker methodInvoker;
private String errorMessage;
/**
* Set the MethodInvoker to use.
*/
public void setMethodInvoker(MethodInvoker methodInvoker) {
this.methodInvoker = methodInvoker;
this.errorMessage = "Could not invoke method '" + this.methodInvoker.getTargetMethod() +
"' on target object [" + this.methodInvoker.getTargetObject() + "]";
}
/**
* Invoke the method via the MethodInvoker.
*/
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
try {
this.methodInvoker.invoke();
}
catch (InvocationTargetException ex) {
logger.warn(this.errorMessage, ex.getTargetException());
if (ex.getTargetException() instanceof JobExecutionException) {
throw (JobExecutionException) ex.getTargetException();
}
if (oldJobExecutionExceptionConstructor != null) {
Exception jobEx = (ex.getTargetException() instanceof Exception) ?
(Exception) ex.getTargetException() : ex;
throw (JobExecutionException) BeanUtils.instantiateClass(
oldJobExecutionExceptionConstructor, new Object[] {this.errorMessage, jobEx, Boolean.FALSE});
}
else {
throw new JobExecutionException(this.errorMessage, ex.getTargetException());
}
}
catch (Exception ex) {
logger.warn(this.errorMessage, ex);
if (oldJobExecutionExceptionConstructor != null) {
throw (JobExecutionException) BeanUtils.instantiateClass(
oldJobExecutionExceptionConstructor, new Object[] {this.errorMessage, ex, Boolean.FALSE});
}
else {
throw new JobExecutionException(this.errorMessage, ex);
}
}
}
}
/**
* Extension of the MethodInvokingJob, implementing the StatefulJob interface.
* Quartz checks whether or not jobs are stateful and if so,
* won't let jobs interfere with each other.
*/
public static class StatefulMethodInvokingJob extends MethodInvokingJob implements StatefulJob {
// No implementation, just an addition of the tag interface StatefulJob
// in order to allow stateful method invoking jobs.
}
}