01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. The ASF licenses this file to You
04: * under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License. For additional information regarding
15: * copyright in this work, please see the NOTICE file in the top level
16: * directory of this distribution.
17: */
18:
19: package org.apache.roller.business.runnable;
20:
21: import org.apache.commons.logging.Log;
22: import org.apache.commons.logging.LogFactory;
23: import org.apache.roller.business.Roller;
24: import org.apache.roller.business.RollerFactory;
25:
26: /**
27: * A generic worker thread that knows how execute a Job.
28: */
29: public class WorkerThread extends Thread {
30:
31: private static Log log = LogFactory.getLog(WorkerThread.class);
32:
33: String id = null;
34: Job job = null;
35:
36: /**
37: * A simple worker.
38: */
39: public WorkerThread(String id) {
40: super (id);
41: this .id = id;
42: }
43:
44: /**
45: * Start off with a job to do.
46: */
47: public WorkerThread(String id, Job job) {
48: super (id);
49: this .id = id;
50: this .job = job;
51: }
52:
53: /**
54: * Thread execution.
55: *
56: * We just execute the job we were given if it's non-null.
57: */
58: public void run() {
59:
60: // we only run once
61: if (this .job != null) {
62: // process job
63: try {
64: this .job.execute();
65: } catch (Throwable t) {
66: // oops
67: log.error("Error executing job. " + "Worker = "
68: + this .id + ", " + "Job = "
69: + this .job.getClass().getName(), t);
70: }
71:
72: // since this is a thread we have to make sure that we tidy up ourselves
73: Roller roller = RollerFactory.getRoller();
74: roller.release();
75: }
76:
77: }
78:
79: /**
80: * Set the job for this worker.
81: */
82: public void setJob(Job newJob) {
83: log.debug("NEW JOB: " + newJob.getClass().getName());
84:
85: // set the job
86: this.job = newJob;
87: }
88:
89: }
|