1 22 package org.jboss.varia.stats; 23 24 import org.jboss.ejb.plugins.cmp.jdbc2.schema.Cache; 25 import org.jboss.logging.Logger; 26 import org.jboss.system.ServiceMBeanSupport; 27 28 import javax.management.ObjectName ; 29 30 36 public class CacheListener 37 extends ServiceMBeanSupport 38 implements Cache.Listener, CacheListenerMBean 39 { 40 private static final Logger log = Logger.getLogger(CacheListener.class); 41 42 private ObjectName statsCollector; 43 private ObjectName cacheName; 44 private String tableName; 45 46 49 public ObjectName getStatsCollector() 50 { 51 return statsCollector; 52 } 53 54 57 public void setStatsCollector(ObjectName statsCollector) 58 { 59 this.statsCollector = statsCollector; 60 } 61 62 65 public ObjectName getCacheName() 66 { 67 return cacheName; 68 } 69 70 73 public void setCacheName(ObjectName cacheName) 74 { 75 this.cacheName = cacheName; 76 } 77 78 public void startService() throws Exception 79 { 80 tableName = cacheName.getKeyProperty("table"); 81 getServer().invoke(cacheName, "registerListener", new Object []{this}, new String []{Cache.Listener.class.getName()}); 82 } 83 84 86 public void contention(int partitionIndex, long time) 87 { 88 try 89 { 90 StatisticalItem item = new ContentionStats(tableName, partitionIndex, time); 91 server.invoke(statsCollector, "addStatisticalItem", 92 new Object []{item}, 93 new String []{StatisticalItem.class.getName()}); 94 } 95 catch(Exception e) 96 { 97 log.error("Failed to add invocation.", e); 98 } 99 } 100 101 public void eviction(int partitionIndex, Object pk, int size) 102 { 103 try 104 { 105 StatisticalItem item = new EvictionStats(tableName, partitionIndex, pk); 106 server.invoke(statsCollector, "addStatisticalItem", 107 new Object []{item}, 108 new String []{StatisticalItem.class.getName()}); 109 } 110 catch(Exception e) 111 { 112 log.error("Failed to add invocation.", e); 113 } 114 } 115 116 public void hit(int partitionIndex) 117 { 118 try 119 { 120 StatisticalItem item = new HitStats(tableName, partitionIndex); 121 server.invoke(statsCollector, "addStatisticalItem", 122 new Object []{item}, 123 new String []{StatisticalItem.class.getName()}); 124 } 125 catch(Exception e) 126 { 127 log.error("Failed to add invocation.", e); 128 } 129 } 130 131 public void miss(int partitionIndex) 132 { 133 try 134 { 135 StatisticalItem item = new MissStats(tableName, partitionIndex); 136 server.invoke(statsCollector, "addStatisticalItem", 137 new Object []{item}, 138 new String []{StatisticalItem.class.getName()}); 139 } 140 catch(Exception e) 141 { 142 log.error("Failed to add invocation.", e); 143 } 144 } 145 146 public static class HitStats extends AbstractStatisticalItem 147 { 148 public static final String NAME = "Cache Hits Per Transaction"; 149 150 private final String tableName; 151 private final int partitionIndex; 152 153 public HitStats(String name, int partitionIndex) 154 { 155 super(NAME); 156 value = name + partitionIndex; 157 this.tableName = name; 158 this.partitionIndex = partitionIndex; 159 } 160 161 public String getTableName() 162 { 163 return tableName; 164 } 165 166 public int getPartitionIndex() 167 { 168 return partitionIndex; 169 } 170 } 171 172 public static class MissStats extends AbstractStatisticalItem 173 { 174 public static final String NAME = "Cache Misses Per Transaction"; 175 176 public MissStats(String name, int partitionIndex) 177 { 178 super(NAME); 179 value = name; 180 } 181 } 182 183 public static class ContentionStats extends AbstractStatisticalItem 184 { 185 public static final String NAME = "Cache Contention Statistics Per Transaction"; 186 187 private long maxContentionTime; 188 private long contentionTimeTotal; 189 190 public ContentionStats(String name, int partitionIndex, long ms) 191 { 192 super(NAME); 193 value = name; 194 contentionTimeTotal = maxContentionTime = ms; 195 } 196 197 public void merge(StatisticalItem item) 198 { 199 super.merge(item); 200 201 ContentionStats cs = (ContentionStats)item; 202 if(cs.maxContentionTime > maxContentionTime) 203 { 204 maxContentionTime = cs.maxContentionTime; 205 } 206 contentionTimeTotal += cs.contentionTimeTotal; 207 } 208 209 public long getContentionTimeTotal() 210 { 211 return contentionTimeTotal; 212 } 213 214 public long getMaxContentionTime() 215 { 216 return maxContentionTime; 217 } 218 } 219 220 public static class EvictionStats extends AbstractStatisticalItem 221 { 222 public static final String NAME = "Cache Eviction Statistics Per Transaction"; 223 224 private final String tableName; 225 private final Object pk; 226 227 public EvictionStats(String tableName, int partitionIndex, Object pk) 228 { 229 super(NAME); 230 value = tableName; 231 232 this.tableName = tableName; 233 this.pk = pk; 234 } 235 236 public String getTableName() 237 { 238 return tableName; 239 } 240 241 public Object getPk() 242 { 243 return pk; 244 } 245 } 246 } 247 | Popular Tags |