KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > scheduling > quartz > MethodInvokingJobDetailFactoryBean


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.scheduling.quartz;
18
19 import java.lang.reflect.Constructor JavaDoc;
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.quartz.JobDetail;
25 import org.quartz.JobExecutionContext;
26 import org.quartz.JobExecutionException;
27 import org.quartz.Scheduler;
28 import org.quartz.StatefulJob;
29
30 import org.springframework.beans.BeanUtils;
31 import org.springframework.beans.factory.BeanClassLoaderAware;
32 import org.springframework.beans.factory.BeanNameAware;
33 import org.springframework.beans.factory.FactoryBean;
34 import org.springframework.beans.factory.InitializingBean;
35 import org.springframework.beans.support.ArgumentConvertingMethodInvoker;
36 import org.springframework.util.ClassUtils;
37 import org.springframework.util.MethodInvoker;
38
39 /**
40  * FactoryBean that exposes a JobDetail object that delegates job execution
41  * to a specified (static or non-static) method. Avoids the need to implement
42  * a one-line Quartz Job that just invokes an existing service method.
43  *
44  * <p>Derived from MethodInvoker to share common properties and behavior
45  * with MethodInvokingFactoryBean.
46  *
47  * <p>Supports both concurrently running jobs and non-currently running
48  * ones through the "concurrent" property. Jobs created by this
49  * MethodInvokingJobDetailFactoryBean are by default volatile and durable
50  * (according to Quartz terminology).
51  *
52  * <p><b>NOTE: JobDetails created via this FactoryBean are <i>not</i>
53  * serializable and thus not suitable for persistent job stores.</b>
54  * You need to implement your own Quartz Job as a thin wrapper for each case
55  * where you want a persistent job to delegate to a specific service method.
56  *
57  * @author Juergen Hoeller
58  * @author Alef Arendsen
59  * @since 18.02.2004
60  * @see #setTargetObject
61  * @see #setTargetMethod
62  * @see #setConcurrent
63  * @see org.springframework.beans.factory.config.MethodInvokingFactoryBean
64  */

65 public class MethodInvokingJobDetailFactoryBean extends ArgumentConvertingMethodInvoker
66     implements FactoryBean, BeanNameAware, BeanClassLoaderAware, InitializingBean {
67
68     /**
69      * Determine whether the old Quartz 1.5 JobExecutionException constructor
70      * with an Exception argument is present. If yes, we'll use it;
71      * else we'll use Quartz 1.6's constructor with a Throwable argument.
72      */

73     private static final Constructor JavaDoc oldJobExecutionExceptionConstructor =
74             ClassUtils.getConstructorIfAvailable(JobExecutionException.class,
75                     new Class JavaDoc[] {String JavaDoc.class, Exception JavaDoc.class, boolean.class});
76
77
78     private String JavaDoc name;
79
80     private String JavaDoc group = Scheduler.DEFAULT_GROUP;
81
82     private boolean concurrent = true;
83
84     private String JavaDoc[] jobListenerNames;
85
86     private String JavaDoc beanName;
87
88     private ClassLoader JavaDoc beanClassLoader = ClassUtils.getDefaultClassLoader();
89
90     private JobDetail jobDetail;
91
92
93     /**
94      * Set the name of the job.
95      * Default is the bean name of this FactoryBean.
96      * @see org.quartz.JobDetail#setName
97      */

98     public void setName(String JavaDoc name) {
99         this.name = name;
100     }
101
102     /**
103      * Set the group of the job.
104      * Default is the default group of the Scheduler.
105      * @see org.quartz.JobDetail#setGroup
106      * @see org.quartz.Scheduler#DEFAULT_GROUP
107      */

108     public void setGroup(String JavaDoc group) {
109         this.group = group;
110     }
111     
112     /**
113      * Specify whether or not multiple jobs should be run in a concurrent
114      * fashion. The behavior when one does not want concurrent jobs to be
115      * executed is realized through adding the {@link StatefulJob} interface.
116      * More information on stateful versus stateless jobs can be found
117      * <a HREF="http://www.opensymphony.com/quartz/tutorial.html#jobsMore">here</a>.
118      * <p>The default setting is to run jobs concurrently.
119      * @param concurrent whether one wants to execute multiple jobs created
120      * by this bean concurrently
121      */

122     public void setConcurrent(boolean concurrent) {
123         this.concurrent = concurrent;
124     }
125
126     /**
127      * Set a list of JobListener names for this job, referring to
128      * non-global JobListeners registered with the Scheduler.
129      * <p>A JobListener name always refers to the name returned
130      * by the JobListener implementation.
131      * @see SchedulerFactoryBean#setJobListeners
132      * @see org.quartz.JobListener#getName
133      */

134     public void setJobListenerNames(String JavaDoc[] names) {
135         this.jobListenerNames = names;
136     }
137
138     public void setBeanName(String JavaDoc beanName) {
139         this.beanName = beanName;
140     }
141
142     public void setBeanClassLoader(ClassLoader JavaDoc classLoader) {
143         this.beanClassLoader = classLoader;
144     }
145
146     protected Class JavaDoc resolveClassName(String JavaDoc className) throws ClassNotFoundException JavaDoc {
147         return ClassUtils.forName(className, this.beanClassLoader);
148     }
149
150
151     public void afterPropertiesSet() throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc {
152         prepare();
153
154         // Use specific name if given, else fall back to bean name.
155
String JavaDoc name = (this.name != null ? this.name : this.beanName);
156
157         // Consider the concurrent flag to choose between stateful and stateless job.
158
Class JavaDoc jobClass = (this.concurrent ? (Class JavaDoc) MethodInvokingJob.class : StatefulMethodInvokingJob.class);
159
160         // Build JobDetail instance.
161
this.jobDetail = new JobDetail(name, this.group, jobClass);
162         this.jobDetail.getJobDataMap().put("methodInvoker", this);
163         this.jobDetail.setVolatility(true);
164         this.jobDetail.setDurability(true);
165
166         // Register job listener names.
167
if (this.jobListenerNames != null) {
168             for (int i = 0; i < this.jobListenerNames.length; i++) {
169                 this.jobDetail.addJobListener(this.jobListenerNames[i]);
170             }
171         }
172
173         postProcessJobDetail(this.jobDetail);
174     }
175
176     /**
177      * Callback for post-processing the JobDetail to be exposed by this FactoryBean.
178      * <p>The default implementation is empty. Can be overridden in subclasses.
179      * @param jobDetail the JobDetail prepared by this FactoryBean
180      */

181     protected void postProcessJobDetail(JobDetail jobDetail) {
182     }
183
184
185     public Object JavaDoc getObject() {
186         return this.jobDetail;
187     }
188
189     public Class JavaDoc getObjectType() {
190         return JobDetail.class;
191     }
192
193     public boolean isSingleton() {
194         return true;
195     }
196
197
198     /**
199      * Quartz Job implementation that invokes a specified method.
200      * Automatically applied by MethodInvokingJobDetailFactoryBean.
201      */

202     public static class MethodInvokingJob extends QuartzJobBean {
203
204         protected static final Log logger = LogFactory.getLog(MethodInvokingJob.class);
205
206         private MethodInvoker methodInvoker;
207
208         private String JavaDoc errorMessage;
209
210         /**
211          * Set the MethodInvoker to use.
212          */

213         public void setMethodInvoker(MethodInvoker methodInvoker) {
214             this.methodInvoker = methodInvoker;
215             this.errorMessage = "Could not invoke method '" + this.methodInvoker.getTargetMethod() +
216                     "' on target object [" + this.methodInvoker.getTargetObject() + "]";
217         }
218
219         /**
220          * Invoke the method via the MethodInvoker.
221          */

222         protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
223             try {
224                 this.methodInvoker.invoke();
225             }
226             catch (InvocationTargetException JavaDoc ex) {
227                 logger.warn(this.errorMessage, ex.getTargetException());
228                 if (ex.getTargetException() instanceof JobExecutionException) {
229                     throw (JobExecutionException) ex.getTargetException();
230                 }
231                 if (oldJobExecutionExceptionConstructor != null) {
232                     Exception JavaDoc jobEx = (ex.getTargetException() instanceof Exception JavaDoc) ?
233                             (Exception JavaDoc) ex.getTargetException() : ex;
234                     throw (JobExecutionException) BeanUtils.instantiateClass(
235                             oldJobExecutionExceptionConstructor, new Object JavaDoc[] {this.errorMessage, jobEx, Boolean.FALSE});
236                 }
237                 else {
238                     throw new JobExecutionException(this.errorMessage, ex.getTargetException());
239                 }
240             }
241             catch (Exception JavaDoc ex) {
242                 logger.warn(this.errorMessage, ex);
243                 if (oldJobExecutionExceptionConstructor != null) {
244                     throw (JobExecutionException) BeanUtils.instantiateClass(
245                             oldJobExecutionExceptionConstructor, new Object JavaDoc[] {this.errorMessage, ex, Boolean.FALSE});
246                 }
247                 else {
248                     throw new JobExecutionException(this.errorMessage, ex);
249                 }
250             }
251         }
252     }
253
254
255     /**
256      * Extension of the MethodInvokingJob, implementing the StatefulJob interface.
257      * Quartz checks whether or not jobs are stateful and if so,
258      * won't let jobs interfere with each other.
259      */

260     public static class StatefulMethodInvokingJob extends MethodInvokingJob implements StatefulJob {
261
262         // No implementation, just an addition of the tag interface StatefulJob
263
// in order to allow stateful method invoking jobs.
264
}
265
266 }
267
Popular Tags