001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: HArrayPoolMonitor.java 9932 2007-01-18 00:03:16Z ehardesty $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.resource.pool.lib;
025:
026: import org.objectweb.jonas.common.Log;
027: import org.objectweb.jonas.resource.pool.api.Pool;
028: import org.objectweb.util.monolog.api.Logger;
029: import org.objectweb.util.monolog.api.BasicLevel;
030:
031: /**
032: * Keeps pool as small as possible (after conn's has reached MaxAge)
033: * @author Eric Hardesty
034: * Contributor(s):
035: */
036: public class HArrayPoolMonitor extends Thread {
037:
038: /**
039: * Pool that is being monitored
040: */
041: private Pool pool;
042: /**
043: * Main logger
044: */
045: private Logger logger = null;
046:
047: /**
048: * Default adjust period of 30 sec
049: */
050: private long adjustPeriod = 30 * 1000L; // default = 30s in ms
051: /**
052: * Default sampling period of 60 sec
053: */
054: private long samplingPeriod = 60 * 1000L; // default = 60s in ms
055: /**
056: * Default validation period of 10 min
057: */
058: private long validationPeriod = 10 * 60 * 1000L; //default = 10 min in ms
059:
060: /**
061: * Adjust period
062: */
063: private long adjustTime = 0;
064: /**
065: * Sampling time
066: */
067: private long samplingTime = 0;
068: /**
069: * Validation time
070: */
071: private long validationTime = 0;
072:
073: private boolean stopped = false;
074:
075: /**
076: * Pool monitor constructor
077: * @param pool Pool to monitor
078: */
079: public HArrayPoolMonitor(Pool pool, String jndiName) {
080: super ("HArrayPoolMonitor-" + jndiName);
081: setDaemon(true);
082: this .pool = pool;
083: logger = Log.getLogger(Log.JONAS_JCA_PREFIX);
084: }
085:
086: /**
087: * Set the adjust period.
088: * Moreover, this can be reconfigured later.
089: * @param sec adjust period in sec.
090: */
091: public void setAdjustPeriod(int sec) {
092: if (logger.isLoggable(BasicLevel.DEBUG)) {
093: logger.log(BasicLevel.DEBUG, " to " + sec);
094: }
095: adjustPeriod = sec * 1000L;
096: }
097:
098: /**
099: * 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: * Stop this thread.
124: */
125: public void stopit() {
126: stopped = true;
127: }
128:
129: /**
130: * Run the array pool monitor
131: */
132: public void run() {
133: long timeout;
134: resetTimes();
135: while (!stopped) {
136: timeout = adjustTime;
137: if (samplingTime < timeout) {
138: timeout = samplingTime;
139: }
140: if (validationTime < timeout) {
141: timeout = validationTime;
142: }
143: try {
144: sleep(timeout);
145: adjustTime -= timeout;
146: samplingTime -= timeout;
147: validationTime -= timeout;
148: if (adjustTime <= 0) {
149: pool.adjust();
150: adjustTime = adjustPeriod;
151: }
152: if (samplingTime <= 0) {
153: pool.sampling();
154: samplingTime = samplingPeriod;
155: }
156: if (validationTime <= 0) {
157: pool.validateMCs();
158: validationTime = validationPeriod;
159: }
160: } catch (Exception e) {
161: logger.log(BasicLevel.ERROR, "HArrayPoolMonitor error",
162: e);
163: resetTimes();
164: }
165: }
166: }
167:
168: private void resetTimes() {
169: adjustTime = adjustPeriod;
170: samplingTime = samplingPeriod;
171: validationTime = validationPeriod;
172: }
173: }
|