KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > scheduler > CmsSchedulerThread


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/scheduler/CmsSchedulerThread.java,v $
3  * Date : $Date: 2006/03/27 14:52:20 $
4  * Version: $Revision: 1.10 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.scheduler;
33
34 import org.opencms.main.CmsLog;
35
36 import org.apache.commons.logging.Log;
37
38 /**
39  * A worker thread for the OpenCms scheduler.<p>
40  *
41  * @author Alexander Kandzior
42  *
43  * @version $Revision: 1.10 $
44  *
45  * @since 6.0.0
46  */

47 public class CmsSchedulerThread extends Thread JavaDoc {
48
49     /** The log object for this class. */
50     private static final Log LOG = CmsLog.getLog(CmsSchedulerThread.class);
51
52     /** The scheduler thread pool this thread belongs to. */
53     private CmsSchedulerThreadPool m_pool;
54
55     /** A flag that signals the thread to terminate. */
56     private boolean m_run;
57
58     /** A runnable class. */
59     private Runnable JavaDoc m_runnable;
60
61     /**
62      * Create a scheduler thread that runs continuosly,
63      * waiting for new runnables to be provided by the scheduler thread pool.<p>
64      *
65      * @param pool the pool to use
66      * @param threadGroup the thread group to use
67      * @param threadName the name for the thread
68      * @param prio the priority of the thread
69      * @param isDaemon controls if this should be a deamon thread or not
70      */

71     CmsSchedulerThread(
72         CmsSchedulerThreadPool pool,
73         ThreadGroup JavaDoc threadGroup,
74         String JavaDoc threadName,
75         int prio,
76         boolean isDaemon) {
77
78         this(pool, threadGroup, threadName, prio, isDaemon, null);
79     }
80
81     /**
82      * Create a scheduler thread that runs the specified runnable exactly once.<p>
83      *
84      * @param pool the pool to use
85      * @param threadGroup the thread group to use
86      * @param threadName the name for the thread
87      * @param prio the priority of the thread
88      * @param isDaemon controls if this should be a deamon thread or not
89      * @param runnable the runnable to run
90      */

91     CmsSchedulerThread(
92         CmsSchedulerThreadPool pool,
93         ThreadGroup JavaDoc threadGroup,
94         String JavaDoc threadName,
95         int prio,
96         boolean isDaemon,
97         Runnable JavaDoc runnable) {
98
99         super(threadGroup, threadName);
100         m_run = true;
101         m_pool = pool;
102         m_runnable = runnable;
103         setPriority(prio);
104         setDaemon(isDaemon);
105         start();
106     }
107
108     /**
109      * Loop, executing targets as they are received.<p>
110      */

111     public void run() {
112
113         boolean runOnce = (m_runnable != null);
114
115         while (m_run) {
116             setPriority(m_pool.getThreadPriority());
117             try {
118                 if (m_runnable == null) {
119                     m_runnable = m_pool.getNextRunnable();
120                 }
121
122                 if (m_runnable != null) {
123                     m_runnable.run();
124                 }
125             } catch (InterruptedException JavaDoc e) {
126                 LOG.error(Messages.get().getBundle().key(Messages.LOG_THREAD_INTERRUPTED_1, getName()), e);
127             } catch (Throwable JavaDoc t) {
128                 LOG.error(Messages.get().getBundle().key(Messages.LOG_THREAD_ERROR_1, getName()), t);
129             } finally {
130                 if (runOnce) {
131                     m_run = false;
132                 }
133                 m_runnable = null;
134             }
135         }
136         if (LOG.isDebugEnabled()) {
137             LOG.debug(Messages.get().getBundle().key(Messages.LOG_THREAD_SHUTDOWN_1, getName()));
138         }
139     }
140
141     /**
142      * Signal the thread that it should terminate.<p>
143      */

144     void shutdown() {
145
146         m_run = false;
147     }
148 }
Popular Tags