01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.jetspeed.scheduler;
18:
19: /**
20: * Wrapper for a <code>JobEntry</code> to actually perform the job's action.
21: *
22: * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
23: * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
24: * @version $Id: WorkerThread.java 516448 2007-03-09 16:25:47Z ate $
25: */
26: public class WorkerThread implements Runnable {
27: /**
28: * The <code>JobEntry</code> to run.
29: */
30: private JobEntry je = null;
31:
32: /**
33: * The {@link org.apache.fulcrum.logging.Logger} facility to use.
34: */
35:
36: /**
37: * Creates a new worker to run the specified <code>JobEntry</code>.
38: *
39: * @param je The <code>JobEntry</code> to create a worker for.
40: */
41: public WorkerThread(JobEntry je) {
42: this .je = je;
43: }
44:
45: /**
46: * Run the job.
47: */
48: public void run() {
49: if (je == null || je.isActive()) {
50: return;
51: }
52:
53: try {
54: if (!je.isActive()) {
55: je.setActive(true);
56: logStateChange("started");
57:
58: // We should have a set of job packages and
59: // search through them like the module
60: // loader does. This right here requires the
61: // getTask() method to return a class name.
62: String className = je.getTask();
63:
64: //If a FactoryService is registered, use it. Otherwise,
65: //instantiate the ScheduledJob directly.
66: ScheduledJob sc = (ScheduledJob) Class.forName(
67: className).newInstance();
68: sc.execute(je);
69: }
70: } catch (Exception e) {
71: //!! use the service for logging
72: //Log.error("Error in WorkerThread for sheduled job #" +
73: // je.getPrimaryKey() + ", task: " + je.getTask(), e);
74: } finally {
75: if (je.isActive()) {
76: je.setActive(false);
77: logStateChange("completed");
78: }
79: }
80: }
81:
82: /**
83: * Macro to log <code>JobEntry</code> status information.
84: *
85: * @param state The new state of the <code>JobEntry</code>.
86: */
87: private final void logStateChange(String state) {
88: //!! use the service to log.
89: //Log.debug("Scheduled job #" + je.getPrimaryKey() + ' ' + state +
90: // ", task: " + je.getTask());
91: }
92: }
|