Source code: com/opencms/core/CmsCronScheduler.java
1 /*
2 * File : $Source: /usr/local/cvs/opencms/src/com/opencms/core/Attic/CmsCronScheduler.java,v $
3 * Date : $Date: 2003/03/28 11:15:32 $
4 * Version: $Revision: 1.4 $
5 *
6 * This library is part of OpenCms -
7 * the Open Source Content Mananagement System
8 *
9 * Copyright (C) 2001 The OpenCms Group
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 OpenCms, please see the
22 * OpenCms Website: http://www.opencms.org
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */
28
29 package com.opencms.core;
30
31 import com.opencms.boot.I_CmsLogChannels;
32
33 import java.util.*;
34
35 /**
36 * This is the CronScheduler. It a deamon-thread that will be stopped
37 * after the system has stopped. It awaiks every minute and starts
38 * cronjobs at issue.
39 */
40 class CmsCronScheduler extends Thread {
41
42 /** The crontable to use */
43 private CmsCronTable m_table;
44
45 /** The A_OpenCms to get access to the system */
46 private A_OpenCms m_opencms;
47
48 /** Flag to indicate if OpenCms has already been shut down */
49 private boolean m_destroyed = false;
50
51 /**
52 * Constructs a new scheduler.
53 * @param opencms to get access to A_OpenCms
54 * @param table the CmsCronTable with all CmsCronEntry's to launch.
55 */
56 CmsCronScheduler(A_OpenCms opencms, CmsCronTable table) {
57 super();
58 // store the crontable
59 m_table = table;
60 // store the OpenCms to get access to the system
61 m_opencms = opencms;
62
63 // set to deamon thread - so java will stop correctly
64 setDaemon(true);
65 // start the thread
66 start();
67 }
68
69 /**
70 * The run-method of this thread awakes every minute to launch jobs at issue.
71 */
72 public void run() {
73 Calendar lastRun = new GregorianCalendar();
74 Calendar thisRun;
75 CmsCronScheduleJobStarter jobStarter;
76 while(! m_destroyed) { // do this as long as OpenCms runs
77 try {
78 Calendar tmp = new GregorianCalendar(lastRun.get(GregorianCalendar.YEAR),
79 lastRun.get(GregorianCalendar.MONTH),
80 lastRun.get(GregorianCalendar.DATE),
81 lastRun.get(GregorianCalendar.HOUR_OF_DAY),
82 lastRun.get(GregorianCalendar.MINUTE)+1,
83 0);
84 long sleeptime = tmp.getTime().getTime() - new GregorianCalendar().getTime().getTime();
85 if(sleeptime > 0) {
86 // sleep til next minute plus ten seconds to get to the next minute
87 sleep(sleeptime + 10000);
88 }
89 } catch(InterruptedException exc) {
90 // ignore this exception - we are interrupted
91 }
92 if(! m_destroyed) {
93 // if not destroyed, read the current values for the crontable from the system
94 m_opencms.updateCronTable();
95 thisRun = new GregorianCalendar();
96 jobStarter = new CmsCronScheduleJobStarter(m_opencms, m_table, thisRun, lastRun);
97 jobStarter.start();
98 lastRun = thisRun;
99 }
100 }
101 }
102
103 /**
104 * Shut down this instance of the CronScheduler Thread.<p>
105 */
106 public void shutDown() {
107 interrupt();
108 m_destroyed = true;
109 if(I_CmsLogChannels.C_LOGGING && A_OpenCms.isLogging(I_CmsLogChannels.C_OPENCMS_INIT)) {
110 A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_INIT, "[CmsCronScheduler] Destroyed");
111 }
112 }
113 }