1 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 ; 14 15 16 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 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 alias; 47 48 public StatsRoller(String alias, CompositeStatisticsListener compositeStatisticsListener, String 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 Calendar 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 final Thread t = new Thread () { 91 92 public void run() { 93 while (running) { 94 try { 95 Thread.sleep(5000); 96 } catch (InterruptedException e) { 97 LOG.debug("Interruption", e); 98 } 99 roll(); 100 } 101 } 102 103 }; 104 t.setDaemon(true); 105 t.start(); 106 } 107 108 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 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 141 public void connectionReturned(long activeTime) { 142 roll(); 143 try { 144 readWriteLock.readLock().acquire(); 145 currentStatistics.connectionReturned(activeTime); 146 } catch (InterruptedException e) { 147 LOG.error("Unable to log connectionReturned", e); 148 } finally { 149 readWriteLock.readLock().release(); 150 } 151 } 152 153 156 public void connectionRefused() { 157 roll(); 158 try { 159 readWriteLock.readLock().acquire(); 160 currentStatistics.connectionRefused(); 161 } catch (InterruptedException e) { 162 LOG.error("Unable to log connectionRefused", e); 163 } finally { 164 readWriteLock.readLock().release(); 165 } 166 } 167 168 172 public Statistics getCompleteStatistics() { 173 try { 174 readWriteLock.readLock().acquire(); 175 return completeStatistics; 176 } catch (InterruptedException e) { 177 LOG.error("Couldn't read statistics", e); 178 return null; 179 } finally { 180 readWriteLock.readLock().release(); 181 } 182 } 183 } 184 185 186 | Popular Tags |