KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > quartz > CronSchedule


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.quartz;
5
6 import java.io.IOException JavaDoc;
7 import java.io.ObjectInputStream JavaDoc;
8 import java.io.ObjectOutputStream JavaDoc;
9 import java.io.Serializable JavaDoc;
10 import java.text.ParseException JavaDoc;
11 import java.util.Date JavaDoc;
12
13 import org.oddjob.Iconic;
14 import org.oddjob.Resetable;
15 import org.oddjob.Stateful;
16 import org.oddjob.arooa.registry.ComponentRegistry;
17 import org.oddjob.framework.BasePrimary;
18 import org.oddjob.images.IconEvent;
19 import org.oddjob.images.IconListener;
20 import org.oddjob.state.JobStateEvent;
21 import org.oddjob.state.JobStateListener;
22 import org.oddjob.util.OddjobConfigException;
23 import org.oddjob.util.OddjobConstantException;
24 import org.quartz.CronTrigger;
25 import org.quartz.JobDataMap;
26 import org.quartz.JobDetail;
27 import org.quartz.Scheduler;
28 import org.quartz.SchedulerException;
29
30 /**
31  * @oddjob.description The schedule component the
32  * {@link org.oddjob.quartz.QuartzSchedulerJob} creates to handle a
33  * {@link org.oddjob.scheduling.CronScheduleInstruction} schedule instruction.
34  *
35  * @author Rob Gordon.
36  */

37 public class CronSchedule extends BasePrimary
38 implements Serializable JavaDoc, Stateful, QuartzSchedule {
39     private static final long serialVersionUID = 20051122;
40
41     /** The quartz schedule group. */
42     private static final String JavaDoc GROUP_NAME = Scheduler.DEFAULT_GROUP;
43     
44     /**
45      * The job token.
46      */

47     private JobToken jobToken;
48         
49     private transient ComponentRegistry componentRegistry;
50     
51     /**
52      * @oddjob.property
53      * @oddjob.description The cron expression. Ignored if changed.
54      * @oddjob.required Set automatically.
55      */

56     private String JavaDoc expression;
57     
58     /** The schedule id. */
59     private String JavaDoc id;
60     
61     private transient JobStateListener jobListener;
62     
63     private transient IconListener iconListener;
64     
65     public CronSchedule() {
66         completeConstruction();
67     }
68     
69     private void completeConstruction() {
70         jobListener = new JobStateListener() {
71             public void jobStateChange(JobStateEvent event) {
72                 stateHandler.fireEvent(new JobStateEvent(
73                         CronSchedule.this, event.getJobState(),
74                         event.getTime(), event.getException()));
75             }
76         };
77         
78         iconListener = new IconListener() {
79             public void iconEvent(IconEvent e) {
80                 iconHelper.changeIcon(e.getIconId());
81             }
82         };
83     }
84     
85     /* (non-Javadoc)
86      * @see org.oddjob.quartz.QuartzSchedule#scheduleWith(org.oddjob.quartz.SimpleQuartzScheduler)
87      */

88     public void scheduleWith(Scheduler scheduler) throws SchedulerException {
89         if (jobToken == null) {
90             throw new NullPointerException JavaDoc("Job to schedule must be specified.");
91         }
92                 
93         OddjobData ojd = new OddjobData();
94         ojd.setJob(new Execute());
95             
96         JobDataMap jobDataMap = new JobDataMap();
97             
98         jobDataMap.put(OddjobData.ODDJOB_DATA, ojd);
99             
100         JobDetail jobDetail = new JobDetail(toString(),
101                     Scheduler.DEFAULT_GROUP,
102                     RunnableQuartzJob.class);
103             
104         jobDetail.setJobDataMap(jobDataMap);
105             
106         CronTrigger trigger = null;
107         try {
108             trigger = new CronTrigger(triggerName(),
109                           GROUP_NAME,
110                           expression);
111         }
112         catch (ParseException JavaDoc e) {
113             throw new SchedulerException("Failed creating trigger for expression ["
114                     + expression + "]", e);
115         }
116         scheduler.scheduleJob(jobDetail, trigger);
117
118     }
119     
120     /*
121      * (non-Javadoc)
122      * @see org.oddjob.quartz.QuartzSchedule#unschedule(org.quartz.Scheduler)
123      */

124     public void unscheduleFrom(Scheduler scheduler) throws SchedulerException {
125         scheduler.unscheduleJob(triggerName(), GROUP_NAME);
126     }
127
128     /**
129      * Set the id for this schedule. This will be set when the schedule is
130      * created and can't be changed.
131      *
132      * @param id The id.
133      */

134     public void setId(String JavaDoc id) {
135         if (this.id != null) {
136             throw new OddjobConfigException("Id can't be changed.");
137         }
138         this.id = id;
139     }
140
141     /**
142      * Helper funtion to build a trigger name.
143      *
144      * @return A quartz trigger name.
145      */

146     private String JavaDoc triggerName() {
147         return id + "-Trigger";
148     }
149
150     public void setComponentRegistry(ComponentRegistry componentRegistry) {
151         this.componentRegistry = componentRegistry;
152     }
153     
154     public void setJob(Runnable JavaDoc job) {
155         if (job == null) {
156             throw new OddjobConfigException("No job to schedule specified!");
157         }
158         if (jobToken != null) {
159             throw new OddjobConstantException("[" + this
160                     + "] job can't be changed.");
161         }
162
163         jobToken = JobToken.create(componentRegistry, job);
164     }
165     
166     
167     public String JavaDoc toString() {
168         if (getName() == null) {
169             return "Cron [" + jobToken + "] expression [" + expression + "]";
170         }
171         return getName();
172     }
173     
174     /**
175      * @return Returns the expression.
176      */

177     public String JavaDoc getExpression() {
178         return expression;
179     }
180     /**
181      * @param expression The expression to set.
182      */

183     public void setExpression(String JavaDoc expression) {
184         this.expression = expression;
185     }
186
187     private void writeObject(ObjectOutputStream JavaDoc os)
188     throws IOException JavaDoc {
189         os.defaultWriteObject();
190     }
191     
192     private void readObject(ObjectInputStream JavaDoc is)
193     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
194         is.defaultReadObject();
195         completeConstruction();
196     }
197     
198     /**
199      */

200     class Execute implements Runnable JavaDoc {
201         synchronized public void run() {
202             logger().debug("Executing at [" + new Date JavaDoc()+ "]");
203             Runnable JavaDoc job = (Runnable JavaDoc) JobToken.retrieve(componentRegistry, jobToken);
204
205             if (job == null) {
206                 setJobStateException(
207                         new NullPointerException JavaDoc("Failed to find job for [" + jobToken + "]"));
208             }
209             
210             if (job instanceof Resetable) {
211                     ((Resetable) job).hardReset();
212             }
213             
214             if (job instanceof Stateful) {
215                 ((Stateful) job).addJobStateListener(jobListener);
216             }
217             if (job instanceof Iconic) {
218                 ((Iconic) job).addIconListener(iconListener);
219             }
220             
221             try {
222                 job.run();
223             }
224             catch (Throwable JavaDoc t) {
225                 setJobStateException(t);
226             }
227             finally {
228                 if (job instanceof Stateful) {
229                     ((Stateful) job).removeJobStateListener(jobListener);
230                 }
231                 if (job instanceof Iconic) {
232                     ((Iconic) job).removeIconListener(iconListener);
233                 }
234             }
235         }
236     }
237 }
238
Popular Tags