KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > admin > StatsRoller


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool.admin;
7
8 import org.logicalcobwebs.concurrent.WriterPreferenceReadWriteLock;
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.logicalcobwebs.proxool.ProxoolException;
12
13 import java.util.Calendar JavaDoc;
14
15
16 /**
17  * Responsbile for a single set of statistics. It rolls over to a new set
18  * whenever it should. It provides access to the latest complete set
19  * when it is available.
20  *
21  * @version $Revision: 1.9 $, $Date: 2006/01/18 14:39:58 $
22  * @author bill
23  * @author $Author: billhorsman $ (current maintainer)
24  * @since Proxool 0.7
25  */

26 class StatsRoller {
27
28     private static final Log LOG = LogFactory.getLog(StatsRoller.class);
29
30     private WriterPreferenceReadWriteLock readWriteLock = new WriterPreferenceReadWriteLock();
31
32     private Statistics completeStatistics;
33
34     private Statistics currentStatistics;
35
36     private Calendar JavaDoc nextRollDate;
37
38     private int period;
39
40     private int units;
41
42     private boolean running = true;
43
44     private CompositeStatisticsListener compositeStatisticsListener;
45
46     private String JavaDoc alias;
47
48     public StatsRoller(String JavaDoc alias, CompositeStatisticsListener compositeStatisticsListener, String JavaDoc token) throws ProxoolException {
49         this.alias = alias;
50         this.compositeStatisticsListener = compositeStatisticsListener;
51
52         nextRollDate = Calendar.getInstance();
53         if (token.endsWith("s")) {
54             units = Calendar.SECOND;
55             nextRollDate.clear(Calendar.SECOND);
56             nextRollDate.clear(Calendar.MILLISECOND);
57         } else if (token.endsWith("m")) {
58             units = Calendar.MINUTE;
59             nextRollDate.clear(Calendar.MINUTE);
60             nextRollDate.clear(Calendar.SECOND);
61             nextRollDate.clear(Calendar.MILLISECOND);
62         } else if (token.endsWith("h")) {
63             nextRollDate.clear(Calendar.HOUR_OF_DAY);
64             nextRollDate.clear(Calendar.MINUTE);
65             nextRollDate.clear(Calendar.SECOND);
66             nextRollDate.clear(Calendar.MILLISECOND);
67             units = Calendar.HOUR_OF_DAY;
68         } else if (token.endsWith("d")) {
69             units = Calendar.DATE;
70             nextRollDate.clear(Calendar.HOUR_OF_DAY);
71             nextRollDate.clear(Calendar.MINUTE);
72             nextRollDate.clear(Calendar.SECOND);
73             nextRollDate.clear(Calendar.MILLISECOND);
74         } else {
75             throw new ProxoolException("Unrecognised suffix in statistics: " + token);
76         }
77
78         period = Integer.parseInt(token.substring(0, token.length() - 1));
79
80         // Now roll forward until you get one step into the future
81
Calendar JavaDoc now = Calendar.getInstance();
82         while (nextRollDate.before(now)) {
83             nextRollDate.add(units, period);
84         }
85
86         LOG.debug("Collecting first statistics for '" + token + "' at " + nextRollDate.getTime());
87         currentStatistics = new Statistics(now.getTime());
88
89         // Automatically trigger roll if no activity
90
final Thread JavaDoc t = new Thread JavaDoc() {
91
92             public void run() {
93                 while (running) {
94                     try {
95                         Thread.sleep(5000);
96                     } catch (InterruptedException JavaDoc e) {
97                         LOG.debug("Interruption", e);
98                     }
99                     roll();
100                 }
101             }
102
103         };
104         t.setDaemon(true);
105         t.start();
106     }
107
108     /**
109      * Cancels the timer that outputs the stats
110      */

111     protected void cancel() {
112         running = false;
113     }
114
115     private void roll() {
116         if (!isCurrent()) {
117             try {
118                 readWriteLock.writeLock().acquire();
119                 if (!isCurrent()) {
120                     currentStatistics.setStopDate(nextRollDate.getTime());
121                     completeStatistics = currentStatistics;
122                     currentStatistics = new Statistics(nextRollDate.getTime());
123                     nextRollDate.add(units, period);
124                     compositeStatisticsListener.statistics(alias, completeStatistics);
125                 }
126             } catch (Throwable JavaDoc e) {
127                 LOG.error("Unable to roll statistics log", e);
128             } finally {
129                 readWriteLock.writeLock().release();
130             }
131         }
132     }
133
134     private boolean isCurrent() {
135         return (System.currentTimeMillis() < nextRollDate.getTime().getTime());
136     }
137
138     /**
139      * @see org.logicalcobwebs.proxool.admin.Admin#connectionReturned
140      */

141     public void connectionReturned(long activeTime) {
142         roll();
143         try {
144             readWriteLock.readLock().acquire();
145             currentStatistics.connectionReturned(activeTime);
146         } catch (InterruptedException JavaDoc e) {
147             LOG.error("Unable to log connectionReturned", e);
148         } finally {
149             readWriteLock.readLock().release();
150         }
151     }
152
153     /**
154      * @see org.logicalcobwebs.proxool.admin.Admin#connectionRefused
155      */

156     public void connectionRefused() {
157         roll();
158         try {
159             readWriteLock.readLock().acquire();
160             currentStatistics.connectionRefused();
161         } catch (InterruptedException JavaDoc e) {
162             LOG.error("Unable to log connectionRefused", e);
163         } finally {
164             readWriteLock.readLock().release();
165         }
166     }
167
168     /**
169      *
170      * @return
171      */

172     public Statistics getCompleteStatistics() {
173         try {
174             readWriteLock.readLock().acquire();
175             return completeStatistics;
176         } catch (InterruptedException JavaDoc e) {
177             LOG.error("Couldn't read statistics", e);
178             return null;
179         } finally {
180             readWriteLock.readLock().release();
181         }
182     }
183 }
184
185
186 /*
187  Revision history:
188  $Log: StatsRoller.java,v $
189  Revision 1.9 2006/01/18 14:39:58 billhorsman
190  Unbundled Jakarta's Commons Logging.
191
192  Revision 1.8 2003/10/27 20:24:48 billhorsman
193  roll() now makes an additional call to isCurrent() *before* it asks for a write lock. Before it
194  was getting a write lock every five seconds which effectively blocks all connections (if only briefly).
195
196  Revision 1.7 2003/09/10 22:21:04 chr32
197  Removing > jdk 1.2 dependencies.
198
199  Revision 1.6 2003/03/11 00:12:11 billhorsman
200  switch to concurrent package
201
202  Revision 1.5 2003/03/06 21:56:27 billhorsman
203  remove too much debug
204
205  Revision 1.4 2003/03/06 12:44:02 billhorsman
206  add readWriteLock
207
208  Revision 1.3 2003/03/03 11:11:59 billhorsman
209  fixed licence
210
211  Revision 1.2 2003/02/28 12:42:45 billhorsman
212  removed unnecessary sleep in timer
213
214  Revision 1.1 2003/02/19 23:36:51 billhorsman
215  renamed monitor package to admin
216
217  Revision 1.11 2003/02/08 14:27:51 chr32
218  Style fixes.
219  Also tried to fix the dublicate linebreaks in the logging classes.
220
221  Revision 1.10 2003/02/07 14:16:46 billhorsman
222  support for StatisticsListenerIF
223
224  Revision 1.9 2003/02/06 17:41:06 billhorsman
225  now uses imported logging
226
227  Revision 1.8 2003/02/06 15:41:18 billhorsman
228  add statistics-log-level
229
230  Revision 1.7 2003/02/04 17:17:03 billhorsman
231  make Timer a daemon
232
233  Revision 1.6 2003/02/04 15:59:49 billhorsman
234  finalize now shuts down StatsRoller timer
235
236  Revision 1.5 2003/02/02 23:32:48 billhorsman
237  fixed bug caused by last variable name change. :(
238
239  Revision 1.4 2003/01/31 16:53:23 billhorsman
240  checkstyle
241
242  Revision 1.3 2003/01/31 16:38:54 billhorsman
243  doc (and removing public modifier for classes where possible)
244
245  Revision 1.2 2003/01/31 14:33:19 billhorsman
246  fix for DatabaseMetaData
247
248  Revision 1.1 2003/01/31 11:35:57 billhorsman
249  improvements to servlet (including connection details)
250
251  Revision 1.1 2003/01/31 00:28:57 billhorsman
252  now handles multiple statistics
253
254  */
Popular Tags