/* * JBoss, Home of Professional Open Source. * Copyright 2006, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.monitor; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.Set; import java.util.TreeSet; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.naming.Reference; import javax.naming.StringRefAddr; import org.jboss.util.naming.NonSerializableFactory; import org.jboss.system.ServiceMBeanSupport; /** * MBean implementation for providing Locking Stats for EntityBeans * * @author Bill Burke * @author Dimitris Andreadis * @version $Revision: 57209 $ */ public class EntityLockMonitor extends ServiceMBeanSupport implements EntityLockMonitorMBean { // Constants ---------------------------------------------------- public static final String JNDI_NAME = "EntityLockMonitor"; // Protected ----------------------------------------------------- protected HashMap monitorMap = new HashMap(); protected long contenders = 0; protected long maxContenders = 0; protected ArrayList times = new ArrayList(); protected long contentions = 0; protected long totalTime = 0; protected long sumContenders = 0; // Constructors ------------------------------------------------- public EntityLockMonitor() { // empty } // ServiceMBeanSupport overrides --------------------------------- protected void startService() throws Exception { bind(); log.info("EntityLockMonitor started"); } protected void stopService() { try { unbind(); } catch (Exception ignored) {} log.info("EntityLockMonitor stopped"); } // Attributes ---------------------------------------------------- /** * @jmx.managed-attribute */ public synchronized long getAverageContenders() { if (contentions == 0) { return 0; } else { return sumContenders / contentions; } } /** * @jmx.managed-attribute */ public synchronized long getMaxContenders() { return maxContenders; } /** * @jmx.managed-attribute */ public synchronized long getMedianWaitTime() { if (times.size() < 1) { return 0; } Long[] alltimes = (Long[])times.toArray(new Long[times.size()]); long[] thetimes = new long[alltimes.length]; for (int i = 0; i < thetimes.length; i++) { thetimes[i] = alltimes[i].longValue(); } Arrays.sort(thetimes); return thetimes[thetimes.length / 2]; } /** * @jmx.managed-attribute */ public synchronized long getTotalContentions() { return contentions; } // Operations ---------------------------------------------------- /** * @jmx.managed-operation */ public Set listMonitoredBeans() { synchronized(monitorMap) { return new TreeSet(monitorMap.keySet()); } } /** * @jmx.managed-operation * * @return the LockMonitor that corresponds to the jndiName or null */ public LockMonitor getLockMonitor(String jndiName) { synchronized(monitorMap) { return (LockMonitor)monitorMap.get(jndiName); } } /** * @jmx.managed-operation */ public String printLockMonitor() { StringBuffer rtn = new StringBuffer(); rtn.append("
EJB JNDI-NAME | Total Lock Time | Num Contentions | Time Outs | Max Contenders |
"); rtn.append(jndiName); rtn.append(" | "); LockMonitor lm = (LockMonitor)monitorMap.get(jndiName); rtn.append(""); rtn.append(("" + lm.getTotalTime())); rtn.append(" | "); rtn.append(("" + lm.getNumContentions())); rtn.append(" | "); rtn.append(("" + lm.getTimeouts())); rtn.append(" | "); rtn.append(("" + lm.getMaxContenders())); rtn.append(" |