00001
00026 package org.objectweb.jonas.resource.pool.lib;
00027
00028 import org.objectweb.jonas.common.Log;
00029 import org.objectweb.jonas.resource.pool.api.Pool;
00030 import org.objectweb.util.monolog.api.Logger;
00031 import org.objectweb.util.monolog.api.BasicLevel;
00032
00038 public class HArrayPoolMonitor extends Thread {
00039
00043 private Pool pool;
00047 private Logger logger = null;
00048
00052 private long adjustPeriod = 15 * 1000L;
00056 private long samplingPeriod = 60 * 1000L;
00060 private long validationPeriod = 10 * 60 * 1000L;
00061
00065 private long adjustTime = 0;
00069 private long samplingTime = 0;
00073 private long validationTime = 0;
00074
00079 public HArrayPoolMonitor(Pool pool) {
00080 super("HArrayPoolMonitor");
00081 setDaemon(true);
00082 this.pool = pool;
00083 logger = Log.getLogger(Log.JONAS_JCA_PREFIX);
00084 }
00085
00091 public void setAdjustPeriod(int sec) {
00092 logger.log(BasicLevel.DEBUG, " to " + sec);
00093 adjustPeriod = sec * 1000L;
00094 }
00095
00101 public void setSamplingPeriod(int sec) {
00102 logger.log(BasicLevel.DEBUG, " to " + sec);
00103 samplingPeriod = sec * 1000L;
00104 }
00105
00111 public void setValidationPeriod(int sec) {
00112 logger.log(BasicLevel.DEBUG, " to " + sec);
00113 validationPeriod = sec * 1000L;
00114 }
00115
00119 public void run() {
00120 long timeout;
00121 resetTimes();
00122 while (true) {
00123 timeout = adjustTime;
00124 if (samplingTime < timeout) {
00125 timeout = samplingTime;
00126 }
00127 if (validationTime < timeout) {
00128 timeout = validationTime;
00129 }
00130 try {
00131 sleep(timeout);
00132 adjustTime -= timeout;
00133 samplingTime -= timeout;
00134 validationTime -= timeout;
00135 if (adjustTime <= 0) {
00136 pool.adjust();
00137 adjustTime = adjustPeriod;
00138 }
00139 if (samplingTime <= 0) {
00140 pool.sampling();
00141 samplingTime = samplingPeriod;
00142 }
00143 if (validationTime <= 0) {
00144 pool.validateMCs();
00145 validationTime = validationPeriod;
00146 }
00147 } catch (NullPointerException e) {
00148 logger.log(BasicLevel.ERROR, "HArrayPoolMonitor NPE:" + e);
00149 e.printStackTrace();
00150 resetTimes();
00151 } catch (Exception e) {
00152 logger.log(BasicLevel.ERROR, "HArrayPoolMonitor error:" + e);
00153 e.printStackTrace();
00154 resetTimes();
00155 }
00156 }
00157 }
00158
00159 private void resetTimes() {
00160 adjustTime = adjustPeriod;
00161 samplingTime = samplingPeriod;
00162 validationTime = validationPeriod;
00163 }
00164 }