001: package org.apache.turbine.services.schedule;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.List;
023: import java.util.Vector;
024:
025: import javax.servlet.ServletConfig;
026:
027: import org.apache.commons.configuration.Configuration;
028:
029: import org.apache.commons.lang.StringUtils;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: import org.apache.turbine.services.InitializationException;
035: import org.apache.turbine.util.TurbineException;
036:
037: /**
038: * Service for a cron like scheduler that uses the
039: * TurbineResources.properties file instead of the database.
040: * The methods that operate on jobs ( get,add,update,remove )
041: * only operate on the queue in memory and changes are not reflected
042: * to the properties file which was used to initilize the jobs.
043: * An example is given below. The job names are the class names that
044: * extend ScheduledJob.
045: *
046: * <PRE>
047: *
048: * services.SchedulerService.scheduler.jobs=scheduledJobName,scheduledJobName2
049: *
050: * services.SchedulerService.scheduler.job.scheduledJobName.ID=1
051: * services.SchedulerService.scheduler.job.scheduledJobName.SECOND=-1
052: * services.SchedulerService.scheduler.job.scheduledJobName.MINUTE=-1
053: * services.SchedulerService.scheduler.job.scheduledJobName.HOUR=7
054: * services.SchedulerService.scheduler.job.scheduledJobName.WEEKDAY=-1
055: * services.SchedulerService.scheduler.job.scheduledJobName.DAY_OF_MONTH=-1
056: *
057: * services.SchedulerService.scheduler.job.scheduledJobName2.ID=1
058: * services.SchedulerService.scheduler.job.scheduledJobName2.SECOND=-1
059: * services.SchedulerService.scheduler.job.scheduledJobName2.MINUTE=-1
060: * services.SchedulerService.scheduler.job.scheduledJobName2.HOUR=7
061: * services.SchedulerService.scheduler.job.scheduledJobName2.WEEKDAY=-1
062: * services.SchedulerService.scheduler.job.scheduledJobName2.DAY_OF_MONTH=-1
063: *
064: * </PRE>
065: *
066: * Based on TamboraSchedulerService written by John Thorhauer.
067: *
068: * @author <a href="mailto:ekkerbj@netscpae.net">Jeff Brekke</a>
069: * @author <a href="mailto:john@zenplex.com">John Thorhauer</a>
070: * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
071: * @version $Id: TurbineNonPersistentSchedulerService.java 534527 2007-05-02 16:10:59Z tv $
072: */
073: public class TurbineNonPersistentSchedulerService extends
074: TurbineSchedulerService {
075: /** Logging */
076: private static Log log = LogFactory
077: .getLog(ScheduleService.LOGGER_NAME);
078:
079: /**
080: * Constructor.
081: *
082: * @exception TurbineException a generic exception.
083: */
084: public TurbineNonPersistentSchedulerService()
085: throws TurbineException {
086: super ();
087: }
088:
089: /**
090: * Called the first time the Service is used.<br>
091: *
092: * Load all the jobs from cold storage. Add jobs to the queue
093: * (sorted in ascending order by runtime) and start the scheduler
094: * thread.
095: */
096: public void init() throws InitializationException {
097: Configuration conf = getConfiguration();
098:
099: try {
100: scheduleQueue = new JobQueue();
101: mainLoop = new MainLoop();
102:
103: List jobProps = conf.getList("scheduler.jobs");
104: List jobs = new Vector();
105: // If there are scheduler.jobs defined then set up a job vector
106: // for the scheduleQueue
107: if (!jobProps.isEmpty()) {
108: for (int i = 0; i < jobProps.size(); i++) {
109: String jobName = (String) jobProps.get(i);
110: String jobPrefix = "scheduler.job." + jobName;
111:
112: String jobId = conf.getString(jobPrefix + ".ID",
113: null);
114: if (StringUtils.isEmpty(jobId)) {
115: throw new Exception(
116: "There is an error in the TurbineResources.properties file. \n"
117: + jobPrefix
118: + ".ID is not found.\n");
119: }
120:
121: int sec = conf.getInt(jobPrefix + ".SECOND", -1);
122: int min = conf.getInt(jobPrefix + ".MINUTE", -1);
123: int hr = conf.getInt(jobPrefix + ".HOUR", -1);
124: int wkday = conf.getInt(jobPrefix + ".WEEKDAY", -1);
125: int dayOfMonth = conf.getInt(jobPrefix
126: + ".DAY_OF_MONTH", -1);
127:
128: JobEntry je = new JobEntry(sec, min, hr, wkday,
129: dayOfMonth, jobName);
130: je.setJobId(Integer.parseInt(jobId));
131: jobs.add(je);
132:
133: }
134: }
135:
136: if (jobs != null && jobs.size() > 0) {
137: scheduleQueue.batchLoad(jobs);
138: }
139:
140: setEnabled(getConfiguration().getBoolean("enabled", true));
141: restart();
142:
143: setInit(true);
144: } catch (Exception e) {
145: String errorMessage = "Could not initialize the scheduler service";
146: log.error(errorMessage, e);
147: throw new InitializationException(errorMessage, e);
148: }
149: }
150:
151: /**
152: * Called the first time the Service is used.<br>
153: *
154: * Load all the jobs from cold storage. Add jobs to the queue
155: * (sorted in ascending order by runtime) and start the scheduler
156: * thread.
157: *
158: * @param config A ServletConfig.
159: * @deprecated use init() instead.
160: */
161: public void init(ServletConfig config)
162: throws InitializationException {
163: init();
164: }
165:
166: /**
167: * This method returns the job element from the internal queue.
168: *
169: * @param oid The int id for the job.
170: * @return A JobEntry.
171: * @exception TurbineException could not retrieve job
172: */
173: public JobEntry getJob(int oid) throws TurbineException {
174: JobEntry je = new JobEntry();
175: je.setJobId(oid);
176: return scheduleQueue.getJob(je);
177: }
178:
179: /**
180: * Add a new job to the queue.
181: *
182: * @param je A JobEntry with the job to add.
183: * @throws TurbineException job could not be added
184: */
185: public void addJob(JobEntry je) throws TurbineException {
186: updateJob(je);
187: }
188:
189: /**
190: * Remove a job from the queue.
191: *
192: * @param je A JobEntry with the job to remove.
193: */
194: public void removeJob(JobEntry je) {
195: // Remove from the queue.
196: scheduleQueue.remove(je);
197: restart();
198: }
199:
200: /**
201: * Add/update a job
202: *
203: * @param je A JobEntry with the job to modify
204: * @throws TurbineException job could not be updated
205: */
206: public void updateJob(JobEntry je) throws TurbineException {
207: try {
208: je.calcRunTime();
209:
210: // Update the queue.
211: scheduleQueue.modify(je);
212: restart();
213: } catch (Exception e) {
214: String errorMessage = "Problem updating Scheduled Job: "
215: + je.getTask();
216: log.error(errorMessage, e);
217: throw new TurbineException(errorMessage, e);
218: }
219: }
220: }
|