%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.services.schedule.JobEntry |
|
|
1 | package org.apache.turbine.services.schedule; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 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.Calendar; |
|
20 | import java.util.Date; |
|
21 | ||
22 | import org.apache.commons.lang.StringUtils; |
|
23 | ||
24 | import org.apache.commons.logging.Log; |
|
25 | import org.apache.commons.logging.LogFactory; |
|
26 | ||
27 | import org.apache.turbine.util.TurbineException; |
|
28 | ||
29 | /** |
|
30 | * This is a wrapper for a scheduled job. It is modeled after the |
|
31 | * Unix scheduler cron. Note: BaseJobEntry is generated by Torque |
|
32 | * at compile time, and is therefore NOT in CVS. |
|
33 | * |
|
34 | * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a> |
|
35 | * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a> |
|
36 | * @version $Id: JobEntry.java 264148 2005-08-29 14:21:04Z henning $ |
|
37 | */ |
|
38 | public class JobEntry |
|
39 | extends BaseJobEntry |
|
40 | implements Comparable |
|
41 | { |
|
42 | /** Logging */ |
|
43 | 4 | private static Log log = LogFactory.getLog(ScheduleService.LOGGER_NAME); |
44 | ||
45 | /** indicates if job is currently running */ |
|
46 | 10 | private boolean jobIsActive = false; |
47 | ||
48 | /** Next runtime. **/ |
|
49 | 10 | private long runtime = 0; |
50 | ||
51 | /** schedule types **/ |
|
52 | private static final int SECOND = 0; |
|
53 | private static final int MINUTE = 1; |
|
54 | private static final int WEEK_DAY = 2; |
|
55 | private static final int DAY_OF_MONTH = 3; |
|
56 | private static final int DAILY = 4; |
|
57 | ||
58 | /** |
|
59 | * default constructor |
|
60 | */ |
|
61 | public JobEntry() |
|
62 | 8 | { |
63 | 8 | } |
64 | ||
65 | /** |
|
66 | * Constuctor. |
|
67 | * |
|
68 | * Schedule a job to run on a certain point of time.<br> |
|
69 | * |
|
70 | * Example 1: Run the DefaultScheduledJob at 8:00am every 15th of |
|
71 | * the month - <br> |
|
72 | * |
|
73 | * JobEntry je = new JobEntry(0,0,8,15,"DefaultScheduledJob");<br> |
|
74 | * |
|
75 | * Example 2: Run the DefaultScheduledJob at 8:00am every day - |
|
76 | * <br> |
|
77 | * |
|
78 | * JobEntry je = new JobEntry(0,0,8,-1,"DefaultScheduledJob");<br> |
|
79 | * |
|
80 | * Example 3: Run the DefaultScheduledJob every 2 hours. - <br> |
|
81 | * |
|
82 | * JobEntry je = new JobEntry(0,120,-1,-1,"DefaultScheduledJob");<br> |
|
83 | * |
|
84 | * Example 4: Run the DefaultScheduledJob every 30 seconds. - <br> |
|
85 | * |
|
86 | * JobEntry je = new JobEntry(30,-1,-1,-1,"DefaultScheduledJob");<br> |
|
87 | * |
|
88 | * @param sec Value for entry "seconds". |
|
89 | * @param min Value for entry "minutes". |
|
90 | * @param hour Value for entry "hours". |
|
91 | * @param wd Value for entry "week days". |
|
92 | * @param day_mo Value for entry "month days". |
|
93 | * @param task Task to execute. |
|
94 | * @exception TurbineException a generic exception. |
|
95 | */ |
|
96 | public JobEntry(int sec, |
|
97 | int min, |
|
98 | int hour, |
|
99 | int wd, |
|
100 | int day_mo, |
|
101 | String task) |
|
102 | throws TurbineException |
|
103 | 2 | { |
104 | 2 | if (StringUtils.isEmpty(task)) |
105 | { |
|
106 | 0 | throw new TurbineException("Error in JobEntry. " + |
107 | "Bad Job parameter. Task not set."); |
|
108 | } |
|
109 | ||
110 | 2 | setSecond(sec); |
111 | 2 | setMinute(min); |
112 | 2 | setHour(hour); |
113 | 2 | setWeekDay(wd); |
114 | 2 | setDayOfMonth(day_mo); |
115 | 2 | setTask(task); |
116 | ||
117 | 2 | calcRunTime(); |
118 | 2 | } |
119 | ||
120 | /** |
|
121 | * Used for ordering Jobentries |
|
122 | * Note: this comparator imposes orderings that are inconsistent with |
|
123 | * equals. |
|
124 | * |
|
125 | * @param je The first <code>JobEntry</code> object. |
|
126 | * @return An <code>int</code> indicating the result of the comparison. |
|
127 | */ |
|
128 | public int compareTo(Object je) |
|
129 | { |
|
130 | 0 | int result = -1; |
131 | 0 | if (je instanceof JobEntry) |
132 | { |
|
133 | 0 | result = getJobId() - ((JobEntry) je).getJobId(); |
134 | } |
|
135 | 0 | return result; |
136 | } |
|
137 | ||
138 | /** |
|
139 | * Sets whether the job is running. |
|
140 | * |
|
141 | * @param isActive Whether the job is running. |
|
142 | */ |
|
143 | public void setActive(boolean isActive) |
|
144 | { |
|
145 | 0 | jobIsActive = isActive; |
146 | 0 | } |
147 | ||
148 | /** |
|
149 | * Check to see if job is currently active/running |
|
150 | * |
|
151 | * @return true if job is currently geing run by the |
|
152 | * workerthread, otherwise false |
|
153 | */ |
|
154 | public boolean isActive() |
|
155 | { |
|
156 | 0 | return jobIsActive; |
157 | } |
|
158 | ||
159 | /** |
|
160 | * Get the next runtime for this job as a long. |
|
161 | * |
|
162 | * @return The next run time as a long. |
|
163 | */ |
|
164 | public long getNextRuntime() |
|
165 | { |
|
166 | 12 | return runtime; |
167 | } |
|
168 | ||
169 | /** |
|
170 | * Gets the next runtime as a date |
|
171 | * |
|
172 | * @return Next run date |
|
173 | */ |
|
174 | public Date getNextRunDate() |
|
175 | { |
|
176 | 6 | return new Date(runtime); |
177 | } |
|
178 | ||
179 | /** |
|
180 | * Get the next runtime for this job as a String. |
|
181 | * |
|
182 | * @return The next run time as a String. |
|
183 | */ |
|
184 | public String getNextRunAsString() |
|
185 | { |
|
186 | 0 | return getNextRunDate().toString(); |
187 | } |
|
188 | ||
189 | /** |
|
190 | * Calculate how long before the next runtime.<br> |
|
191 | * |
|
192 | * The runtime determines it's position in the job queue. |
|
193 | * Here's the logic:<br> |
|
194 | * |
|
195 | * 1. Create a date the represents when this job is to run.<br> |
|
196 | * |
|
197 | * 2. If this date has expired, them "roll" appropriate date |
|
198 | * fields forward to the next date.<br> |
|
199 | * |
|
200 | * 3. Calculate the diff in time between the current time and the |
|
201 | * next run time.<br> |
|
202 | * |
|
203 | * @exception TurbineException a generic exception. |
|
204 | */ |
|
205 | public void calcRunTime() |
|
206 | throws TurbineException |
|
207 | { |
|
208 | 6 | Calendar schedrun = Calendar.getInstance(); |
209 | 6 | Calendar now = Calendar.getInstance(); |
210 | ||
211 | 6 | switch (evaluateJobType()) |
212 | { |
|
213 | case SECOND: |
|
214 | // SECOND (every so many seconds...) |
|
215 | 2 | schedrun.add(Calendar.SECOND, getSecond()); |
216 | 2 | runtime = schedrun.getTime().getTime(); |
217 | 2 | break; |
218 | ||
219 | case MINUTE: |
|
220 | // MINUTE (every so many minutes...) |
|
221 | 4 | schedrun.add(Calendar.SECOND, getSecond()); |
222 | 4 | schedrun.add(Calendar.MINUTE, getMinute()); |
223 | 4 | runtime = schedrun.getTime().getTime(); |
224 | 4 | break; |
225 | ||
226 | case WEEK_DAY: |
|
227 | // WEEKDAY (day of the week) |
|
228 | 0 | schedrun.set(Calendar.SECOND, getSecond()); |
229 | 0 | schedrun.set(Calendar.MINUTE, getMinute()); |
230 | 0 | schedrun.set(Calendar.HOUR_OF_DAY, getHour()); |
231 | 0 | schedrun.set(Calendar.DAY_OF_WEEK, getWeekDay()); |
232 | ||
233 | 0 | if (now.before(schedrun)) |
234 | { |
|
235 | // Scheduled time has NOT expired. |
|
236 | 0 | runtime = schedrun.getTime().getTime(); |
237 | } |
|
238 | else |
|
239 | { |
|
240 | // Scheduled time has expired; roll to the next week. |
|
241 | 0 | schedrun.add(Calendar.DAY_OF_WEEK, 7); |
242 | 0 | runtime = schedrun.getTime().getTime(); |
243 | } |
|
244 | 0 | break; |
245 | ||
246 | case DAY_OF_MONTH: |
|
247 | // DAY_OF_MONTH (date of the month) |
|
248 | 0 | schedrun.set(Calendar.SECOND, getSecond()); |
249 | 0 | schedrun.set(Calendar.MINUTE, getMinute()); |
250 | 0 | schedrun.set(Calendar.HOUR_OF_DAY, getHour()); |
251 | 0 | schedrun.set(Calendar.DAY_OF_MONTH, getDayOfMonth()); |
252 | ||
253 | 0 | if (now.before(schedrun)) |
254 | { |
|
255 | // Scheduled time has NOT expired. |
|
256 | 0 | runtime = schedrun.getTime().getTime(); |
257 | } |
|
258 | else |
|
259 | { |
|
260 | // Scheduled time has expired; roll to the next month. |
|
261 | 0 | schedrun.add(Calendar.MONTH, 1); |
262 | 0 | runtime = schedrun.getTime().getTime(); |
263 | } |
|
264 | 0 | break; |
265 | ||
266 | case DAILY: |
|
267 | // DAILY (certain hour:minutes of the day) |
|
268 | 0 | schedrun.set(Calendar.SECOND, getSecond()); |
269 | 0 | schedrun.set(Calendar.MINUTE, getMinute()); |
270 | 0 | schedrun.set(Calendar.HOUR_OF_DAY, getHour()); |
271 | ||
272 | // Scheduled time has NOT expired. |
|
273 | 0 | if (now.before(schedrun)) |
274 | { |
|
275 | 0 | runtime = schedrun.getTime().getTime(); |
276 | } |
|
277 | else |
|
278 | { |
|
279 | // Scheduled time has expired; roll forward 24 hours. |
|
280 | 0 | schedrun.add(Calendar.HOUR_OF_DAY, 24); |
281 | 0 | runtime = schedrun.getTime().getTime(); |
282 | } |
|
283 | 0 | break; |
284 | ||
285 | default: |
|
286 | // Do nothing. |
|
287 | } |
|
288 | ||
289 | 6 | log.info("Next runtime for task " + this.getTask() + " is " + this.getNextRunDate()); |
290 | 6 | } |
291 | ||
292 | /** |
|
293 | * What schedule am I on? |
|
294 | * |
|
295 | * I know this is kinda ugly! If you can think of a cleaner way |
|
296 | * to do this, please jump in! |
|
297 | * |
|
298 | * @return A number specifying the type of schedule. See |
|
299 | * calcRunTime(). |
|
300 | * @exception TurbineException a generic exception. |
|
301 | */ |
|
302 | private int evaluateJobType() |
|
303 | throws TurbineException |
|
304 | { |
|
305 | ||
306 | // First start by checking if it's a day of the month job. |
|
307 | 6 | if (getDayOfMonth() < 0) |
308 | { |
|
309 | // Not a day of the month job... check weekday. |
|
310 | 6 | if (getWeekDay() < 0) |
311 | { |
|
312 | // Not a weekday job...check if by the hour. |
|
313 | 6 | if (getHour() < 0) |
314 | { |
|
315 | // Not an hourly job...check if it is by the minute |
|
316 | 6 | if (getMinute() < 0) |
317 | { |
|
318 | // Not a by the minute job so must be by the second |
|
319 | 2 | if (getSecond() < 0) |
320 | 0 | throw new TurbineException("Error in JobEntry. Bad Job parameter."); |
321 | ||
322 | 2 | return SECOND; |
323 | } |
|
324 | else |
|
325 | { |
|
326 | // Must be a job run by the minute so we need minutes and |
|
327 | // seconds. |
|
328 | 4 | if (getMinute() < 0 || getSecond() < 0) |
329 | 0 | throw new TurbineException("Error in JobEntry. Bad Job parameter."); |
330 | ||
331 | 4 | return MINUTE; |
332 | } |
|
333 | } |
|
334 | else |
|
335 | { |
|
336 | // Must be a daily job by hours minutes, and seconds. In |
|
337 | // this case, we need the minute, second, and hour params. |
|
338 | 0 | if (getMinute() < 0 || getHour() < 0 || getSecond() < 0) |
339 | 0 | throw new TurbineException("Error in JobEntry. Bad Job parameter."); |
340 | ||
341 | 0 | return DAILY; |
342 | } |
|
343 | } |
|
344 | else |
|
345 | { |
|
346 | // Must be a weekday job. In this case, we need |
|
347 | // minute, second, and hour params |
|
348 | 0 | if (getMinute() < 0 || getHour() < 0 || getSecond() < 0) |
349 | 0 | throw new TurbineException("Error in JobEntry. Bad Job parameter."); |
350 | ||
351 | 0 | return WEEK_DAY; |
352 | } |
|
353 | } |
|
354 | else |
|
355 | { |
|
356 | // Must be a day of the month job. In this case, we need |
|
357 | // minute, second, and hour params |
|
358 | 0 | if (getMinute() < 0 || getHour() < 0) |
359 | 0 | throw new TurbineException("Error in JobEntry. Bad Job parameter."); |
360 | ||
361 | 0 | return DAY_OF_MONTH; |
362 | } |
|
363 | } |
|
364 | ||
365 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |