1 package org.apache.turbine.services.schedule;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.turbine.modules.ScheduledJobLoader;
22
23 /***
24 * Wrapper for a <code>JobEntry</code> to actually perform the job's action.
25 *
26 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
27 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
28 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
29 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
30 * @version $Id: WorkerThread.java 264148 2005-08-29 14:21:04Z henning $
31 */
32 public class WorkerThread
33 implements Runnable
34 {
35 /***
36 * The <code>JobEntry</code> to run.
37 */
38 private JobEntry je = null;
39
40 /*** Logging */
41 private static Log log = LogFactory.getLog(ScheduleService.LOGGER_NAME);
42
43 /***
44 * Creates a new worker to run the specified <code>JobEntry</code>.
45 *
46 * @param je The <code>JobEntry</code> to create a worker for.
47 */
48 public WorkerThread(JobEntry je)
49 {
50 this.je = je;
51 }
52
53 /***
54 * Run the job.
55 */
56 public void run()
57 {
58 if (je == null || je.isActive())
59 {
60 return;
61 }
62
63 try
64 {
65 if (!je.isActive())
66 {
67 je.setActive(true);
68 logStateChange("started");
69 ScheduledJobLoader.getInstance().exec(je, je.getTask());
70 }
71 }
72 catch (Exception e)
73 {
74 log.error("Error in WorkerThread for scheduled job #" +
75 je.getPrimaryKey() + ", task: " + je.getTask(), e);
76 }
77 finally
78 {
79 if (je.isActive())
80 {
81 je.setActive(false);
82 logStateChange("completed");
83 }
84 }
85 }
86
87 /***
88 * Macro to log <code>JobEntry</code> status information.
89 *
90 * @param state The new state of the <code>JobEntry</code>.
91 */
92 private final void logStateChange(String state)
93 {
94 log.debug("Scheduled job #" + je.getPrimaryKey() + ' ' + state +
95 ", task: " + je.getTask());
96 }
97 }