01: package org.apache.turbine.services.schedule;
02:
03: /*
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
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 implements Runnable {
36: /**
37: * The <code>JobEntry</code> to run.
38: */
39: private JobEntry je = null;
40:
41: /** Logging */
42: private static Log log = LogFactory
43: .getLog(ScheduleService.LOGGER_NAME);
44:
45: /**
46: * Creates a new worker to run the specified <code>JobEntry</code>.
47: *
48: * @param je The <code>JobEntry</code> to create a worker for.
49: */
50: public WorkerThread(JobEntry je) {
51: this .je = je;
52: }
53:
54: /**
55: * Run the job.
56: */
57: public void run() {
58: if (je == null || je.isActive()) {
59: return;
60: }
61:
62: try {
63: if (!je.isActive()) {
64: je.setActive(true);
65: logStateChange("started");
66: ScheduledJobLoader.getInstance().exec(je, je.getTask());
67: }
68: } catch (Exception e) {
69: log
70: .error("Error in WorkerThread for scheduled job #"
71: + je.getPrimaryKey() + ", task: "
72: + je.getTask(), e);
73: } finally {
74: if (je.isActive()) {
75: je.setActive(false);
76: logStateChange("completed");
77: }
78: }
79: }
80:
81: /**
82: * Macro to log <code>JobEntry</code> status information.
83: *
84: * @param state The new state of the <code>JobEntry</code>.
85: */
86: private final void logStateChange(String state) {
87: log.debug("Scheduled job #" + je.getPrimaryKey() + ' ' + state
88: + ", task: " + je.getTask());
89: }
90: }
|