KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > schedule > TurbineNonPersistentSchedulerService


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

18
19 import java.util.List JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import javax.servlet.ServletConfig JavaDoc;
23
24 import org.apache.commons.configuration.Configuration;
25
26 import org.apache.commons.lang.StringUtils;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 import org.apache.turbine.services.InitializationException;
32 import org.apache.turbine.util.TurbineException;
33
34 /**
35  * Service for a cron like scheduler that uses the
36  * TurbineResources.properties file instead of the database.
37  * The methods that operate on jobs ( get,add,update,remove )
38  * only operate on the queue in memory and changes are not reflected
39  * to the properties file which was used to initilize the jobs.
40  * An example is given below. The job names are the class names that
41  * extend ScheduledJob.
42  *
43  * <PRE>
44  *
45  * services.SchedulerService.scheduler.jobs=scheduledJobName,scheduledJobName2
46  *
47  * services.SchedulerService.scheduler.job.scheduledJobName.ID=1
48  * services.SchedulerService.scheduler.job.scheduledJobName.SECOND=-1
49  * services.SchedulerService.scheduler.job.scheduledJobName.MINUTE=-1
50  * services.SchedulerService.scheduler.job.scheduledJobName.HOUR=7
51  * services.SchedulerService.scheduler.job.scheduledJobName.WEEKDAY=-1
52  * services.SchedulerService.scheduler.job.scheduledJobName.DAY_OF_MONTH=-1
53  *
54  * services.SchedulerService.scheduler.job.scheduledJobName2.ID=1
55  * services.SchedulerService.scheduler.job.scheduledJobName2.SECOND=-1
56  * services.SchedulerService.scheduler.job.scheduledJobName2.MINUTE=-1
57  * services.SchedulerService.scheduler.job.scheduledJobName2.HOUR=7
58  * services.SchedulerService.scheduler.job.scheduledJobName2.WEEKDAY=-1
59  * services.SchedulerService.scheduler.job.scheduledJobName2.DAY_OF_MONTH=-1
60  *
61  * </PRE>
62  *
63  * Based on TamboraSchedulerService written by John Thorhauer.
64  *
65  * @author <a HREF="mailto:ekkerbj@netscpae.net">Jeff Brekke</a>
66  * @author <a HREF="mailto:john@zenplex.com">John Thorhauer</a>
67  * @author <a HREF="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
68  * @version $Id: TurbineNonPersistentSchedulerService.java,v 1.16.2.3 2004/08/16 22:57:49 henning Exp $
69  */

70 public class TurbineNonPersistentSchedulerService
71         extends TurbineSchedulerService
72 {
73     /** Logging */
74     private static Log log = LogFactory.getLog(ScheduleService.LOGGER_NAME);
75
76     /**
77      * Constructor.
78      *
79      * @exception TurbineException a generic exception.
80      */

81     public TurbineNonPersistentSchedulerService()
82             throws TurbineException
83     {
84         super();
85     }
86
87     /**
88      * Called the first time the Service is used.<br>
89      *
90      * Load all the jobs from cold storage. Add jobs to the queue
91      * (sorted in ascending order by runtime) and start the scheduler
92      * thread.
93      */

94     public void init()
95             throws InitializationException
96     {
97         Configuration conf = getConfiguration();
98
99         try
100         {
101             scheduleQueue = new JobQueue();
102             mainLoop = new MainLoop();
103
104             List JavaDoc jobProps = conf.getList("scheduler.jobs");
105             List JavaDoc jobs = new Vector JavaDoc();
106             // If there are scheduler.jobs defined then set up a job vector
107
// for the scheduleQueue
108
if (!jobProps.isEmpty())
109             {
110                 for (int i = 0; i < jobProps.size(); i++)
111                 {
112                     String JavaDoc jobName = (String JavaDoc) jobProps.get(i);
113                     String JavaDoc jobPrefix = "scheduler.job." + jobName;
114
115                     String JavaDoc jobId = conf.getString(jobPrefix + ".ID", null);
116                     if (StringUtils.isEmpty(jobId))
117                     {
118                         throw new Exception JavaDoc(
119                                 "There is an error in the TurbineResources.properties file. \n"
120                                 + jobPrefix + ".ID is not found.\n");
121                     }
122
123                     int sec = conf.getInt(jobPrefix + ".SECOND", -1);
124                     int min = conf.getInt(jobPrefix + ".MINUTE", -1);
125                     int hr = conf.getInt(jobPrefix + ".HOUR", -1);
126                     int wkday = conf.getInt(jobPrefix + ".WEEKDAY", -1);
127                     int dayOfMonth = conf.getInt(jobPrefix + ".DAY_OF_MONTH", -1);
128
129                     JobEntry je = new JobEntry(
130                             sec,
131                             min,
132                             hr,
133                             wkday,
134                             dayOfMonth,
135                             jobName);
136                     je.setJobId(Integer.parseInt(jobId));
137                     jobs.add(je);
138
139                 }
140             }
141
142             if (jobs != null && jobs.size() > 0)
143             {
144                 scheduleQueue.batchLoad(jobs);
145             }
146
147             setEnabled(getConfiguration().getBoolean("enabled", true));
148             restart();
149
150             setInit(true);
151         }
152         catch (Exception JavaDoc e)
153         {
154             String JavaDoc errorMessage = "Could not initialize the scheduler service";
155             log.error(errorMessage, e);
156             throw new InitializationException(errorMessage, e);
157         }
158     }
159
160     /**
161      * Called the first time the Service is used.<br>
162      *
163      * Load all the jobs from cold storage. Add jobs to the queue
164      * (sorted in ascending order by runtime) and start the scheduler
165      * thread.
166      *
167      * @param config A ServletConfig.
168      * @deprecated use init() instead.
169      */

170     public void init(ServletConfig JavaDoc config)
171             throws InitializationException
172     {
173         init();
174     }
175
176     /**
177      * This method returns the job element from the internal queue.
178      *
179      * @param oid The int id for the job.
180      * @return A JobEntry.
181      * @exception TurbineException could not retrieve job
182      */

183     public JobEntry getJob(int oid)
184             throws TurbineException
185     {
186         JobEntry je = new JobEntry();
187         je.setJobId(oid);
188         return scheduleQueue.getJob(je);
189     }
190
191     /**
192      * Add a new job to the queue.
193      *
194      * @param je A JobEntry with the job to add.
195      * @throws TurbineException job could not be added
196      */

197     public void addJob(JobEntry je)
198             throws TurbineException
199     {
200         updateJob(je);
201     }
202
203     /**
204      * Remove a job from the queue.
205      *
206      * @param je A JobEntry with the job to remove.
207      */

208     public void removeJob(JobEntry je)
209     {
210         // Remove from the queue.
211
scheduleQueue.remove(je);
212         restart();
213     }
214
215     /**
216      * Add/update a job
217      *
218      * @param je A JobEntry with the job to modify
219      * @throws TurbineException job could not be updated
220      */

221     public void updateJob(JobEntry je)
222             throws TurbineException
223     {
224         try
225         {
226             je.calcRunTime();
227
228             // Update the queue.
229
scheduleQueue.modify(je);
230             restart();
231         }
232         catch (Exception JavaDoc e)
233         {
234             String JavaDoc errorMessage = "Problem updating Scheduled Job: " + je.getTask();
235             log.error(errorMessage, e);
236             throw new TurbineException(errorMessage, e);
237         }
238     }
239 }
240
Popular Tags