KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.quartz.Job;
20 import org.quartz.JobExecutionContext;
21 import org.quartz.JobExecutionException;
22 import org.quartz.SchedulerException;
23
24 import org.springframework.beans.BeanWrapper;
25 import org.springframework.beans.BeanWrapperImpl;
26 import org.springframework.beans.MutablePropertyValues;
27
28 /**
29  * Simple implementation of the Quartz Job interface, applying the
30  * passed-in JobDataMap and also the SchedulerContext as bean property
31  * values. This is appropriate because a new Job instance will be created
32  * for each execution. JobDataMap entries will override SchedulerContext
33  * entries with the same keys.
34  *
35  * <p>For example, let's assume that the JobDataMap contains a key
36  * "myParam" with value "5": The Job implementation can then expose
37  * a bean property "myParam" of type int to receive such a value,
38  * i.e. a method "setMyParam(int)". This will also work for complex
39  * types like business objects etc.
40  *
41  * <p>Note: The QuartzJobBean class itself only implements the standard
42  * Quartz {@link org.quartz.Job} interface. Let your subclass explicitly
43  * implement the Quartz {@link org.quartz.StatefulJob} interface to
44  * mark your concrete job bean as stateful.
45  *
46  * <p>This version of QuartzJobBean requires Quartz 1.5 or higher,
47  * due to the support for trigger-specific job data.
48  *
49  * <p><b>Note that as of Spring 2.0 and Quartz 1.5, the preferred way
50  * to apply dependency injection to Job instances is via a JobFactory:</b>
51  * that is, to specify {@link SpringBeanJobFactory} as Quartz JobFactory
52  * (typically via
53  * {@link SchedulerFactoryBean#setJobFactory} SchedulerFactoryBean's "jobFactory" property}).
54  * This allows to implement dependency-injected Quartz Jobs without
55  * a dependency on Spring base classes.
56  *
57  * @author Juergen Hoeller
58  * @since 18.02.2004
59  * @see org.quartz.JobExecutionContext#getMergedJobDataMap()
60  * @see org.quartz.Scheduler#getContext()
61  * @see JobDetailBean#setJobDataAsMap
62  * @see SimpleTriggerBean#setJobDataAsMap
63  * @see CronTriggerBean#setJobDataAsMap
64  * @see SchedulerFactoryBean#setSchedulerContextAsMap
65  * @see SpringBeanJobFactory
66  * @see SchedulerFactoryBean#setJobFactory
67  */

68 public abstract class QuartzJobBean implements Job {
69
70     /**
71      * This implementation applies the passed-in job data map as bean property
72      * values, and delegates to <code>executeInternal</code> afterwards.
73      * @see #executeInternal
74      */

75     public final void execute(JobExecutionContext context) throws JobExecutionException {
76         try {
77             BeanWrapper bw = new BeanWrapperImpl(this);
78             MutablePropertyValues pvs = new MutablePropertyValues();
79             pvs.addPropertyValues(context.getScheduler().getContext());
80             pvs.addPropertyValues(context.getMergedJobDataMap());
81             bw.setPropertyValues(pvs, true);
82         }
83         catch (SchedulerException ex) {
84             throw new JobExecutionException(ex);
85         }
86         executeInternal(context);
87     }
88
89     /**
90      * Execute the actual job. The job data map will already have been
91      * applied as bean property values by execute. The contract is
92      * exactly the same as for the standard Quartz execute method.
93      * @see #execute
94      */

95     protected abstract void executeInternal(JobExecutionContext context) throws JobExecutionException;
96
97 }
98
Popular Tags