KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2007 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.text.ParseException JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.TimeZone JavaDoc;
23
24 import org.quartz.CronTrigger;
25 import org.quartz.JobDetail;
26 import org.quartz.Scheduler;
27
28 import org.springframework.beans.factory.BeanNameAware;
29 import org.springframework.beans.factory.InitializingBean;
30 import org.springframework.core.Constants;
31
32 /**
33  * Convenience subclass of Quartz's {@link org.quartz.CronTrigger}
34  * class, making bean-style usage easier.
35  *
36  * <p>CronTrigger itself is already a JavaBean but lacks sensible defaults.
37  * This class uses the Spring bean name as job name, the Quartz default group
38  * ("DEFAULT") as job group, the current time as start time, and indefinite
39  * repetition, if not specified.
40  *
41  * <p>This class will also register the trigger with the job name and group of
42  * a given {@link org.quartz.JobDetail}. This allows {@link SchedulerFactoryBean}
43  * to automatically register a trigger for the corresponding JobDetail,
44  * instead of registering the JobDetail separately.
45  *
46  * <p><b>NOTE:</b> This convenience subclass does not work with trigger
47  * persistence in Quartz 1.6, due to a change in Quartz's trigger handling.
48  * Use Quartz 1.5 if you rely on trigger persistence based on this class,
49  * or the standard Quartz {@link org.quartz.CronTrigger} class instead.
50  *
51  * @author Juergen Hoeller
52  * @since 18.02.2004
53  * @see #setName
54  * @see #setGroup
55  * @see #setStartTime
56  * @see #setJobName
57  * @see #setJobGroup
58  * @see #setJobDetail
59  * @see SchedulerFactoryBean#setTriggers
60  * @see SchedulerFactoryBean#setJobDetails
61  * @see SimpleTriggerBean
62  */

63 public class CronTriggerBean extends CronTrigger
64     implements JobDetailAwareTrigger, BeanNameAware, InitializingBean {
65
66     /** Constants for the CronTrigger class */
67     private static final Constants constants = new Constants(CronTrigger.class);
68
69
70     private JobDetail jobDetail;
71
72     private String JavaDoc beanName;
73
74
75     /**
76      * Register objects in the JobDataMap via a given Map.
77      * <p>These objects will be available to this Trigger only,
78      * in contrast to objects in the JobDetail's data map.
79      * @param jobDataAsMap Map with String keys and any objects as values
80      * (for example Spring-managed beans)
81      * @see JobDetailBean#setJobDataAsMap
82      */

83     public void setJobDataAsMap(Map JavaDoc jobDataAsMap) {
84         getJobDataMap().putAll(jobDataAsMap);
85     }
86
87     /**
88      * Set the misfire instruction via the name of the corresponding
89      * constant in the {@link org.quartz.CronTrigger} class.
90      * Default is <code>MISFIRE_INSTRUCTION_SMART_POLICY</code>.
91      * @see org.quartz.CronTrigger#MISFIRE_INSTRUCTION_FIRE_ONCE_NOW
92      * @see org.quartz.CronTrigger#MISFIRE_INSTRUCTION_DO_NOTHING
93      * @see org.quartz.Trigger#MISFIRE_INSTRUCTION_SMART_POLICY
94      */

95     public void setMisfireInstructionName(String JavaDoc constantName) {
96         setMisfireInstruction(constants.asNumber(constantName).intValue());
97     }
98
99     /**
100      * Set a list of TriggerListener names for this job, referring to
101      * non-global TriggerListeners registered with the Scheduler.
102      * <p>A TriggerListener name always refers to the name returned
103      * by the TriggerListener implementation.
104      * @see SchedulerFactoryBean#setTriggerListeners
105      * @see org.quartz.TriggerListener#getName
106      */

107     public void setTriggerListenerNames(String JavaDoc[] names) {
108         for (int i = 0; i < names.length; i++) {
109             addTriggerListener(names[i]);
110         }
111     }
112
113     /**
114      * Set the JobDetail that this trigger should be associated with.
115      * <p>This is typically used with a bean reference if the JobDetail
116      * is a Spring-managed bean. Alternatively, the trigger can also
117      * be associated with a job by name and group.
118      * @see #setJobName
119      * @see #setJobGroup
120      */

121     public void setJobDetail(JobDetail jobDetail) {
122         this.jobDetail = jobDetail;
123     }
124
125     public JobDetail getJobDetail() {
126         return this.jobDetail;
127     }
128
129     public void setBeanName(String JavaDoc beanName) {
130         this.beanName = beanName;
131     }
132
133
134     public void afterPropertiesSet() throws ParseException JavaDoc {
135         if (getName() == null) {
136             setName(this.beanName);
137         }
138         if (getGroup() == null) {
139             setGroup(Scheduler.DEFAULT_GROUP);
140         }
141         if (getStartTime() == null) {
142             setStartTime(new Date JavaDoc());
143         }
144         if (getTimeZone() == null) {
145             setTimeZone(TimeZone.getDefault());
146         }
147         if (this.jobDetail != null) {
148             setJobName(this.jobDetail.getName());
149             setJobGroup(this.jobDetail.getGroup());
150         }
151     }
152
153 }
154
Popular Tags