Author: davsclaus
Date: Wed Sep  5 12:16:15 2012
New Revision: 1381155

URL: http://svn.apache.org/viewvc?rev=1381155&view=rev
Log:
CAMEL-5564: Ensure default error handler thread pool and management load task 
pool is shutdown properly.

Modified:
    camel/branches/camel-2.9.x/   (props changed)
    
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
    
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java
    
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/support/TimerListenerManager.java
    
camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/impl/MultipleLifecycleStrategyTest.java
    
camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/management/LoadTimerTest.java
    
camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/support/TimerListenerManagerTest.java

Propchange: camel/branches/camel-2.9.x/
------------------------------------------------------------------------------
  Merged /camel/trunk:r1381089
  Merged /camel/branches/camel-2.10.x:r1381138

Propchange: camel/branches/camel-2.9.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=1381155&r1=1381154&r2=1381155&view=diff
==============================================================================
--- 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
 (original)
+++ 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
 Wed Sep  5 12:16:15 2012
@@ -1561,6 +1561,12 @@ public class DefaultCamelContext extends
 
         // the stop order is important
 
+        // shutdown default error handler thread pool
+        if (errorHandlerExecutorService != null) {
+            getExecutorServiceManager().shutdown(errorHandlerExecutorService);
+            errorHandlerExecutorService = null;
+        }
+
         // shutdown debugger
         ServiceHelper.stopAndShutdownService(getDebugger());
 

Modified: 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java?rev=1381155&r1=1381154&r2=1381155&view=diff
==============================================================================
--- 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java
 (original)
+++ 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java
 Wed Sep  5 12:16:15 2012
@@ -81,7 +81,6 @@ import org.apache.camel.support.ServiceS
 import org.apache.camel.support.TimerListenerManager;
 import org.apache.camel.util.KeyValueHolder;
 import org.apache.camel.util.ObjectHelper;
-import org.apache.camel.util.ServiceHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -788,11 +787,10 @@ public class DefaultManagementLifecycleS
         boolean enabled = 
camelContext.getManagementStrategy().getStatisticsLevel() != 
ManagementStatisticsLevel.Off;
         if (enabled) {
             LOG.info("StatisticsLevel at {} so enabling load performance 
statistics", camelContext.getManagementStrategy().getStatisticsLevel());
-            ScheduledExecutorService executorService = 
camelContext.getExecutorServiceManager().newSingleThreadScheduledExecutor(this, 
"ManagementLoadTask");
-            timerListenerManager.setExecutorService(executorService);
             // must use 1 sec interval as the load statistics is based on 1 
sec calculations
             timerListenerManager.setInterval(1000);
-            ServiceHelper.startService(timerListenerManager);
+            // add as a service so we can manage its lifecycle
+            getCamelContext().addService(timerListenerManager);
         }
     }
 
@@ -803,7 +801,6 @@ public class DefaultManagementLifecycleS
         preServices.clear();
         wrappedProcessors.clear();
         managedTracers.clear();
-        ServiceHelper.stopService(timerListenerManager);
     }
 
     /**

Modified: 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/support/TimerListenerManager.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/support/TimerListenerManager.java?rev=1381155&r1=1381154&r2=1381155&view=diff
==============================================================================
--- 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/support/TimerListenerManager.java
 (original)
+++ 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/support/TimerListenerManager.java
 Wed Sep  5 12:16:15 2012
@@ -22,6 +22,8 @@ import java.util.concurrent.ScheduledExe
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
 import org.apache.camel.TimerListener;
 import org.apache.camel.util.ObjectHelper;
 import org.slf4j.Logger;
@@ -31,18 +33,16 @@ import org.slf4j.LoggerFactory;
  * A {@link TimerListener} manager which triggers the
  * {@link org.apache.camel.TimerListener} listeners once every second.
  * <p/>
- * The {@link 
#setExecutorService(java.util.concurrent.ScheduledExecutorService)} method
- * must be invoked prior to starting this manager using the {@link #start()} 
method.
- * <p/>
  * Also ensure when adding and remove listeners, that they are correctly 
removed to avoid
  * leaking memory.
  *
  * @see TimerListener
  */
-public class TimerListenerManager extends ServiceSupport implements Runnable {
+public class TimerListenerManager extends ServiceSupport implements Runnable, 
CamelContextAware {
 
     private static final Logger LOG = 
LoggerFactory.getLogger(TimerListenerManager.class);
     private final Set<TimerListener> listeners = new 
LinkedHashSet<TimerListener>();
+    private CamelContext camelContext;
     private ScheduledExecutorService executorService;
     private volatile ScheduledFuture task;
     private long interval = 1000L;
@@ -50,8 +50,14 @@ public class TimerListenerManager extend
     public TimerListenerManager() {
     }
 
-    public void setExecutorService(ScheduledExecutorService executorService) {
-        this.executorService = executorService;
+    @Override
+    public void setCamelContext(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    public CamelContext getCamelContext() {
+        return camelContext;
     }
 
     /**
@@ -122,8 +128,11 @@ public class TimerListenerManager extend
 
     @Override
     protected void doStart() throws Exception {
-        ObjectHelper.notNull(executorService, "executorService", this);
-        task = executorService.scheduleAtFixedRate(this, 1000L, interval, 
TimeUnit.MILLISECONDS);
+        ObjectHelper.notNull(camelContext, "camelContext", this);
+
+        // create scheduled thread pool to trigger the task to run every 
interval
+        executorService = 
camelContext.getExecutorServiceManager().newSingleThreadScheduledExecutor(this, 
"ManagementLoadTask");
+        task = executorService.scheduleAtFixedRate(this, interval, interval, 
TimeUnit.MILLISECONDS);
         LOG.debug("Started scheduled TimerListener task to run with interval 
{} ms", interval);
     }
 
@@ -139,6 +148,9 @@ public class TimerListenerManager extend
     @Override
     protected void doShutdown() throws Exception {
         super.doShutdown();
+        // shutdown thread pool when we are shutting down
+        camelContext.getExecutorServiceManager().shutdown(executorService);
+        executorService = null;
         listeners.clear();
     }
 }

Modified: 
camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/impl/MultipleLifecycleStrategyTest.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/impl/MultipleLifecycleStrategyTest.java?rev=1381155&r1=1381154&r2=1381155&view=diff
==============================================================================
--- 
camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/impl/MultipleLifecycleStrategyTest.java
 (original)
+++ 
camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/impl/MultipleLifecycleStrategyTest.java
 Wed Sep  5 12:16:15 2012
@@ -50,9 +50,9 @@ public class MultipleLifecycleStrategyTe
         context.removeComponent("log");
         context.stop();
 
-        List<String> expectedEvents = Arrays.asList("onThreadPoolAdd", 
"onContextStart", "onServiceAdd", "onServiceAdd", "onServiceAdd",
-                "onServiceAdd", "onServiceAdd", "onServiceAdd", 
"onServiceAdd", "onThreadPoolAdd", "onComponentAdd", "onEndpointAdd",
-                "onComponentRemove", "onThreadPoolAdd", "onContextStop");
+        List<String> expectedEvents = Arrays.asList("onServiceAdd", 
"onThreadPoolAdd", "onContextStart", "onServiceAdd",
+                "onServiceAdd", "onServiceAdd", "onServiceAdd", 
"onServiceAdd", "onServiceAdd", "onServiceAdd",
+                "onThreadPoolAdd", "onComponentAdd", "onEndpointAdd", 
"onComponentRemove", "onThreadPoolAdd", "onContextStop");
 
         assertEquals(expectedEvents, dummy1.getEvents());
         assertEquals(expectedEvents, dummy2.getEvents());

Modified: 
camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/management/LoadTimerTest.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/management/LoadTimerTest.java?rev=1381155&r1=1381154&r2=1381155&view=diff
==============================================================================
--- 
camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/management/LoadTimerTest.java
 (original)
+++ 
camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/management/LoadTimerTest.java
 Wed Sep  5 12:16:15 2012
@@ -16,23 +16,23 @@
  */
 package org.apache.camel.management;
 
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-
-import junit.framework.TestCase;
+import org.apache.camel.ContextTestSupport;
 import org.apache.camel.TimerListener;
 import org.apache.camel.management.mbean.LoadTriplet;
 import org.apache.camel.support.TimerListenerManager;
 
-public class LoadTimerTest extends TestCase {
+public class LoadTimerTest extends ContextTestSupport {
 
     private static final int SAMPLES = 3;
 
-    public void testTimer() throws Exception {
-        ScheduledExecutorService executorService = 
Executors.newScheduledThreadPool(1);
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
 
+    public void testTimer() throws Exception {
         TimerListenerManager myTimer = new TimerListenerManager();
-        myTimer.setExecutorService(executorService);
+        myTimer.setCamelContext(context);
         myTimer.start();
 
         TestLoadAware test = new TestLoadAware();
@@ -48,7 +48,6 @@ public class LoadTimerTest extends TestC
         }
 
         myTimer.stop();
-        executorService.shutdown();
     }
 
     private class TestLoadAware implements TimerListener {

Modified: 
camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/support/TimerListenerManagerTest.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/support/TimerListenerManagerTest.java?rev=1381155&r1=1381154&r2=1381155&view=diff
==============================================================================
--- 
camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/support/TimerListenerManagerTest.java
 (original)
+++ 
camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/support/TimerListenerManagerTest.java
 Wed Sep  5 12:16:15 2012
@@ -21,20 +21,25 @@ import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
 
-import junit.framework.TestCase;
+import org.apache.camel.ContextTestSupport;
 import org.apache.camel.TimerListener;
 
 /**
  *
  */
-public class TimerListenerManagerTest extends TestCase {
+public class TimerListenerManagerTest extends ContextTestSupport {
 
     private final MyTask task = new MyTask();
 
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
     public void testTimerListenerManager() throws Exception {
         ScheduledExecutorService executor = 
Executors.newScheduledThreadPool(1);
         TimerListenerManager manager = new TimerListenerManager();
-        manager.setExecutorService(executor);
+        manager.setCamelContext(context);
         manager.addTimerListener(task);
         manager.start();
 


Reply via email to