View Javadoc

1   package org.apache.turbine.services.schedule;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.List;
23  import java.util.Vector;
24  
25  import javax.servlet.ServletConfig;
26  
27  import org.apache.commons.configuration.Configuration;
28  
29  import org.apache.commons.lang.StringUtils;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  import org.apache.turbine.services.InitializationException;
35  import org.apache.turbine.util.TurbineException;
36  
37  /***
38   * Service for a cron like scheduler that uses the
39   * TurbineResources.properties file instead of the database.
40   * The methods that operate on jobs ( get,add,update,remove )
41   * only operate on the queue in memory and changes are not reflected
42   * to the properties file which was used to initilize the jobs.
43   * An example is given below.  The job names are the class names that
44   * extend ScheduledJob.
45   *
46   * <PRE>
47   *
48   * services.SchedulerService.scheduler.jobs=scheduledJobName,scheduledJobName2
49   *
50   * services.SchedulerService.scheduler.job.scheduledJobName.ID=1
51   * services.SchedulerService.scheduler.job.scheduledJobName.SECOND=-1
52   * services.SchedulerService.scheduler.job.scheduledJobName.MINUTE=-1
53   * services.SchedulerService.scheduler.job.scheduledJobName.HOUR=7
54   * services.SchedulerService.scheduler.job.scheduledJobName.WEEKDAY=-1
55   * services.SchedulerService.scheduler.job.scheduledJobName.DAY_OF_MONTH=-1
56   *
57   * services.SchedulerService.scheduler.job.scheduledJobName2.ID=1
58   * services.SchedulerService.scheduler.job.scheduledJobName2.SECOND=-1
59   * services.SchedulerService.scheduler.job.scheduledJobName2.MINUTE=-1
60   * services.SchedulerService.scheduler.job.scheduledJobName2.HOUR=7
61   * services.SchedulerService.scheduler.job.scheduledJobName2.WEEKDAY=-1
62   * services.SchedulerService.scheduler.job.scheduledJobName2.DAY_OF_MONTH=-1
63   *
64   * </PRE>
65   *
66   * Based on TamboraSchedulerService written by John Thorhauer.
67   *
68   * @author <a href="mailto:ekkerbj@netscpae.net">Jeff Brekke</a>
69   * @author <a href="mailto:john@zenplex.com">John Thorhauer</a>
70   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
71   * @version $Id: TurbineNonPersistentSchedulerService.java 534527 2007-05-02 16:10:59Z tv $
72   */
73  public class TurbineNonPersistentSchedulerService
74          extends TurbineSchedulerService
75  {
76      /*** Logging */
77      private static Log log = LogFactory.getLog(ScheduleService.LOGGER_NAME);
78  
79      /***
80       * Constructor.
81       *
82       * @exception TurbineException a generic exception.
83       */
84      public TurbineNonPersistentSchedulerService()
85              throws TurbineException
86      {
87          super();
88      }
89  
90      /***
91       * Called the first time the Service is used.<br>
92       *
93       * Load all the jobs from cold storage.  Add jobs to the queue
94       * (sorted in ascending order by runtime) and start the scheduler
95       * thread.
96       */
97      public void init()
98              throws InitializationException
99      {
100         Configuration conf = getConfiguration();
101 
102         try
103         {
104             scheduleQueue = new JobQueue();
105             mainLoop = new MainLoop();
106 
107             List jobProps = conf.getList("scheduler.jobs");
108             List jobs = new Vector();
109             // If there are scheduler.jobs defined then set up a job vector
110             // for the scheduleQueue
111             if (!jobProps.isEmpty())
112             {
113                 for (int i = 0; i < jobProps.size(); i++)
114                 {
115                     String jobName = (String) jobProps.get(i);
116                     String jobPrefix = "scheduler.job." + jobName;
117 
118                     String jobId = conf.getString(jobPrefix + ".ID", null);
119                     if (StringUtils.isEmpty(jobId))
120                     {
121                         throw new Exception(
122                                 "There is an error in the TurbineResources.properties file. \n"
123                                 + jobPrefix + ".ID is not found.\n");
124                     }
125 
126                     int sec = conf.getInt(jobPrefix + ".SECOND", -1);
127                     int min = conf.getInt(jobPrefix + ".MINUTE", -1);
128                     int hr = conf.getInt(jobPrefix + ".HOUR", -1);
129                     int wkday = conf.getInt(jobPrefix + ".WEEKDAY", -1);
130                     int dayOfMonth = conf.getInt(jobPrefix + ".DAY_OF_MONTH", -1);
131 
132                     JobEntry je = new JobEntry(
133                             sec,
134                             min,
135                             hr,
136                             wkday,
137                             dayOfMonth,
138                             jobName);
139                     je.setJobId(Integer.parseInt(jobId));
140                     jobs.add(je);
141 
142                 }
143             }
144 
145             if (jobs != null && jobs.size() > 0)
146             {
147                 scheduleQueue.batchLoad(jobs);
148             }
149 
150             setEnabled(getConfiguration().getBoolean("enabled", true));
151             restart();
152 
153             setInit(true);
154         }
155         catch (Exception e)
156         {
157             String errorMessage = "Could not initialize the scheduler service";
158             log.error(errorMessage, e);
159             throw new InitializationException(errorMessage, e);
160         }
161     }
162 
163     /***
164      * Called the first time the Service is used.<br>
165      *
166      * Load all the jobs from cold storage.  Add jobs to the queue
167      * (sorted in ascending order by runtime) and start the scheduler
168      * thread.
169      *
170      * @param config A ServletConfig.
171      * @deprecated use init() instead.
172      */
173     public void init(ServletConfig config)
174             throws InitializationException
175     {
176         init();
177     }
178 
179     /***
180      * This method returns the job element from the internal queue.
181      *
182      * @param oid The int id for the job.
183      * @return A JobEntry.
184      * @exception TurbineException could not retrieve job
185      */
186     public JobEntry getJob(int oid)
187             throws TurbineException
188     {
189         JobEntry je = new JobEntry();
190         je.setJobId(oid);
191         return scheduleQueue.getJob(je);
192     }
193 
194     /***
195      * Add a new job to the queue.
196      *
197      * @param je A JobEntry with the job to add.
198      * @throws TurbineException job could not be added
199      */
200     public void addJob(JobEntry je)
201             throws TurbineException
202     {
203         updateJob(je);
204     }
205 
206     /***
207      * Remove a job from the queue.
208      *
209      * @param je A JobEntry with the job to remove.
210      */
211     public void removeJob(JobEntry je)
212     {
213         // Remove from the queue.
214         scheduleQueue.remove(je);
215         restart();
216     }
217 
218     /***
219      * Add/update a job
220      *
221      * @param je A JobEntry with the job to modify
222      * @throws TurbineException job could not be updated
223      */
224     public void updateJob(JobEntry je)
225             throws TurbineException
226     {
227         try
228         {
229             je.calcRunTime();
230 
231             // Update the queue.
232             scheduleQueue.modify(je);
233             restart();
234         }
235         catch (Exception e)
236         {
237             String errorMessage = "Problem updating Scheduled Job: " + je.getTask();
238             log.error(errorMessage, e);
239             throw new TurbineException(errorMessage, e);
240         }
241     }
242 }