KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ContinuousWorkerThread.java
20  *
21  * Created on December 20, 2005, 1:57 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
29
30 /**
31  * A worker that performs a given job continuously.
32  *
33  * @author Allen Gilliland
34  */

35 public class ContinuousWorkerThread extends WorkerThread {
36     
37     private static Log mLogger = LogFactory.getLog(ContinuousWorkerThread.class);
38     
39     // default sleep time is 10 seconds
40
long sleepTime = 10000;
41     
42     
43     public ContinuousWorkerThread(String JavaDoc id) {
44         super(id);
45     }
46     
47     
48     public ContinuousWorkerThread(String JavaDoc id, long sleep) {
49         super(id);
50         
51         this.sleepTime = sleep;
52     }
53     
54     
55     public ContinuousWorkerThread(String JavaDoc id, Job job) {
56         super(id, job);
57     }
58     
59     
60     public ContinuousWorkerThread(String JavaDoc id, Job job, long sleep) {
61         super(id, job);
62         
63         this.sleepTime = sleep;
64     }
65     
66     
67     /**
68      * Thread execution.
69      *
70      * We run forever. Each time a job completes we sleep for
71      * some amount of time before trying again.
72      *
73      * If we ever get interrupted then we quit.
74      */

75     public void run() {
76         
77         mLogger.info(this.id+" Started.");
78         
79         // run forever
80
while(true) {
81             
82             // execute our job
83
super.run();
84             
85             // job is done, lets sleep it off for a bit
86
try {
87                 mLogger.debug(this.id+" SLEEPING for "+this.sleepTime+" milliseconds ...");
88                 this.sleep(this.sleepTime);
89             } catch (InterruptedException JavaDoc e) {
90                 mLogger.info(this.id+" INTERRUPT: "+e.getMessage());
91                 break;
92             }
93         }
94     }
95     
96 }
97
Popular Tags