KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > dbm > PoolKeeper


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: PoolKeeper.java,v 1.15 2005/06/30 14:50:07 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

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

36 public class PoolKeeper extends Thread JavaDoc {
37
38     /**
39      * Pool object to monitor
40      */

41     private Pool pool;
42
43     /**
44      * Logger object to log events
45      */

46     private Logger logger = null;
47
48     /**
49      * Default adjustment period of 15 seconds
50      */

51     private long adjustperiod = 15000L; // default = 15s
52

53     /**
54      * Default sampling period of 60 seconds
55      */

56     private long samplingperiod = 60000L; // default = 60s
57

58     /**
59      * time to wait on error
60      */

61     private final long errorperiod = 120000L; // 2 mn
62

63     /**
64      * Constructor
65      * @param pool Pool to monitor
66      */

67     public PoolKeeper(Pool pool, Logger log) {
68         super("PoolKeeper");
69         setDaemon(true);
70         this.pool = pool;
71         logger = log;
72     }
73
74     /**
75      * Set the sampling period.
76      * This cannot be done in the PoolKeeper constructor, because the
77      * sampling period has not been set already in the Pool.
78      * Moreover, this can be reconfigured later.
79      * @param sec sampling period in sec.
80      */

81     public void setSamplingPeriod(int sec) {
82         if (logger.isLoggable(BasicLevel.DEBUG)) {
83             logger.log(BasicLevel.DEBUG, " to " + sec);
84         }
85         samplingperiod = sec * 1000L;
86     }
87
88     /**
89      * Start the pool keeper thread
90      */

91     public void run() {
92         long timeout;
93         long adjusttime = adjustperiod;
94         long samplingtime = samplingperiod;
95         while (true) {
96             timeout = adjusttime;
97             if (samplingtime < timeout) {
98                 timeout = samplingtime;
99             }
100             try {
101                 sleep(timeout);
102                 adjusttime -= timeout;
103                 samplingtime -= timeout;
104                 if (adjusttime <= 0) {
105                     pool.adjust();
106                     adjusttime = adjustperiod;
107                 }
108                 if (samplingtime <= 0) {
109                     pool.sampling();
110                     samplingtime = samplingperiod;
111                 }
112             } catch (NullPointerException JavaDoc e) {
113                 logger.log(BasicLevel.ERROR, "poolkeeper NPE", e);
114                 e.printStackTrace();
115                 adjusttime = errorperiod;
116                 samplingtime = errorperiod;
117             } catch (Exception JavaDoc e) {
118                 logger.log(BasicLevel.ERROR, "poolkeeper error", e);
119                 e.printStackTrace();
120                 adjusttime = errorperiod;
121                 samplingtime = errorperiod;
122             }
123         }
124     }
125 }
126
Popular Tags