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:
24: /**
25: * A worker that performs a given job continuously.
26: */
27: public class ContinuousWorkerThread extends WorkerThread {
28:
29: private static Log mLogger = LogFactory
30: .getLog(ContinuousWorkerThread.class);
31:
32: // default sleep time is 10 seconds
33: long sleepTime = 10000;
34:
35: public ContinuousWorkerThread(String id) {
36: super (id);
37: }
38:
39: public ContinuousWorkerThread(String id, long sleep) {
40: super (id);
41:
42: this .sleepTime = sleep;
43: }
44:
45: public ContinuousWorkerThread(String id, Job job) {
46: super (id, job);
47: }
48:
49: public ContinuousWorkerThread(String id, Job job, long sleep) {
50: super (id, job);
51:
52: this .sleepTime = sleep;
53: }
54:
55: /**
56: * Thread execution.
57: *
58: * We run forever. Each time a job completes we sleep for
59: * some amount of time before trying again.
60: *
61: * If we ever get interrupted then we quit.
62: */
63: public void run() {
64:
65: mLogger.info(this .id + " Started.");
66:
67: // run forever
68: while (true) {
69:
70: // execute our job
71: super .run();
72:
73: // job is done, lets sleep it off for a bit
74: try {
75: mLogger.debug(this .id + " SLEEPING for "
76: + this .sleepTime + " milliseconds ...");
77: this .sleep(this .sleepTime);
78: } catch (InterruptedException e) {
79: mLogger.info(this .id + " INTERRUPT: " + e.getMessage());
80: break;
81: }
82: }
83: }
84:
85: }
|