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