1 22 package org.jboss.monitor; 23 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Set ; 29 import java.util.TreeSet ; 30 31 import javax.naming.Context ; 32 import javax.naming.InitialContext ; 33 import javax.naming.NamingException ; 34 import javax.naming.Reference ; 35 import javax.naming.StringRefAddr ; 36 37 import org.jboss.naming.NonSerializableFactory; 38 import org.jboss.system.ServiceMBeanSupport; 39 40 47 public class EntityLockMonitor extends ServiceMBeanSupport 48 implements EntityLockMonitorMBean 49 { 50 52 public static final String JNDI_NAME = "EntityLockMonitor"; 53 54 56 protected HashMap monitorMap = new HashMap (); 57 protected long contenders = 0; 58 protected long maxContenders = 0; 59 protected ArrayList times = new ArrayList (); 60 protected long contentions = 0; 61 protected long totalTime = 0; 62 protected long sumContenders = 0; 63 64 66 public EntityLockMonitor() 67 { 68 } 70 71 73 protected void startService() 74 throws Exception 75 { 76 bind(); 77 78 log.info("EntityLockMonitor started"); 79 } 80 81 protected void stopService() { 82 try 83 { 84 unbind(); 85 } 86 catch (Exception ignored) {} 87 88 log.info("EntityLockMonitor stopped"); 89 } 90 91 93 96 public synchronized long getAverageContenders() 97 { 98 if (contentions == 0) 99 { 100 return 0; 101 } 102 else 103 { 104 return sumContenders / contentions; 105 } 106 } 107 108 111 public synchronized long getMaxContenders() 112 { 113 return maxContenders; 114 } 115 116 119 public synchronized long getMedianWaitTime() 120 { 121 if (times.size() < 1) 122 { 123 return 0; 124 } 125 126 Long [] alltimes = (Long [])times.toArray(new Long [times.size()]); 127 long[] thetimes = new long[alltimes.length]; 128 for (int i = 0; i < thetimes.length; i++) 129 { 130 thetimes[i] = alltimes[i].longValue(); 131 } 132 Arrays.sort(thetimes); 133 return thetimes[thetimes.length / 2]; 134 } 135 136 139 public synchronized long getTotalContentions() 140 { 141 return contentions; 142 } 143 144 146 149 public Set listMonitoredBeans() 150 { 151 synchronized(monitorMap) 152 { 153 return new TreeSet (monitorMap.keySet()); 154 } 155 } 156 157 162 public LockMonitor getLockMonitor(String jndiName) 163 { 164 synchronized(monitorMap) 165 { 166 return (LockMonitor)monitorMap.get(jndiName); 167 } 168 } 169 170 173 public String printLockMonitor() 174 { 175 StringBuffer rtn = new StringBuffer (); 176 rtn.append("<table width=\"1\" border=\"1\">"); 177 rtn.append("<tr><td><b>EJB JNDI-NAME</b></td><td><b>Total Lock Time</b></td><td><b>Num Contentions</b></td><td><b>Time Outs</b></td><td><b>Max Contenders</b></td></tr>"); 178 synchronized(monitorMap) 179 { 180 Iterator it = monitorMap.keySet().iterator(); 181 while (it.hasNext()) 182 { 183 rtn.append("<tr>"); 184 String jndiName = (String )it.next(); 185 rtn.append("<td>"); 186 rtn.append(jndiName); 187 rtn.append("</td>"); 188 LockMonitor lm = (LockMonitor)monitorMap.get(jndiName); 189 rtn.append("<td>"); 190 rtn.append(("" + lm.getTotalTime())); 191 rtn.append("</td><td>"); 192 rtn.append(("" + lm.getNumContentions())); 193 rtn.append("</td><td>"); 194 rtn.append(("" + lm.getTimeouts())); 195 rtn.append("</td><td>"); 196 rtn.append(("" + lm.getMaxContenders())); 197 rtn.append("</td></tr>"); 198 } 199 } 200 rtn.append("</table>"); 201 return rtn.toString(); 202 } 203 204 207 public synchronized void clearMonitor() 208 { 209 contenders = 0; 210 maxContenders = 0; 211 times.clear(); 212 contentions = 0; 213 totalTime = 0; 214 sumContenders = 0; 215 216 synchronized(monitorMap) 217 { 218 Iterator it = monitorMap.keySet().iterator(); 219 while (it.hasNext()) 220 { 221 String jndiName = (String )it.next(); 222 LockMonitor lm = (LockMonitor)monitorMap.get(jndiName); 223 lm.reset(); 224 } 225 } 226 } 227 228 230 public synchronized void incrementContenders() 231 { 232 ++contenders; 233 ++contentions; 234 sumContenders += contenders; 235 236 if (contenders > maxContenders) 237 { 238 maxContenders = contenders; 239 } 240 } 241 242 public synchronized void decrementContenders(long time) 243 { 244 times.add(new Long (time)); 245 --contenders; 246 } 247 248 public LockMonitor getEntityLockMonitor(String jndiName) 249 { 250 LockMonitor lm = null; 251 252 synchronized(monitorMap) 253 { 254 lm = (LockMonitor)monitorMap.get(jndiName); 255 if (lm == null) 256 { 257 lm = new LockMonitor(this); 258 monitorMap.put(jndiName, lm); 259 } 260 } 261 return lm; 262 } 263 264 266 private void bind() throws NamingException 267 { 268 Context ctx = new InitialContext (); 269 270 NonSerializableFactory.bind(JNDI_NAME, this); 272 273 StringRefAddr addr = new StringRefAddr ("nns", JNDI_NAME); 276 Reference ref = new Reference (EntityLockMonitor.class.getName(), addr, NonSerializableFactory.class.getName(), null); 277 ctx.bind(JNDI_NAME, ref); 278 } 279 280 private void unbind() throws NamingException 281 { 282 new InitialContext ().unbind(JNDI_NAME); 283 NonSerializableFactory.unbind(JNDI_NAME); 284 } 285 286 } | Popular Tags |