KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jotm > TimerManager


1 /*
2  * @(#) TimerManager.java
3  *
4  * JOTM: Java Open Transaction Manager
5  *
6  *
7  * This module was originally developed by
8  *
9  * - Bull S.A. as part of the JOnAS application server code released in
10  * July 1999 (www.bull.com)
11  *
12  * --------------------------------------------------------------------------
13  * The original code and portions created by Bull SA are
14  * Copyright (c) 1999 BULL SA
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions are met:
19  *
20  * -Redistributions of source code must retain the above copyright notice, this
21  * list of conditions and the following disclaimer.
22  *
23  * -Redistributions in binary form must reproduce the above copyright notice,
24  * this list of conditions and the following disclaimer in the documentation
25  * and/or other materials provided with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  *
39  * --------------------------------------------------------------------------
40  * $Id: TimerManager.java,v 1.4 2005/03/15 00:06:05 tonyortiz Exp $
41  * --------------------------------------------------------------------------
42  */

43 package org.objectweb.jotm;
44
45 import java.util.Vector JavaDoc;
46
47 /**
48  * Clock thread for a TimerManager
49  * Every second, decrement timers and launch action if expired
50  */

51 class Clock extends Thread JavaDoc {
52
53     private TimerManager tmgr;
54
55     public Clock(TimerManager tmgr) {
56         super("JotmClock");
57         if (TraceTm.jta.isDebugEnabled()) {
58             TraceTm.jta.debug("Clock constructor");
59         }
60
61         this.tmgr = tmgr;
62     }
63
64     public void run() {
65         tmgr.clock();
66     }
67 }
68
69 /**
70  * Batch thread for a TimerManager
71  * pocess all expired timers
72  */

73 class Batch extends Thread JavaDoc {
74
75     private TimerManager tmgr;
76
77     public Batch(TimerManager tmgr) {
78         super("JotmBatch");
79         if (TraceTm.jta.isDebugEnabled()) {
80             TraceTm.jta.debug("Batch constructor");
81         }
82
83         this.tmgr = tmgr;
84     }
85
86     public void run() {
87         tmgr.batch();
88     }
89 }
90
91 /**
92  * A timer manager manages 2 lists of timers with 2 threads
93  * One thread is a clock which decrements timers every second
94  * and passes them when expired in a list of expired timers.
95  * The other thread looks in the list of expired timers to process
96  * them.
97  */

98 public class TimerManager {
99
100     // threads managing the service.
101
private static Batch batchThread;
102     private static Clock clockThread;
103
104     // lists
105
private Vector JavaDoc timerList = new Vector JavaDoc();
106     private Vector JavaDoc expiredList = new Vector JavaDoc();
107
108     private static TimerManager unique = null;
109     private static boolean shuttingdown = false;
110
111     /**
112      * Constructor
113      */

114     private TimerManager() {
115         // launch threads for timers
116
batchThread = new Batch(this);
117         batchThread.setDaemon(true);
118         batchThread.start();
119         clockThread = new Clock(this);
120         clockThread.setDaemon(true);
121         clockThread.start();
122     }
123
124     /**
125      * Get an instance of the TimerManager
126      */

127     public static TimerManager getInstance() {
128         if (unique == null)
129             unique = new TimerManager();
130         return unique;
131     }
132
133     /**
134      * stop the service
135      * @param force tell the manager NOT to wait for the timers to be completed
136      */

137     public static void stop(boolean force) {
138         if (TraceTm.jta.isDebugEnabled()) {
139             TraceTm.jta.debug("Stop TimerManager");
140         }
141
142         TimerManager tmgr = getInstance();
143         shuttingdown = true;
144         while (clockThread.isAlive() || batchThread.isAlive()) {
145             try {
146                 Thread.sleep(100);
147             } catch (InterruptedException JavaDoc e) {
148                 break;
149             }
150         }
151         if (TraceTm.jta.isDebugEnabled()) {
152             TraceTm.jta.debug("TimerManager has stopped");
153         }
154     }
155
156     public static void stop() {
157         stop(true);
158     }
159
160     /**
161      * cney speed up the clock x1000 when shutting down
162      * update all timers in the list
163      * each timer expired is put in a special list of expired timers
164      * they will be processed then by the Batch Thread.
165      */

166     public void clock() {
167         while (true) {
168             try {
169                 Thread.sleep(shuttingdown?1:1000); // 1 second or 1ms shen shuttingdown
170
// Thread.currentThread().sleep(shuttingdown?1:1000); // 1 second or 1ms shen shuttingdown
171
synchronized(timerList) {
172                     int found = 0;
173                     boolean empty = true;
174                     for (int i = 0; i < timerList.size(); i++) {
175                         TimerEvent t = (TimerEvent) timerList.elementAt(i);
176                         if (!t.isStopped()) {
177                             empty = false;
178                         }
179                         if (t.update() <= 0) {
180                             timerList.removeElementAt(i--);
181                             if (t.valid()) {
182                                 expiredList.addElement(t);
183                                 found++;
184                                 if (t.ispermanent() && !shuttingdown) {
185                                     t.restart();
186                                     timerList.addElement(t);
187                                 }
188                             }
189                         }
190                         // Be sure there is no more ref on bean in this local variable.
191
t = null;
192                     }
193                     if (found > 0) {
194                         timerList.notify();
195                     } else {
196                         if (empty && shuttingdown) {
197                             break;
198                         }
199                     }
200                 }
201             } catch (InterruptedException JavaDoc e) {
202                 TraceTm.jta.error("Timer interrupted");
203             }
204         }
205         synchronized(timerList) { // notify batch so that function can return.
206
timerList.notify();
207         }
208     }
209
210     /**
211      * process all expired timers
212      */

213     public void batch() {
214
215         while (!(shuttingdown && timerList.isEmpty() && expiredList.isEmpty())) {
216             TimerEvent t;
217             synchronized(timerList) {
218                 while (expiredList.isEmpty()) {
219                     if (shuttingdown) return;
220                     try {
221                         timerList.wait();
222                     } catch (Exception JavaDoc e) {
223                         TraceTm.jta.error("Exception in Batch: ", e);
224                     }
225                 }
226                 t = (TimerEvent) expiredList.elementAt(0);
227                 expiredList.removeElementAt(0);
228             }
229             // Do not keep the lock during the processing of the timer
230
t.process();
231         }
232     }
233
234     /**
235      * add a new timer in the list
236      * @param tel Object that will be notified when the timer expire.
237      * @param timeout nb of seconds before the timer expires.
238      * @param arg info passed with the timer
239      * @param permanent true if the timer is permanent.
240      */

241     public TimerEvent addTimer(TimerEventListener tel, long timeout, Object JavaDoc arg, boolean permanent) {
242         TimerEvent te = new TimerEvent(tel, timeout, arg, permanent);
243         synchronized(timerList) {
244             timerList.addElement(te);
245         }
246         return te;
247     }
248
249     /**
250      * remove a timer from the list. this is not very efficient.
251      * A better way to do this is TimerEvent.unset()
252      * @deprecated
253      */

254     public void removeTimer(TimerEvent te) {
255         synchronized(timerList) {
256             timerList.removeElement(te);
257         }
258     }
259 }
260
Popular Tags