Refactor JMX name into a constant, add a notification during init
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 17 Dec 2008 04:00:46 +0000 (04:00 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 17 Dec 2008 04:00:46 +0000 (04:00 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@727266 13f79535-47bb-0310-9956-ffa450edef68

modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java
modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/interceptor/SlowQueryReportJmx.java
modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/jmx/ConnectionPool.java

index 7c32103..a9707a7 100644 (file)
@@ -58,6 +58,8 @@ import javax.management.ObjectName;
  */
 
 public class ConnectionPool {
+    public static final String POOL_JMX_TYPE_PREFIX = "org.apache.tomcat.jdbc.pool.jmx:type=";
+    
     //logger
     protected static Log log = LogFactory.getLog(ConnectionPool.class);
 
@@ -346,13 +348,15 @@ public class ConnectionPool {
             properties.setMaxIdle(properties.getMinIdle());
         }
 
-
+        if (this.getPoolProperties().isJmxEnabled()) startJmx();
+        
         PoolProperties.InterceptorDefinition[] proxies = getPoolProperties().getJdbcInterceptorsAsArray();
         for (int i=0; i<proxies.length; i++) {
             try {
                 proxies[i].getInterceptorClass().newInstance().poolStarted(this);
             }catch (Exception x) {
                 log.warn("Unable to inform interceptor of pool start.",x);
+                if (jmxPool!=null) jmxPool.notify(jmxPool.NOTIFY_INIT, getStackTrace(x));
                 close(true);
                 SQLException ex = new SQLException();
                 ex.initCause(x);
@@ -367,6 +371,7 @@ public class ConnectionPool {
             } //for
 
         } catch (SQLException x) {
+            if (jmxPool!=null) jmxPool.notify(jmxPool.NOTIFY_INIT, getStackTrace(x));
             close(true);
             throw x;
         } finally {
@@ -377,7 +382,7 @@ public class ConnectionPool {
                 } //end if
             } //for
         } //catch
-        if (this.getPoolProperties().isJmxEnabled()) startJmx();
+        
         closed = false;
     }
 
@@ -765,7 +770,7 @@ public class ConnectionPool {
     protected void startJmx() {
         try {
             MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
-            ObjectName name = new ObjectName("org.apache.tomcat.jdbc.pool.jmx:type=ConnectionPool,name="+getName());
+            ObjectName name = new ObjectName(POOL_JMX_TYPE_PREFIX+"ConnectionPool,name="+getName());
             jmxPool = new org.apache.tomcat.jdbc.pool.jmx.ConnectionPool(this);
             mbs.registerMBean(jmxPool, name);
         } catch (Exception x) {
@@ -776,8 +781,9 @@ public class ConnectionPool {
     protected void stopJmx() {
         try {
             MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
-            ObjectName name = new ObjectName("org.apache.tomcat.jdbc.pool.jmx:type=ConnectionPool,name="+getName());
-            mbs.unregisterMBean(name);
+            ObjectName name = new ObjectName(POOL_JMX_TYPE_PREFIX+"ConnectionPool,name="+getName());
+            if (mbs.isRegistered(name))
+                mbs.unregisterMBean(name);
             jmxPool = null;
         }catch (Exception x) {
             log.warn("Unable to stop JMX integration for connection pool. Instance["+getName()+"].",x);
index dc776ee..b456a6c 100644 (file)
@@ -202,7 +202,7 @@ public class SlowQueryReportJmx extends SlowQueryReport {
                 Registry registry = Registry.getRegistry(null, null);
                 ManagedBean managed = registry.findManagedBean(this.getClass().getName());
                 if (managed!=null) {
-                    ObjectName oname = new ObjectName("org.apache.tomcat.jdbc.pool.jmx:type="+getClass().getName()+",name=" + poolName);
+                    ObjectName oname = new ObjectName(ConnectionPool.POOL_JMX_TYPE_PREFIX+getClass().getName()+",name=" + poolName);
                     registry.unregisterComponent(oname);
                     registry.removeManagedBean(managed);
                 }
@@ -219,15 +219,15 @@ public class SlowQueryReportJmx extends SlowQueryReport {
     protected void registerJmx() {
         try {
             if (getCompositeType()!=null) {
-                ObjectName oname = new ObjectName("org.apache.tomcat.jdbc.pool.jmx:type="+getClass().getName()+",name=" + poolName);
+                ObjectName oname = new ObjectName(ConnectionPool.POOL_JMX_TYPE_PREFIX+getClass().getName()+",name=" + poolName);
                 Registry registry = Registry.getRegistry(null, null);
                 registry.loadDescriptors(getClass().getPackage().getName(),getClass().getClassLoader());
                 ManagedBean managed = registry.findManagedBean(this.getClass().getName());
                 DynamicMBean mbean = managed!=null?managed.createMBean(this):null;
                 if (mbean!=null && mbeans.putIfAbsent(poolName, mbean)==null) {
                     registry.getMBeanServer().registerMBean( mbean, oname);
-                } else {
-                    log.warn(SlowQueryReport.class.getName()+ "- No JMX support, composite type was not found.");
+                } else if (mbean==null){
+                    log.warn(SlowQueryReport.class.getName()+ "- No JMX support, unable to initiate Tomcat JMX.");
                 }
             } else {
                 log.warn(SlowQueryReport.class.getName()+ "- No JMX support, composite type was not found.");
index 3395135..f75c9c7 100644 (file)
@@ -43,6 +43,7 @@ public class ConnectionPool extends NotificationBroadcasterSupport implements Co
     //=================================================================
     //       NOTIFICATION INFO
     //=================================================================
+    public static final String NOTIFY_INIT = "INIT FAILED";
     public static final String NOTIFY_CONNECT = "CONNECTION FAILED";
     public static final String NOTIFY_ABANDON = "CONNECTION ABANDONED";
     
@@ -54,7 +55,7 @@ public class ConnectionPool extends NotificationBroadcasterSupport implements Co
     }
     
     public static MBeanNotificationInfo[] getDefaultNotificationInfo() {
-        String[] types = new String[] {NOTIFY_CONNECT, NOTIFY_ABANDON}; 
+        String[] types = new String[] {NOTIFY_INIT, NOTIFY_CONNECT, NOTIFY_ABANDON}; 
         String name = Notification.class.getName(); 
         String description = "A connection pool error condition was met."; 
         MBeanNotificationInfo info = new MBeanNotificationInfo(types, name, description);