Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 4CF5B7E87 for ; Wed, 17 Aug 2011 14:29:08 +0000 (UTC) Received: (qmail 1511 invoked by uid 500); 17 Aug 2011 14:29:07 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 1216 invoked by uid 500); 17 Aug 2011 14:29:07 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 1035 invoked by uid 99); 17 Aug 2011 14:29:06 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 17 Aug 2011 14:29:06 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 17 Aug 2011 14:29:02 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id CA46A238897D for ; Wed, 17 Aug 2011 14:28:40 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1158736 - in /commons/proper/pool/trunk/src: changes/ java/org/apache/commons/pool2/impl/ Date: Wed, 17 Aug 2011 14:28:40 -0000 To: commits@commons.apache.org From: markt@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110817142840.CA46A238897D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: markt Date: Wed Aug 17 14:28:40 2011 New Revision: 1158736 URL: http://svn.apache.org/viewvc?rev=1158736&view=rev Log: Expose GOP and GKOP attributes over JMX. Added: commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPoolMBean.java (with props) commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericObjectPoolMBean.java (with props) Modified: commons/proper/pool/trunk/src/changes/changes.xml commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericObjectPool.java Modified: commons/proper/pool/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/changes/changes.xml?rev=1158736&r1=1158735&r2=1158736&view=diff ============================================================================== --- commons/proper/pool/trunk/src/changes/changes.xml (original) +++ commons/proper/pool/trunk/src/changes/changes.xml Wed Aug 17 14:28:40 2011 @@ -74,6 +74,9 @@ Re-factor common code into common base classes. + + Expose GOP and GKOP attributes via JMX. + Modified: commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java?rev=1158736&r1=1158735&r2=1158736&view=diff ============================================================================== --- commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java (original) +++ commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java Wed Aug 17 14:28:40 2011 @@ -75,7 +75,15 @@ public abstract class BaseObjectPoolConf * The default "block when exhausted" value for the pool. */ public static final boolean DEFAULT_BLOCK_WHEN_EXHAUSTED = true; - + + public static final boolean DEFAULT_JMX_ENABLE = true; + + /** + * The default prefix to use for the name component of the JMX object name + * under which the pool will be registered. + */ + public static final String DEFAULT_JMX_NAME_PREFIX = "pool"; + private boolean lifo = DEFAULT_LIFO; private long maxWait = DEFAULT_MAX_WAIT; @@ -97,6 +105,10 @@ public abstract class BaseObjectPoolConf private boolean blockWhenExhausted = DEFAULT_BLOCK_WHEN_EXHAUSTED; + private boolean jmxEnabled = DEFAULT_JMX_ENABLE; + + private String jmxNamePrefix = DEFAULT_JMX_NAME_PREFIX; + public boolean getLifo() { return lifo; } @@ -168,4 +180,20 @@ public abstract class BaseObjectPoolConf public void setBlockWhenExhausted(boolean blockWhenExhausted) { this.blockWhenExhausted = blockWhenExhausted; } + + public boolean isJmxEnabled() { + return jmxEnabled; + } + + public void setJmxEnabled(boolean jmxEnabled) { + this.jmxEnabled = jmxEnabled; + } + + public String getJmxNamePrefix() { + return jmxNamePrefix; + } + + public void setJmxNamePrefix(String jmxNamePrefix) { + this.jmxNamePrefix = jmxNamePrefix; + } } Modified: commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java?rev=1158736&r1=1158735&r2=1158736&view=diff ============================================================================== --- commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java (original) +++ commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java Wed Aug 17 14:28:40 2011 @@ -17,7 +17,9 @@ package org.apache.commons.pool2.impl; +import java.lang.management.ManagementFactory; import java.util.ArrayList; +import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -33,6 +35,13 @@ 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.MBeanRegistrationException; +import javax.management.MBeanServer; +import javax.management.MalformedObjectNameException; +import javax.management.NotCompliantMBeanException; +import javax.management.ObjectName; + import org.apache.commons.pool2.BaseKeyedObjectPool; import org.apache.commons.pool2.KeyedPoolableObjectFactory; import org.apache.commons.pool2.PoolUtils; @@ -200,7 +209,8 @@ import org.apache.commons.pool2.PoolUtil * @version $Revision$ $Date$ * @since Pool 1.0 */ -public class GenericKeyedObjectPool extends BaseKeyedObjectPool { +public class GenericKeyedObjectPool extends BaseKeyedObjectPool + implements GenericKeyedObjectPoolMBean { //--- constructors ----------------------------------------------- @@ -242,6 +252,42 @@ public class GenericKeyedObjectPool this.blockWhenExhausted = config.getBlockWhenExhausted(); startEvictor(getMinEvictableIdleTimeMillis()); + + // JMX Registration + if (config.isJmxEnabled()) { + 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); + 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; + } + } + } } //--- configuration methods -------------------------------------- @@ -1508,6 +1554,27 @@ public class GenericKeyedObjectPool return objectDefecit; } + + //--- JMX specific attributes ---------------------------------------------- + public Map getNumActivePerKey() { + HashMap result = new HashMap(); + + Iterator>> iter = poolMap.entrySet().iterator(); + while (iter.hasNext()) { + Entry> entry = iter.next(); + if (entry != null) { + K key = entry.getKey(); + ObjectDeque objectDequeue = entry.getValue(); + if (key != null && objectDequeue != null) { + result.put(key.toString(), Integer.valueOf( + objectDequeue.getAllObjects().size() - + objectDequeue.getIdleObjects().size())); + } + } + } + return result; + } + //--- inner classes ---------------------------------------------- /** @@ -1758,4 +1825,7 @@ public class GenericKeyedObjectPool * currently being evicted. */ private K evictionKey = null; + + private static final String ONAME_BASE = + "org.apache.commoms.pool2:type=GenericKeyedObjectPool,name="; } Added: commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPoolMBean.java URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPoolMBean.java?rev=1158736&view=auto ============================================================================== --- commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPoolMBean.java (added) +++ commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPoolMBean.java Wed Aug 17 14:28:40 2011 @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.pool2.impl; + +import java.util.Map; + +public interface GenericKeyedObjectPoolMBean { + // Expose standard attributes via JMX + boolean getBlockWhenExhausted(); + boolean getLifo(); + int getMaxIdlePerKey(); + int getMaxTotal(); + int getMaxTotalPerKey(); + long getMaxWait(); + long getMinEvictableIdleTimeMillis(); + int getMinIdlePerKey(); + int getNumActive(); + int getNumIdle(); + int getNumTestsPerEvictionRun(); + boolean getTestOnBorrow(); + boolean getTestOnReturn(); + boolean getTestWhileIdle(); + long getTimeBetweenEvictionRunsMillis(); + boolean isClosed(); + // JMX specific attributes + Map getNumActivePerKey(); +} Propchange: commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPoolMBean.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericObjectPool.java URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericObjectPool.java?rev=1158736&r1=1158735&r2=1158736&view=diff ============================================================================== --- commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericObjectPool.java (original) +++ commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericObjectPool.java Wed Aug 17 14:28:40 2011 @@ -17,6 +17,7 @@ package org.apache.commons.pool2.impl; +import java.lang.management.ManagementFactory; import java.util.Iterator; import java.util.Map; import java.util.NoSuchElementException; @@ -25,6 +26,13 @@ import java.util.concurrent.ConcurrentHa import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; +import javax.management.InstanceAlreadyExistsException; +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.BaseObjectPool; import org.apache.commons.pool2.ObjectPool; import org.apache.commons.pool2.PoolUtils; @@ -164,7 +172,8 @@ import org.apache.commons.pool2.Poolable * 2011) $ * @since Pool 1.0 */ -public class GenericObjectPool extends BaseObjectPool { +public class GenericObjectPool extends BaseObjectPool + implements GenericObjectPoolMBean { // --- constructors ----------------------------------------------- @@ -196,6 +205,42 @@ public class GenericObjectPool extend this.blockWhenExhausted = config.getBlockWhenExhausted(); startEvictor(timeBetweenEvictionRunsMillis); + + // JMX Registration + if (config.isJmxEnabled()) { + 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); + 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; + } + } + } } @@ -1384,4 +1429,7 @@ public class GenericObjectPool extend /** An iterator for {@link #idleObjects} that is used by the evictor. */ private Iterator> evictionIterator = null; + + private static final String ONAME_BASE = + "org.apache.commoms.pool2:type=GenericObjectPool,name="; } Added: commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericObjectPoolMBean.java URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericObjectPoolMBean.java?rev=1158736&view=auto ============================================================================== --- commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericObjectPoolMBean.java (added) +++ commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericObjectPoolMBean.java Wed Aug 17 14:28:40 2011 @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.pool2.impl; + +public interface GenericObjectPoolMBean { + // Expose standard attributes via JMX + boolean getBlockWhenExhausted(); + boolean getLifo(); + int getMaxIdle(); + int getMaxTotal(); + long getMaxWait(); + long getMinEvictableIdleTimeMillis(); + int getMinIdle(); + int getNumActive(); + int getNumIdle(); + int getNumTestsPerEvictionRun(); + boolean getTestOnBorrow(); + boolean getTestOnReturn(); + boolean getTestWhileIdle(); + long getTimeBetweenEvictionRunsMillis(); + boolean isClosed(); +} Propchange: commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericObjectPoolMBean.java ------------------------------------------------------------------------------ svn:eol-style = native