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 @@
     <action dev="markt" type="fix" issue="POOL-178">
       Re-factor common code into common base classes.
     </action>
+    <action dev="markt" type="update" issue="POOL-172">
+      Expose GOP and GKOP attributes via JMX.
+    </action>
   </release>
   <release version="1.5.6" date="2011-04-03" description="This is a patch 
release, including bugfixes only.">
     <action dev="markt" type="fix" issue="POOL-179" due-to="Axel Grossmann">

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<K,T> extends BaseKeyedObjectPool<K,T>  {
+public class GenericKeyedObjectPool<K,T> extends BaseKeyedObjectPool<K,T>
+        implements GenericKeyedObjectPoolMBean<K> {
 
     //--- constructors -----------------------------------------------
 
@@ -242,6 +252,42 @@ public class GenericKeyedObjectPool<K,T>
         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<K,T>
         return objectDefecit;
     }
 
+    
+    //--- JMX specific attributes 
----------------------------------------------
+    public Map<String,Integer> getNumActivePerKey() {
+        HashMap<String,Integer> result = new HashMap<String,Integer>();
+
+        Iterator<Entry<K,ObjectDeque<T>>> iter = poolMap.entrySet().iterator();
+        while (iter.hasNext()) {
+            Entry<K,ObjectDeque<T>> entry = iter.next();
+            if (entry != null) {
+                K key = entry.getKey();
+                ObjectDeque<T> 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<K,T>
      * 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<K> {
+    // 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<String,Integer> 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<T> extends BaseObjectPool<T> {
+public class GenericObjectPool<T> extends BaseObjectPool<T>
+        implements GenericObjectPoolMBean {
 
     // --- constructors -----------------------------------------------
 
@@ -196,6 +205,42 @@ public class GenericObjectPool<T> 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<T> extend
 
     /** An iterator for {@link #idleObjects} that is used by the evictor. */
     private Iterator<PooledObject<T>> 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


Reply via email to