Author: markt Date: Tue May 1 19:56:37 2012 New Revision: 1332801 URL: http://svn.apache.org/viewvc?rev=1332801&view=rev Log: Pull up JMX registration an unregistration
Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java?rev=1332801&r1=1332800&r2=1332801&view=diff ============================================================================== --- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java (original) +++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java Tue May 1 19:56:37 2012 @@ -19,6 +19,7 @@ package org.apache.commons.pool2.impl; import java.io.PrintWriter; import java.io.StringWriter; import java.io.Writer; +import java.lang.management.ManagementFactory; import java.util.ArrayList; import java.util.Deque; import java.util.Iterator; @@ -28,8 +29,14 @@ import java.util.TimerTask; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; +import javax.management.InstanceAlreadyExistsException; +import javax.management.InstanceNotFoundException; import javax.management.ListenerNotFoundException; import javax.management.MBeanNotificationInfo; +import javax.management.MBeanRegistrationException; +import javax.management.MBeanServer; +import javax.management.MalformedObjectNameException; +import javax.management.NotCompliantMBeanException; import javax.management.Notification; import javax.management.NotificationBroadcasterSupport; import javax.management.NotificationEmitter; @@ -100,6 +107,7 @@ public abstract class BaseGenericObjectP // Monitoring (primarily JMX) attributes + private final ObjectName oname; private final NotificationBroadcasterSupport jmxNotificationSupport; private final String creationStackTrace; private final Deque<String> swallowedExceptions = new LinkedList<String>(); @@ -117,11 +125,14 @@ public abstract class BaseGenericObjectP private volatile long maxBorrowWaitTimeMillis = 0; // @GuardedBy("maxBorrowWaitTimeMillisLock") - public BaseGenericObjectPool(BaseObjectPoolConfig config) { + public BaseGenericObjectPool(BaseObjectPoolConfig config, + String jmxNameBase, String jmxNamePrefix) { if (config.getJmxEnabled()) { this.jmxNotificationSupport = new NotificationBroadcasterSupport(); + this.oname = jmxRegister(jmxNameBase, jmxNamePrefix); } else { this.jmxNotificationSupport = null; + this.oname = null; } // Populate the swallowed exceptions queue @@ -629,7 +640,9 @@ public abstract class BaseGenericObjectP * platform MBean server or <code>null</code> if the pool has not been * registered. */ - public abstract ObjectName getJmxName(); + public ObjectName getJmxName() { + return oname; + } /** * Provides the stack trace for the call that created this pool. JMX @@ -750,7 +763,7 @@ public abstract class BaseGenericObjectP String msg = getStackTrace(e); ObjectName oname = getJmxName(); - if (oname != null) { + if (oname != null && !isClosed()) { Notification n = new Notification(NOTIFICATION_SWALLOWED_EXCEPTION, oname, swallowedExcpetionCount.incrementAndGet(), msg); getJmxNotificationSupport().sendNotification(n); @@ -788,6 +801,56 @@ public abstract class BaseGenericObjectP } } + void jmxUnregister() { + if (oname != null) { + try { + ManagementFactory.getPlatformMBeanServer().unregisterMBean( + oname); + } catch (MBeanRegistrationException e) { + swallowException(e); + } catch (InstanceNotFoundException e) { + swallowException(e); + } + } + } + + private ObjectName jmxRegister(String jmxNameBase, String jmxNamePrefix) { + ObjectName objectName = null; + MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); + int i = 1; + boolean registered = false; + while (!registered) { + try { + ObjectName oname = + new ObjectName(jmxNameBase + jmxNamePrefix + i); + mbs.registerMBean(this, oname); + objectName = oname; + registered = true; + } catch (MalformedObjectNameException e) { + if (BaseObjectPoolConfig.DEFAULT_JMX_NAME_PREFIX.equals( + jmxNamePrefix)) { + // Shouldn't happen. Skip registration if it does. + registered = true; + } else { + // Must be an invalid name prefix. Use the default + // instead. + jmxNamePrefix = + BaseObjectPoolConfig.DEFAULT_JMX_NAME_PREFIX; + } + } catch (InstanceAlreadyExistsException e) { + // Increment the index and try again + i++; + } catch (MBeanRegistrationException e) { + // Shouldn't happen. Skip registration if it does. + registered = true; + } catch (NotCompliantMBeanException e) { + // Shouldn't happen. Skip registration if it does. + registered = true; + } + } + return objectName; + } + private String getStackTrace(Exception e) { // Need the exception in string form to prevent the retention of // references to classes in the stack trace that could trigger a memory @@ -901,7 +964,7 @@ public abstract class BaseGenericObjectP try { evict(); } catch(Exception e) { - // Ignored + swallowException(e); } catch(OutOfMemoryError oome) { // Log problem but give evictor thread a chance to continue // in case error is recoverable @@ -911,7 +974,7 @@ public abstract class BaseGenericObjectP try { ensureMinIdle(); } catch (Exception e) { - // Ignored + swallowException(e); } } finally { // Restore the previous CCL Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java?rev=1332801&r1=1332800&r2=1332801&view=diff ============================================================================== --- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java (original) +++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java Tue May 1 19:56:37 2012 @@ -16,7 +16,6 @@ */ package org.apache.commons.pool2.impl; -import java.lang.management.ManagementFactory; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -33,14 +32,6 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; -import javax.management.InstanceAlreadyExistsException; -import javax.management.InstanceNotFoundException; -import javax.management.MBeanRegistrationException; -import javax.management.MBeanServer; -import javax.management.MalformedObjectNameException; -import javax.management.NotCompliantMBeanException; -import javax.management.ObjectName; - import org.apache.commons.pool2.KeyedObjectPool; import org.apache.commons.pool2.KeyedPoolableObjectFactory; import org.apache.commons.pool2.PoolUtils; @@ -216,51 +207,12 @@ public class GenericKeyedObjectPool<K,T> */ public GenericKeyedObjectPool(KeyedPoolableObjectFactory<K,T> factory, GenericKeyedObjectPoolConfig config) { - super(config); + super(config, ONAME_BASE, config.getJmxNamePrefix()); this.factory = factory; setConfig(config); startEvictor(getMinEvictableIdleTimeMillis()); - - ObjectName onameTemp = null; - // JMX Registration - if (config.getJmxEnabled()) { - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - String jmxNamePrefix = config.getJmxNamePrefix(); - int i = 1; - boolean registered = false; - while (!registered) { - try { - ObjectName oname = - new ObjectName(ONAME_BASE + jmxNamePrefix + i); - mbs.registerMBean(this, oname); - onameTemp = oname; - registered = true; - } catch (MalformedObjectNameException e) { - if (GenericObjectPoolConfig.DEFAULT_JMX_NAME_PREFIX.equals( - jmxNamePrefix)) { - // Shouldn't happen. Skip registration if it does. - registered = true; - } else { - // Must be an invalid name prefix. Use the default - // instead. - jmxNamePrefix = - GenericObjectPoolConfig.DEFAULT_JMX_NAME_PREFIX; - } - } catch (InstanceAlreadyExistsException e) { - // Increment the index and try again - i++; - } catch (MBeanRegistrationException e) { - // Shouldn't happen. Skip registration if it does. - registered = true; - } catch (NotCompliantMBeanException e) { - // Shouldn't happen. Skip registration if it does. - registered = true; - } - } - } - this.oname = onameTemp; } /** @@ -814,18 +766,8 @@ public class GenericKeyedObjectPool<K,T> closed = true; // This clear removes any idle objects clear(); - if (oname != null) { - try { - ManagementFactory.getPlatformMBeanServer().unregisterMBean( - oname); - } catch (MBeanRegistrationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (InstanceNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } + + jmxUnregister(); // Release any threads that were waiting for an object Iterator<ObjectDeque<T>> iter = poolMap.values().iterator(); @@ -836,7 +778,6 @@ public class GenericKeyedObjectPool<K,T> // interrupted clear(); } - } @@ -1504,10 +1445,6 @@ public class GenericKeyedObjectPool<K,T> return keyCopy; } - @Override - public ObjectName getJmxName() { - return oname; - } //--- inner classes ---------------------------------------------- @@ -1651,8 +1588,6 @@ public class GenericKeyedObjectPool<K,T> private K evictionKey = null; // @GuardedBy("evictionLock") // JMX specific attributes - private final ObjectName oname; - private static final String ONAME_BASE = "org.apache.commoms.pool2:type=GenericKeyedObjectPool,name="; } Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java?rev=1332801&r1=1332800&r2=1332801&view=diff ============================================================================== --- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java (original) +++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java Tue May 1 19:56:37 2012 @@ -16,21 +16,12 @@ */ package org.apache.commons.pool2.impl; -import java.lang.management.ManagementFactory; import java.util.Map; import java.util.NoSuchElementException; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; -import javax.management.InstanceAlreadyExistsException; -import javax.management.InstanceNotFoundException; -import javax.management.MBeanRegistrationException; -import javax.management.MBeanServer; -import javax.management.MalformedObjectNameException; -import javax.management.NotCompliantMBeanException; -import javax.management.ObjectName; - import org.apache.commons.pool2.ObjectPool; import org.apache.commons.pool2.PoolUtils; import org.apache.commons.pool2.PoolableObjectFactory; @@ -178,51 +169,12 @@ public class GenericObjectPool<T> extend */ public GenericObjectPool(PoolableObjectFactory<T> factory, GenericObjectPoolConfig config) { - super(config); + super(config, ONAME_BASE, config.getJmxNamePrefix()); this.factory = factory; setConfig(config); startEvictor(getTimeBetweenEvictionRunsMillis()); - - ObjectName onameTemp = null; - // JMX Registration - if (config.getJmxEnabled()) { - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - String jmxNamePrefix = config.getJmxNamePrefix(); - int i = 1; - boolean registered = false; - while (!registered) { - try { - ObjectName oname = - new ObjectName(ONAME_BASE + jmxNamePrefix + i); - mbs.registerMBean(this, oname); - onameTemp = oname; - registered = true; - } catch (MalformedObjectNameException e) { - if (GenericObjectPoolConfig.DEFAULT_JMX_NAME_PREFIX.equals( - jmxNamePrefix)) { - // Shouldn't happen. Skip registration if it does. - registered = true; - } else { - // Must be an invalid name prefix. Use the default - // instead. - jmxNamePrefix = - GenericObjectPoolConfig.DEFAULT_JMX_NAME_PREFIX; - } - } catch (InstanceAlreadyExistsException e) { - // Increment the index and try again - i++; - } catch (MBeanRegistrationException e) { - // Shouldn't happen. Skip registration if it does. - registered = true; - } catch (NotCompliantMBeanException e) { - // Shouldn't happen. Skip registration if it does. - registered = true; - } - } - } - this.oname = onameTemp; } /** @@ -666,18 +618,8 @@ public class GenericObjectPool<T> extend closed = true; // This clear removes any idle objects clear(); - if (oname != null) { - try { - ManagementFactory.getPlatformMBeanServer().unregisterMBean( - oname); - } catch (MBeanRegistrationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (InstanceNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } + + jmxUnregister(); // Release any threads that were waiting for an object idleObjects.interuptTakeWaiters(); @@ -903,11 +845,6 @@ public class GenericObjectPool<T> extend } } - @Override - public ObjectName getJmxName() { - return oname; - } - // --- configuration attributes -------------------------------------------- @@ -955,8 +892,6 @@ public class GenericObjectPool<T> extend new LinkedBlockingDeque<PooledObject<T>>(); // JMX specific attributes - private final ObjectName oname; - private static final String ONAME_BASE = "org.apache.commoms.pool2:type=GenericObjectPool,name="; }