KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > resource > pool > lib > HArrayPoolMonitor


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: HArrayPoolMonitor.java,v 1.6 2005/04/28 08:43:25 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.resource.pool.lib;
27
28 import org.objectweb.jonas.common.Log;
29 import org.objectweb.jonas.resource.pool.api.Pool;
30 import org.objectweb.util.monolog.api.Logger;
31 import org.objectweb.util.monolog.api.BasicLevel;
32
33 /**
34  * Keeps pool as small as possible (after conn's has reached MaxAge)
35  * @author Eric Hardesty
36  * Contributor(s):
37  */

38 public class HArrayPoolMonitor extends Thread JavaDoc {
39
40     /**
41      * Pool that is being monitored
42      */

43     private Pool pool;
44     /**
45      * Main logger
46      */

47     private Logger logger = null;
48
49     /**
50      * Default adjust period of 15 sec
51      */

52     private long adjustPeriod = 15 * 1000L; // default = 15s in ms
53
/**
54      * Default sampling period of 60 sec
55      */

56     private long samplingPeriod = 60 * 1000L; // default = 60s in ms
57
/**
58      * Default validation period of 10 min
59      */

60     private long validationPeriod = 10 * 60 * 1000L; //default = 10 min in ms
61

62     /**
63      * Adjust period
64      */

65     private long adjustTime = 0;
66     /**
67      * Sampling time
68      */

69     private long samplingTime = 0;
70     /**
71      * Validation time
72      */

73     private long validationTime = 0;
74
75     /**
76      * Pool monitor constructor
77      * @param pool Pool to monitor
78      */

79     public HArrayPoolMonitor(Pool pool) {
80         super("HArrayPoolMonitor");
81         setDaemon(true);
82         this.pool = pool;
83         logger = Log.getLogger(Log.JONAS_JCA_PREFIX);
84     }
85
86     /**
87      * Set the adjust period.
88      * Moreover, this can be reconfigured later.
89      * @param sec adjust period in sec.
90      */

91     public void setAdjustPeriod(int sec) {
92         if (logger.isLoggable(BasicLevel.DEBUG)) {
93             logger.log(BasicLevel.DEBUG, " to " + sec);
94         }
95         adjustPeriod = sec * 1000L;
96     }
97
98     /**
99      * Set the sampling period.
100      * Moreover, this can be reconfigured later.
101      * @param sec sampling period in sec.
102      */

103     public void setSamplingPeriod(int sec) {
104         if (logger.isLoggable(BasicLevel.DEBUG)) {
105             logger.log(BasicLevel.DEBUG, " to " + sec);
106         }
107         samplingPeriod = sec * 1000L;
108     }
109
110     /**
111      * Set the validation period.
112      * Moreover, this can be reconfigured later.
113      * @param sec validation period in sec.
114      */

115     public void setValidationPeriod(int sec) {
116         if (logger.isLoggable(BasicLevel.DEBUG)) {
117             logger.log(BasicLevel.DEBUG, " to " + sec);
118         }
119         validationPeriod = sec * 1000L;
120     }
121
122     /**
123      * Run the array pool monitor
124      */

125     public void run() {
126         long timeout;
127         resetTimes();
128         while (true) {
129             timeout = adjustTime;
130             if (samplingTime < timeout) {
131                 timeout = samplingTime;
132             }
133             if (validationTime < timeout) {
134                 timeout = validationTime;
135             }
136             try {
137                 sleep(timeout);
138                 adjustTime -= timeout;
139                 samplingTime -= timeout;
140                 validationTime -= timeout;
141                 if (adjustTime <= 0) {
142                     pool.adjust();
143                     adjustTime = adjustPeriod;
144                 }
145                 if (samplingTime <= 0) {
146                     pool.sampling();
147                     samplingTime = samplingPeriod;
148                 }
149                 if (validationTime <= 0) {
150                     pool.validateMCs();
151                     validationTime = validationPeriod;
152                 }
153             } catch (NullPointerException JavaDoc e) {
154                 logger.log(BasicLevel.ERROR, "HArrayPoolMonitor NPE:" + e);
155                 e.printStackTrace();
156                 resetTimes();
157             } catch (Exception JavaDoc e) {
158                 logger.log(BasicLevel.ERROR, "HArrayPoolMonitor error:" + e);
159                 e.printStackTrace();
160                 resetTimes();
161             }
162         }
163     }
164
165     private void resetTimes() {
166         adjustTime = adjustPeriod;
167         samplingTime = samplingPeriod;
168         validationTime = validationPeriod;
169     }
170 }
171
Popular Tags