KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > business > runnable > WorkerThread


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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  * WorkerThread.java
20  *
21  * Created on December 16, 2005, 6:12 PM
22  */

23
24 package org.apache.roller.business.runnable;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.roller.model.Roller;
29 import org.apache.roller.model.RollerFactory;
30
31
32 /**
33  * A generic worker thread that knows how execute a Job.
34  *
35  * @author Allen Gilliland
36  */

37 public class WorkerThread extends Thread JavaDoc {
38     
39     private static Log mLogger = LogFactory.getLog(WorkerThread.class);
40     
41     String JavaDoc id = null;
42     Job job = null;
43     
44     
45     /**
46      * A simple worker.
47      */

48     public WorkerThread(String JavaDoc id) {
49         this.id = id;
50     }
51     
52     
53     /**
54      * Start off with a job to do.
55      */

56     public WorkerThread(String JavaDoc id, Job job) {
57         this.id = id;
58         this.job = job;
59     }
60     
61     
62     /**
63      * Thread execution.
64      *
65      * We just execute the job we were given if it's non-null.
66      */

67     public void run() {
68         
69         // we only run once
70
if (this.job != null) {
71             // process job
72
try {
73                 this.job.execute();
74             } catch(Throwable JavaDoc t) {
75                 // oops
76
mLogger.error("Error executing job. "+
77                         "Worker = "+this.id+", "+
78                         "Job = "+this.job.getClass().getName(), t);
79             }
80             
81             // since this is a thread we have to make sure that we tidy up ourselves
82
Roller roller = RollerFactory.getRoller();
83             roller.release();
84         }
85         
86     }
87     
88     
89     /**
90      * Set the job for this worker.
91      */

92     public void setJob(Job newJob) {
93         mLogger.debug("NEW JOB: "+newJob.getClass().getName());
94         
95         // set the job
96
this.job = newJob;
97     }
98     
99 }
100
Popular Tags