View Javadoc
1 package org.apache.turbine.services.schedule; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.util.Vector; 58 import javax.servlet.ServletConfig; 59 import org.apache.torque.om.NumberKey; 60 import org.apache.torque.om.ObjectKey; 61 import org.apache.turbine.services.resources.TurbineResources; 62 63 /*** 64 * Service for a cron like scheduler that uses the 65 * TurbineResources.properties file instead of the database. 66 * The methods that operate on jobs ( get,add,update,remove ) 67 * only operate on the queue in memory and changes are not reflected 68 * to the properties file which was used to initilize the jobs. 69 * An example is given below. The job names are the class names that 70 * extend ScheduledJob. 71 * 72 *<PRE> 73 scheduler.jobs=scheduledJobName,scheduledJobName2 74 75 scheduler.job.scheduledJobName.ID=1 76 scheduler.job.scheduledJobName.SECOND=-1 77 scheduler.job.scheduledJobName.MINUTE=-1 78 scheduler.job.scheduledJobName.HOUR=7 79 scheduler.job.scheduledJobName.WEEKDAY=-1 80 scheduler.job.scheduledJobName.DAY_OF_MONTH=-1 81 82 scheduler.job.scheduledJobName2.ID=1 83 scheduler.job.scheduledJobName2.SECOND=-1 84 scheduler.job.scheduledJobName2.MINUTE=-1 85 scheduler.job.scheduledJobName2.HOUR=7 86 scheduler.job.scheduledJobName2.WEEKDAY=-1 87 scheduler.job.scheduledJobName2.DAY_OF_MONTH=-1 88 </PRE> 89 * 90 * Based on TamboraSchedulerService written by John Thorhauer. 91 * 92 * @author <a href="mailto:ekkerbj@netscpae.net">Jeff Brekke</a> 93 * @author <a href="mailto:john@zenplex.com">John Thorhauer</a> 94 */ 95 public class TurbineNonPersistentSchedulerService 96 extends TurbineSchedulerService 97 { 98 /*** 99 * Constructor. 100 * 101 * @exception Exception, a generic exception. 102 */ 103 public TurbineNonPersistentSchedulerService() 104 throws Exception 105 { 106 super(); 107 } 108 109 /*** 110 * Called the first time the Service is used.<br> 111 * 112 * Load all the jobs from cold storage. Add jobs to the queue 113 * (sorted in ascending order by runtime) and start the scheduler 114 * thread. 115 * 116 * @param config A ServletConfig. 117 */ 118 public void init(ServletConfig config) 119 { 120 if ( getInit() ) 121 { 122 return; 123 } 124 125 try 126 { 127 org.apache.turbine.util.Log.info ( "TurbineNonPersistentSchedulerService init()....starting!"); 128 129 scheduleQueue = new JobQueue(); 130 mainLoop = new MainLoop(); 131 132 Vector jobProps = TurbineResources.getVector("scheduler.jobs"); 133 Vector jobs = new Vector(); 134 // If there are scheduler.jobs defined then set up a job vector 135 // for the scheduleQueue 136 if (!jobProps.isEmpty()) 137 { 138 for (int i=0;i<jobProps.size();i++) 139 { 140 String jobName = (String)jobProps.elementAt(i); 141 String jobPrefix = "scheduler.job." + jobName ; 142 143 if ( (TurbineResources.getString(jobPrefix + ".ID", null)) == null) 144 { 145 throw new Exception( 146 "There is an error in the TurbineResources.properties file. \n" + 147 jobPrefix + ".ID is not found.\n"); 148 } 149 150 int sec = TurbineResources.getInt(jobPrefix + ".SECOND", -1); 151 int min = TurbineResources.getInt(jobPrefix + ".MINUTE", -1); 152 int hr = TurbineResources.getInt(jobPrefix + ".HOUR", -1); 153 int wkday = TurbineResources.getInt(jobPrefix + ".WEEKDAY", -1); 154 int dayOfMonth = TurbineResources.getInt(jobPrefix + ".DAY_OF_MONTH", -1); 155 156 JobEntry je = new JobEntry(sec, 157 min, 158 hr, 159 wkday, 160 dayOfMonth, 161 jobName); 162 jobs.addElement(je); 163 164 } 165 } 166 167 if ( jobs != null && jobs.size() > 0 ) 168 { 169 scheduleQueue.batchLoad(jobs); 170 restart(); 171 } 172 173 setInit(true); 174 org.apache.turbine.util.Log.info ( "TurbineNonPersistentSchedulerService init()....finished!"); 175 } 176 catch (Exception e) 177 { 178 org.apache.turbine.util.Log.error ( "Cannot initialize TurbineNonPersistentSchedulerService!: " + e ); 179 } 180 } 181 182 /*** 183 * This method returns the job element from the internal queue. 184 * 185 * @param oid The int id for the job. 186 * @return A JobEntry. 187 * @exception Exception, a generic exception. 188 */ 189 public JobEntry getJob(int oid) 190 throws Exception 191 { 192 JobEntry je = new JobEntry(-1, 193 -1, 194 -1, 195 -1, 196 -1, 197 null); 198 je.setPrimaryKey((ObjectKey) new NumberKey(oid)); 199 return scheduleQueue.getJob(je); 200 } 201 202 /*** 203 * Add a new job to the queue. 204 * 205 * @param je A JobEntry with the job to add. 206 * @exception Exception, a generic exception. 207 */ 208 public void addJob(JobEntry je) 209 throws Exception 210 { 211 // Add to the queue. 212 scheduleQueue.add(je); 213 restart(); 214 } 215 216 /*** 217 * Remove a job from the queue. 218 * 219 * @param je A JobEntry with the job to remove. 220 * @exception Exception, a generic exception. 221 */ 222 public void removeJob(JobEntry je) 223 throws Exception 224 { 225 // Remove from the queue. 226 scheduleQueue.remove(je); 227 restart(); 228 } 229 230 /*** 231 * Modify a Job. 232 * 233 * @param je A JobEntry with the job to modify 234 * @exception Exception, a generic exception. 235 */ 236 public void updateJob(JobEntry je) 237 throws Exception 238 { 239 try 240 { 241 je.calcRunTime(); 242 } 243 catch(Exception e) 244 { 245 // Log problems. 246 org.apache.turbine.util.Log.error ( "Problem updating Scheduled Job: " + e); 247 } 248 // Update the queue. 249 scheduleQueue.modify(je); 250 restart(); 251 } 252 }

This page was automatically generated by Maven