001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.scheduler;
029
030import org.opencms.main.CmsLog;
031
032import org.apache.commons.logging.Log;
033
034/**
035 * A worker thread for the OpenCms scheduler.<p>
036 *
037 * @since 6.0.0
038 */
039public class CmsSchedulerThread extends Thread {
040
041    /** The log object for this class. */
042    private static final Log LOG = CmsLog.getLog(CmsSchedulerThread.class);
043
044    /** The scheduler thread pool this thread belongs to. */
045    private CmsSchedulerThreadPool m_pool;
046
047    /** A flag that signals the thread to terminate. */
048    private boolean m_run;
049
050    /** A runnable class. */
051    private Runnable m_runnable;
052
053    /**
054     * Create a scheduler thread that runs continuosly,
055     * waiting for new runnables to be provided by the scheduler thread pool.<p>
056     *
057     * @param pool the pool to use
058     * @param threadGroup the thread group to use
059     * @param threadName the name for the thread
060     * @param prio the priority of the thread
061     * @param isDaemon controls if this should be a deamon thread or not
062     */
063    CmsSchedulerThread(
064        CmsSchedulerThreadPool pool,
065        ThreadGroup threadGroup,
066        String threadName,
067        int prio,
068        boolean isDaemon) {
069
070        this(pool, threadGroup, threadName, prio, isDaemon, null);
071    }
072
073    /**
074     * Create a scheduler thread that runs the specified runnable exactly once.<p>
075     *
076     * @param pool the pool to use
077     * @param threadGroup the thread group to use
078     * @param threadName the name for the thread
079     * @param prio the priority of the thread
080     * @param isDaemon controls if this should be a deamon thread or not
081     * @param runnable the runnable to run
082     */
083    CmsSchedulerThread(
084        CmsSchedulerThreadPool pool,
085        ThreadGroup threadGroup,
086        String threadName,
087        int prio,
088        boolean isDaemon,
089        Runnable runnable) {
090
091        super(threadGroup, threadName);
092        m_run = true;
093        m_pool = pool;
094        m_runnable = runnable;
095        setPriority(prio);
096        setDaemon(isDaemon);
097    }
098
099    /**
100     * Loop, executing targets as they are received.<p>
101     */
102    @Override
103    public void run() {
104
105        boolean runOnce = (m_runnable != null);
106
107        while (m_run) {
108            setPriority(m_pool.getThreadPriority());
109            try {
110                if (m_runnable == null) {
111                    m_runnable = m_pool.getNextRunnable();
112                }
113
114                if (m_runnable != null) {
115                    m_runnable.run();
116                }
117            } catch (InterruptedException e) {
118                LOG.error(Messages.get().getBundle().key(Messages.LOG_THREAD_INTERRUPTED_1, getName()), e);
119            } catch (Throwable t) {
120                LOG.error(Messages.get().getBundle().key(Messages.LOG_THREAD_ERROR_1, getName()), t);
121            } finally {
122                if (runOnce) {
123                    m_run = false;
124                }
125                m_runnable = null;
126            }
127        }
128        if (LOG.isDebugEnabled()) {
129            LOG.debug(Messages.get().getBundle().key(Messages.LOG_THREAD_SHUTDOWN_1, getName()));
130        }
131    }
132
133    /**
134     * Signal the thread that it should terminate.<p>
135     */
136    void shutdown() {
137
138        m_run = false;
139    }
140}