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