You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2012/02/27 17:13:17 UTC

svn commit: r1294198 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/impl/ camel-core/src/main/java/org/apache/camel/management/ camel-core/src/main/java/org/apache/camel/spi/ camel-core/src/test/java/org/apache/camel/impl/ camel-core/src/...

Author: davsclaus
Date: Mon Feb 27 16:13:16 2012
New Revision: 1294198

URL: http://svn.apache.org/viewvc?rev=1294198&view=rev
Log:
CAMEL-5042: Shutting down a thread pool will remove it from JMX if it was managed.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedSedaRouteRemoveTest.java
      - copied, changed from r1294043, camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteRemoveTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExecutorServiceManager.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/spi/LifecycleStrategy.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextWithLifecycleStrategyRestartTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DummyLifecycleStrategy.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/impl/VetoCamelContextStartTest.java
    camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/DummyLifecycleStrategy.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExecutorServiceManager.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExecutorServiceManager.java?rev=1294198&r1=1294197&r2=1294198&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExecutorServiceManager.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExecutorServiceManager.java Mon Feb 27 16:13:16 2012
@@ -245,26 +245,37 @@ public class DefaultExecutorServiceManag
     public void shutdown(ExecutorService executorService) {
         ObjectHelper.notNull(executorService, "executorService");
 
-        if (executorService.isShutdown()) {
-            return;
+        if (!executorService.isShutdown()) {
+            LOG.debug("Shutdown ExecutorService: {}", executorService);
+            executorService.shutdown();
+            LOG.trace("Shutdown ExecutorService: {} complete.", executorService);
         }
 
-        LOG.debug("Shutdown ExecutorService: {}", executorService);
-        executorService.shutdown();
-        LOG.trace("Shutdown ExecutorService: {} complete.", executorService);
+        if (executorService instanceof ThreadPoolExecutor) {
+            ThreadPoolExecutor threadPool = (ThreadPoolExecutor) executorService;
+            for (LifecycleStrategy lifecycle : camelContext.getLifecycleStrategies()) {
+                lifecycle.onThreadPoolRemove(camelContext, threadPool);
+            }
+        }
     }
 
     @Override
     public List<Runnable> shutdownNow(ExecutorService executorService) {
         ObjectHelper.notNull(executorService, "executorService");
 
-        if (executorService.isShutdown()) {
-            return null;
+        List<Runnable> answer = null;
+        if (!executorService.isShutdown()) {
+            LOG.debug("ShutdownNow ExecutorService: {}", executorService);
+            answer = executorService.shutdownNow();
+            LOG.trace("ShutdownNow ExecutorService: {} complete.", executorService);
         }
 
-        LOG.debug("ShutdownNow ExecutorService: {}", executorService);
-        List<Runnable> answer = executorService.shutdownNow();
-        LOG.trace("ShutdownNow ExecutorService: {} complete.", executorService);
+        if (executorService instanceof ThreadPoolExecutor) {
+            ThreadPoolExecutor threadPool = (ThreadPoolExecutor) executorService;
+            for (LifecycleStrategy lifecycle : camelContext.getLifecycleStrategies()) {
+                lifecycle.onThreadPoolRemove(camelContext, threadPool);
+            }
+        }
 
         return answer;
     }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java?rev=1294198&r1=1294197&r2=1294198&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java Mon Feb 27 16:13:16 2012
@@ -105,6 +105,7 @@ public class DefaultManagementLifecycleS
     private volatile boolean initialized;
     private final Set<String> knowRouteIds = new HashSet<String>();
     private final Map<Tracer, ManagedTracer> managedTracers = new HashMap<Tracer, ManagedTracer>();
+    private final Map<ThreadPoolExecutor, Object> managedThreadPools = new HashMap<ThreadPoolExecutor, Object>();
 
     public DefaultManagementLifecycleStrategy() {
     }
@@ -573,11 +574,36 @@ public class DefaultManagementLifecycleS
 
         try {
             manageObject(mtp);
+            // store a reference so we can unmanage from JMX when the thread pool is removed
+            // we need to keep track here, as we cannot re-construct the thread pool ObjectName when removing the thread pool
+            managedThreadPools.put(threadPool, mtp);
         } catch (Exception e) {
             LOG.warn("Could not register thread pool: " + threadPool + " as ThreadPool MBean.", e);
         }
     }
 
+    public void onThreadPoolRemove(CamelContext camelContext, ThreadPoolExecutor threadPool) {
+        if (!initialized) {
+            return;
+        }
+
+        // lookup the thread pool and remove it from JMX
+        Object mtp = managedThreadPools.remove(threadPool);
+        if (mtp != null) {
+            // skip unmanaged routes
+            if (!getManagementStrategy().isManaged(mtp, null)) {
+                LOG.trace("The thread pool is not managed: {}", threadPool);
+                return;
+            }
+
+            try {
+                unmanageObject(mtp);
+            } catch (Exception e) {
+                LOG.warn("Could not unregister ThreadPool MBean", e);
+            }
+        }
+    }
+
     public void onRouteContextCreate(RouteContext routeContext) {
         if (!initialized) {
             return;
@@ -795,6 +821,7 @@ public class DefaultManagementLifecycleS
         preServices.clear();
         wrappedProcessors.clear();
         managedTracers.clear();
+        managedThreadPools.clear();
         ServiceHelper.stopService(timerListenerManager);
     }
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/LifecycleStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/LifecycleStrategy.java?rev=1294198&r1=1294197&r2=1294198&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/spi/LifecycleStrategy.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/spi/LifecycleStrategy.java Mon Feb 27 16:13:16 2012
@@ -140,4 +140,12 @@ public interface LifecycleStrategy {
     void onThreadPoolAdd(CamelContext camelContext, ThreadPoolExecutor threadPool, String id,
                          String sourceId, String routeId, String threadPoolProfileId);
 
+    /**
+     * Notification on removing a thread pool.
+     *
+     * @param camelContext the camel context
+     * @param threadPool   the thread pool
+     */
+    void onThreadPoolRemove(CamelContext camelContext, ThreadPoolExecutor threadPool);
+
 }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextWithLifecycleStrategyRestartTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextWithLifecycleStrategyRestartTest.java?rev=1294198&r1=1294197&r2=1294198&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextWithLifecycleStrategyRestartTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextWithLifecycleStrategyRestartTest.java Mon Feb 27 16:13:16 2012
@@ -160,6 +160,10 @@ public class DefaultCamelContextWithLife
         public void onThreadPoolAdd(CamelContext camelContext, ThreadPoolExecutor threadPool, String id, String sourceId, String routeId, String threadPoolProfileId) {
         }
 
+        @Override
+        public void onThreadPoolRemove(CamelContext camelContext, ThreadPoolExecutor threadPool) {
+        }
+
         public int getContextStartCounter() {
             return contextStartCounter.get();
         }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DummyLifecycleStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DummyLifecycleStrategy.java?rev=1294198&r1=1294197&r2=1294198&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DummyLifecycleStrategy.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DummyLifecycleStrategy.java Mon Feb 27 16:13:16 2012
@@ -91,6 +91,10 @@ public class DummyLifecycleStrategy impl
         events.add("onThreadPoolAdd");
     }
 
+    public void onThreadPoolRemove(CamelContext camelContext, ThreadPoolExecutor threadPool) {
+        events.add("onThreadPoolRemove");
+    }
+
     public List<String> getEvents() {
         return events;
     }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/VetoCamelContextStartTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/VetoCamelContextStartTest.java?rev=1294198&r1=1294197&r2=1294198&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/VetoCamelContextStartTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/VetoCamelContextStartTest.java Mon Feb 27 16:13:16 2012
@@ -118,5 +118,9 @@ public class VetoCamelContextStartTest e
         @Override
         public void onThreadPoolAdd(CamelContext camelContext, ThreadPoolExecutor threadPool, String id, String sourceId, String routeId, String threadPoolProfileId) {
         }
+
+        @Override
+        public void onThreadPoolRemove(CamelContext camelContext, ThreadPoolExecutor threadPool) {
+        }
     }
 }

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedSedaRouteRemoveTest.java (from r1294043, camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteRemoveTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedSedaRouteRemoveTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedSedaRouteRemoveTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteRemoveTest.java&r1=1294043&r2=1294198&rev=1294198&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteRemoveTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedSedaRouteRemoveTest.java Mon Feb 27 16:13:16 2012
@@ -20,7 +20,6 @@ import java.util.Set;
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
 
-import org.apache.camel.Exchange;
 import org.apache.camel.ServiceStatus;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
@@ -28,13 +27,7 @@ import org.apache.camel.component.mock.M
 /**
  * @version 
  */
-public class ManagedRouteRemoveTest extends ManagementTestSupport {
-
-    @Override
-    protected void setUp() throws Exception {
-        deleteDirectory("target/managed");
-        super.setUp();
-    }
+public class ManagedSedaRouteRemoveTest extends ManagementTestSupport {
 
     public void testRemove() throws Exception {
         MBeanServer mbeanServer = getMBeanServer();
@@ -43,7 +36,7 @@ public class ManagedRouteRemoveTest exte
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedBodiesReceived("Hello World");
 
-        template.sendBodyAndHeader("file://target/managed", "Hello World", Exchange.FILE_NAME, "hello.txt");
+        template.sendBody("seda:foo", "Hello World");
 
         assertMockEndpointsSatisfied();
 
@@ -51,6 +44,19 @@ public class ManagedRouteRemoveTest exte
         String state = (String) mbeanServer.getAttribute(on, "State");
         assertEquals("Should be started", ServiceStatus.Started.name(), state);
 
+        // and there should be 2 thread pools (1 default + 1 seda)
+        Set<ObjectName> set = mbeanServer.queryNames(new ObjectName("*:type=threadpools,*"), null);
+        assertEquals(2, set.size());
+        // there should be a seda thread pool in there
+        boolean seda = false;
+        for (ObjectName names : set) {
+            if (names.toString().contains("Seda")) {
+                seda = true;
+                break;
+            }
+        }
+        assertTrue("There should be a seda thread pool", seda);
+
         // stop
         mbeanServer.invoke(on, "stop", null, null);
 
@@ -65,8 +71,15 @@ public class ManagedRouteRemoveTest exte
         assertFalse("Route mbean should have been unregistered", registered);
 
         // and no more routes
-        Set<ObjectName> set = mbeanServer.queryNames(new ObjectName("*:type=routes,*"), null);
+        set = mbeanServer.queryNames(new ObjectName("*:type=routes,*"), null);
         assertEquals(0, set.size());
+
+        // and thread pool should be removed (shutdown creates a new thread pool as well)
+        set = mbeanServer.queryNames(new ObjectName("*:type=threadpools,*"), null);
+        assertEquals(1, set.size());
+        // and the thread pool should not be a seda thread pool
+        String name = set.iterator().next().toString();
+        assertFalse("There should not be a seda thread pool", name.contains("Seda"));
     }
 
     static ObjectName getRouteObjectName(MBeanServer mbeanServer) throws Exception {
@@ -81,7 +94,7 @@ public class ManagedRouteRemoveTest exte
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("file://target/managed").to("mock:result");
+                from("seda:foo").to("mock:result");
             }
         };
     }

Modified: camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java?rev=1294198&r1=1294197&r2=1294198&view=diff
==============================================================================
--- camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java (original)
+++ camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java Mon Feb 27 16:13:16 2012
@@ -134,4 +134,8 @@ public class OsgiServiceRegistry impleme
                                 String id, String sourceId, String routeId, String threadPoolProfileId) {
         // noop
     }
+
+    public void onThreadPoolRemove(CamelContext camelContext, ThreadPoolExecutor threadPoolExecutor) {
+        // noop
+    }
 }

Modified: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/DummyLifecycleStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/DummyLifecycleStrategy.java?rev=1294198&r1=1294197&r2=1294198&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/DummyLifecycleStrategy.java (original)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/DummyLifecycleStrategy.java Mon Feb 27 16:13:16 2012
@@ -75,4 +75,7 @@ public class DummyLifecycleStrategy impl
     public void onThreadPoolAdd(CamelContext camelContext, ThreadPoolExecutor threadPool, String id,
                                 String sourceId, String routeId, String threadPoolProfileId) {
     }
+
+    public void onThreadPoolRemove(CamelContext camelContext, ThreadPoolExecutor threadPool) {
+    }
 }