You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by lh...@apache.org on 2011/01/27 10:07:25 UTC

svn commit: r1064040 - in /servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl: ExecutorFactoryImpl.java ExecutorImpl.java

Author: lhein
Date: Thu Jan 27 09:07:25 2011
New Revision: 1064040

URL: http://svn.apache.org/viewvc?rev=1064040&view=rev
Log:
fixed mbean resource leak (see ESB-1367)

Modified:
    servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorFactoryImpl.java
    servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorImpl.java

Modified: servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorFactoryImpl.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorFactoryImpl.java?rev=1064040&r1=1064039&r2=1064040&view=diff
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorFactoryImpl.java (original)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorFactoryImpl.java Thu Jan 27 09:07:25 2011
@@ -19,6 +19,7 @@ package org.apache.servicemix.executors.
 import org.apache.servicemix.executors.Executor;
 import org.apache.servicemix.executors.ExecutorFactory;
 
+import javax.management.ObjectName;
 import java.lang.reflect.Method;
 import java.util.HashMap;
 import java.util.Map;
@@ -52,11 +53,12 @@ public class ExecutorFactoryImpl impleme
     private javax.management.MBeanServer mbeanServer;
     private org.fusesource.commons.management.ManagementStrategy managementStrategy;
 
-    private Map<String, ExecutorConfig> configs = new HashMap<String, ExecutorConfig>();
+    private Map<String, ExecutorConfig> configs         = new HashMap<String, ExecutorConfig>();
+    private Map<Executor, ObjectName>   executorNames   = new HashMap<Executor, ObjectName>();
 
     public Executor createExecutor(String id) {
         ExecutorConfig config = getConfig(id);
-        ExecutorImpl executor = new ExecutorImpl(createService(id, config), config.getShutdownDelay(), config.isBypassIfSynchronous());
+        ExecutorImpl executor = new ExecutorImpl(this, createService(id, config), config.getShutdownDelay(), config.isBypassIfSynchronous());
         try {
             registerMBean(id, executor, config);
         } catch (Exception ex) {
@@ -68,7 +70,7 @@ public class ExecutorFactoryImpl impleme
     public Executor createDaemonExecutor(String id) {
         ExecutorConfig config = getConfig(id);
         config.setThreadDaemon(true);
-        ExecutorImpl executor = new ExecutorImpl(createService(id, config), config.getShutdownDelay(), config.isBypassIfSynchronous());
+        ExecutorImpl executor = new ExecutorImpl(this, createService(id, config), config.getShutdownDelay(), config.isBypassIfSynchronous());
         try {
             registerMBean(id, executor, config);
         } catch (Exception ex) {
@@ -222,25 +224,43 @@ public class ExecutorFactoryImpl impleme
     }
 
     private void registerMBean(String id, ExecutorImpl executor, ExecutorConfig config) throws Exception {
-        ManagedExecutor mbean = new org.apache.servicemix.executors.impl.ManagedExecutor(id, executor, config);
-
+        ManagedExecutor mbean = new ManagedExecutor(id, executor, config);
+        ObjectName oName = null;
         if (this.managementStrategy != null) {
             // SMX 4 - ManagementStrategy
             if (hasSubType(id)) {
-                this.managementStrategy.manageNamedObject(mbean, new javax.management.ObjectName(String.format("%s%s%s%s", OBJECT_NAME_PREFIX, sanitize(getType(id)), OBJECT_NAME_POSTFIX, sanitize(getSubType(id)))));
+                oName = new javax.management.ObjectName(String.format("%s%s%s%s", OBJECT_NAME_PREFIX, sanitize(getType(id)), OBJECT_NAME_POSTFIX, sanitize(getSubType(id))));
+                this.managementStrategy.manageNamedObject(mbean, oName);
             } else {
-                this.managementStrategy.manageNamedObject(mbean, new javax.management.ObjectName(String.format("%s%s", OBJECT_NAME_PREFIX, sanitize(id))));
+                oName = new javax.management.ObjectName(String.format("%s%s", OBJECT_NAME_PREFIX, sanitize(id)));
+                this.managementStrategy.manageNamedObject(mbean, oName);
             }
         } else if (this.mbeanServer != null) {
             // SMX 3 - MBeanServer
             if (hasSubType(id)) {
-                this.mbeanServer.registerMBean(mbean, new javax.management.ObjectName(String.format("%s%s%s%s", OBJECT_NAME_PREFIX, sanitize(getType(id)), OBJECT_NAME_POSTFIX, sanitize(getSubType(id)))));
+                oName = new javax.management.ObjectName(String.format("%s%s%s%s", OBJECT_NAME_PREFIX, sanitize(getType(id)), OBJECT_NAME_POSTFIX, sanitize(getSubType(id))));
+                this.mbeanServer.registerMBean(mbean, oName);
             } else {
-                this.mbeanServer.registerMBean(mbean, new javax.management.ObjectName(String.format("%s%s", OBJECT_NAME_PREFIX, sanitize(id))));
+                oName = new javax.management.ObjectName(String.format("%s%s", OBJECT_NAME_PREFIX, sanitize(id)));
+                this.mbeanServer.registerMBean(mbean, oName);
             }
         } else {
             // no possibility to insert the mbean
         }
+        this.executorNames.put(mbean.getInternalExecutor(), oName);
+    }
+
+    void unregisterMBean(Executor executor) throws Exception {
+        ObjectName oName = this.executorNames.remove(executor);
+        if (this.managementStrategy != null) {
+            // SMX 4 - ManagementStrategy
+            this.managementStrategy.unmanageNamedObject(oName);
+        } else if (this.mbeanServer != null) {
+            // SMX 3 - MBeanServer
+            this.mbeanServer.unregisterMBean(oName);
+        } else {
+            // no possibility to remove the mbean
+        }
     }
 
     private String sanitize(String in) {

Modified: servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorImpl.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorImpl.java?rev=1064040&r1=1064039&r2=1064040&view=diff
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorImpl.java (original)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/executors/impl/ExecutorImpl.java Thu Jan 27 09:07:25 2011
@@ -16,13 +16,13 @@
  */
 package org.apache.servicemix.executors.impl;
 
+import org.apache.servicemix.executors.Executor;
+import org.apache.servicemix.executors.ExecutorAwareRunnable;
+
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 
-import org.apache.servicemix.executors.Executor;
-import org.apache.servicemix.executors.ExecutorAwareRunnable;
-
 /**
  * The default Executor implementation which uses a
  * ThreadPoolExecutor underneath.
@@ -37,7 +37,10 @@ public class ExecutorImpl implements Exe
 
     private final boolean bypassIfSynchronous;
 
-    public ExecutorImpl(ThreadPoolExecutor threadPool, long shutdownDelay, boolean bypassIfSynchronous) {
+    private ExecutorFactoryImpl executorFactory;
+
+    public ExecutorImpl(ExecutorFactoryImpl executorFactory, ThreadPoolExecutor threadPool, long shutdownDelay, boolean bypassIfSynchronous) {
+        this.executorFactory = executorFactory;
         this.threadPool = threadPool;
         this.shutdownDelay = shutdownDelay;
         this.bypassIfSynchronous = bypassIfSynchronous;
@@ -54,6 +57,11 @@ public class ExecutorImpl implements Exe
     }
 
     public void shutdown() {
+        try {
+            this.executorFactory.unregisterMBean(this);
+        } catch (Exception ex) {
+            // ignored
+        }
         threadPool.shutdown();
         if (!threadPool.isTerminated() && shutdownDelay > 0) {
             new Thread(new Runnable() {