Author: davsclaus
Date: Fri Mar  9 13:21:34 2012
New Revision: 1298823

URL: http://svn.apache.org/viewvc?rev=1298823&view=rev
Log:
CAMEL-5072: Shutdown thread pool more eagerly in scheduled poll consumers. Also 
shutdown services when stopping producer/consumer templates as the intent is 
not to use them anymore.

Added:
    
camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/component/file/ConsumerTemplateFileShutdownTest.java
      - copied unchanged from r1298821, 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/ConsumerTemplateFileShutdownTest.java
    
camel/branches/camel-2.9.x/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/ConsumerTemplateFtpShutdownTest.java
      - copied unchanged from r1298821, 
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/ConsumerTemplateFtpShutdownTest.java
Modified:
    camel/branches/camel-2.9.x/   (props changed)
    
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/ConsumerTemplate.java
    
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/ProducerTemplate.java
    
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java
    
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/DefaultConsumerTemplate.java
    
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/EventDrivenPollingConsumer.java
    
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java
    
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/ScheduledPollConsumer.java
    camel/branches/camel-2.9.x/components/camel-solr/   (props changed)

Propchange: camel/branches/camel-2.9.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Fri Mar  9 13:21:34 2012
@@ -1 +1 @@
-/camel/trunk:1243046,1243057,1243234,1244518,1244644,1244859,1244861,1244864,1244870,1244872,1245021,1291555,1291727,1291848,1291864,1292114,1292384,1292725,1292760,1292767,1293079,1293268,1293288,1293330,1293590,1293828,1293852,1293855,1294130,1294482,1294502,1294533,1294588,1294639,1294709,1294909,1294976,1295073,1295108,1295120,1296653,1296790,1298125,1298155,1298447,1298795
+/camel/trunk:1243046,1243057,1243234,1244518,1244644,1244859,1244861,1244864,1244870,1244872,1245021,1291555,1291727,1291848,1291864,1292114,1292384,1292725,1292760,1292767,1293079,1293268,1293288,1293330,1293590,1293828,1293852,1293855,1294130,1294482,1294502,1294533,1294588,1294639,1294709,1294909,1294976,1295073,1295108,1295120,1296653,1296790,1298125,1298155,1298447,1298795,1298821

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/ConsumerTemplate.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/ConsumerTemplate.java?rev=1298823&r1=1298822&r2=1298823&view=diff
==============================================================================
--- 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/ConsumerTemplate.java
 (original)
+++ 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/ConsumerTemplate.java
 Fri Mar  9 13:21:34 2012
@@ -38,9 +38,13 @@ package org.apache.camel;
  * <li>The <tt>in.body</tt></li>
  * </ul>
  * <p/>
- * <b>Important note on usage:</b> See this
+ * <br/>
+ * Before using the template it must be started.
+ * And when you are done using the template, make sure to {@link #stop()} the 
template.
+ * <br/>
+ * <p/><b>Important note on usage:</b> See this
  * <a 
href="http://camel.apache.org/why-does-camel-use-too-many-threads-with-producertemplate.html";>FAQ
 entry</a>
- * before using, it applies to ConsumerTemplate as well.
+ * before using, it applies to this {@link ConsumerTemplate} as well.
  *
  * @version 
  */

Modified: 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/ProducerTemplate.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/ProducerTemplate.java?rev=1298823&r1=1298822&r2=1298823&view=diff
==============================================================================
--- 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/ProducerTemplate.java
 (original)
+++ 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/ProducerTemplate.java
 Fri Mar  9 13:21:34 2012
@@ -47,7 +47,11 @@ import org.apache.camel.spi.Synchronizat
  *   <li>Either <tt>IN</tt> or <tt>OUT</tt> body according to the message 
exchange pattern. If the pattern is
  *   Out capable then the <tt>OUT</tt> body is returned, otherwise <tt>IN</tt>.
  * </ul>
- * <br/><br/>
+ * <p/>
+ * <br/>
+ * Before using the template it must be started.
+ * And when you are done using the template, make sure to {@link #stop()} the 
template.
+ * <br/>
  * <p/><b>Important note on usage:</b> See this
  * <a 
href="http://camel.apache.org/why-does-camel-use-too-many-threads-with-producertemplate.html";>FAQ
 entry</a>
  * before using.

Modified: 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java?rev=1298823&r1=1298822&r2=1298823&view=diff
==============================================================================
--- 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java
 (original)
+++ 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java
 Fri Mar  9 13:21:34 2012
@@ -216,7 +216,8 @@ public class ConsumerCache extends Servi
     }
 
     protected void doStop() throws Exception {
-        ServiceHelper.stopServices(consumers.values());
+        // when stopping we intend to shutdown
+        ServiceHelper.stopAndShutdownServices(consumers.values());
         consumers.clear();
     }
 

Modified: 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/DefaultConsumerTemplate.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/DefaultConsumerTemplate.java?rev=1298823&r1=1298822&r2=1298823&view=diff
==============================================================================
--- 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/DefaultConsumerTemplate.java
 (original)
+++ 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/DefaultConsumerTemplate.java
 Fri Mar  9 13:21:34 2012
@@ -264,7 +264,8 @@ public class DefaultConsumerTemplate ext
     }
 
     protected void doStop() throws Exception {
-        ServiceHelper.stopService(consumerCache);
+        // we should shutdown the services as this is our intention, to not 
re-use the services anymore
+        ServiceHelper.stopAndShutdownService(consumerCache);
         consumerCache = null;
     }
 

Modified: 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/EventDrivenPollingConsumer.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/EventDrivenPollingConsumer.java?rev=1298823&r1=1298822&r2=1298823&view=diff
==============================================================================
--- 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/EventDrivenPollingConsumer.java
 (original)
+++ 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/EventDrivenPollingConsumer.java
 Fri Mar  9 13:21:34 2012
@@ -152,4 +152,8 @@ public class EventDrivenPollingConsumer 
     protected void doStop() throws Exception {
         ServiceHelper.stopService(consumer);
     }
+
+    protected void doShutdown() throws Exception {
+        ServiceHelper.stopAndShutdownService(consumer);
+    }
 }

Modified: 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java?rev=1298823&r1=1298822&r2=1298823&view=diff
==============================================================================
--- 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java
 (original)
+++ 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/ProducerCache.java
 Fri Mar  9 13:21:34 2012
@@ -394,17 +394,18 @@ public class ProducerCache extends Servi
         return answer;
     }
 
-    protected void doStop() throws Exception {
-        ServiceHelper.stopServices(pool);
-        ServiceHelper.stopServices(producers.values());
-        producers.clear();
-    }
-
     protected void doStart() throws Exception {
         ServiceHelper.startServices(producers.values());
         ServiceHelper.startServices(pool);
     }
 
+    protected void doStop() throws Exception {
+        // when stopping we intend to shutdown
+        ServiceHelper.stopAndShutdownService(pool);
+        ServiceHelper.stopAndShutdownServices(producers.values());
+        producers.clear();
+    }
+
     /**
      * Returns the current size of the cache
      *

Modified: 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/ScheduledPollConsumer.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/ScheduledPollConsumer.java?rev=1298823&r1=1298822&r2=1298823&view=diff
==============================================================================
--- 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/ScheduledPollConsumer.java
 (original)
+++ 
camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/ScheduledPollConsumer.java
 Fri Mar  9 13:21:34 2012
@@ -40,7 +40,8 @@ import org.slf4j.LoggerFactory;
 public abstract class ScheduledPollConsumer extends DefaultConsumer implements 
Runnable, SuspendableService, PollingConsumerPollingStrategy {
     private static final transient Logger LOG = 
LoggerFactory.getLogger(ScheduledPollConsumer.class);
 
-    private final ScheduledExecutorService executor;
+    private ScheduledExecutorService executor;
+    private boolean shutdownExecutor;
     private ScheduledFuture<?> future;
 
     // if adding more options then align with 
ScheduledPollEndpoint#configureScheduledPollConsumerProperties
@@ -56,15 +57,12 @@ public abstract class ScheduledPollConsu
 
     public ScheduledPollConsumer(Endpoint endpoint, Processor processor) {
         super(endpoint, processor);
-
-        // we only need one thread in the pool to schedule this task
-        this.executor = endpoint.getCamelContext().getExecutorServiceManager()
-                            .newScheduledThreadPool(this, 
endpoint.getEndpointUri(), 1);
-        ObjectHelper.notNull(executor, "executor");
     }
 
     public ScheduledPollConsumer(Endpoint endpoint, Processor processor, 
ScheduledExecutorService executor) {
         super(endpoint, processor);
+        // we have been given an existing thread pool, so we should not manage 
its lifecycle
+        // so we should keep shutdownExecutor as false
         this.executor = executor;
         ObjectHelper.notNull(executor, "executor");
     }
@@ -298,6 +296,16 @@ public abstract class ScheduledPollConsu
     @Override
     protected void doStart() throws Exception {
         super.doStart();
+
+        // if no existing executor provided, then create a new thread pool 
ourselves
+        if (executor == null) {
+            // we only need one thread in the pool to schedule this task
+            this.executor = 
getEndpoint().getCamelContext().getExecutorServiceManager()
+                    .newScheduledThreadPool(this, 
getEndpoint().getEndpointUri(), 1);
+            // and we should shutdown the thread pool when no longer needed
+            this.shutdownExecutor = true;
+        }
+
         ObjectHelper.notNull(executor, "executor", this);
         ObjectHelper.notNull(pollStrategy, "pollStrategy", this);
 
@@ -332,6 +340,16 @@ public abstract class ScheduledPollConsu
     }
 
     @Override
+    protected void doShutdown() throws Exception {
+        if (shutdownExecutor && executor != null) {
+            
getEndpoint().getCamelContext().getExecutorServiceManager().shutdownNow(executor);
+            executor = null;
+            future = null;
+        }
+        super.doShutdown();
+    }
+
+    @Override
     protected void doSuspend() throws Exception {
         // dont stop/cancel the future task since we just check in the run 
method
     }

Propchange: camel/branches/camel-2.9.x/components/camel-solr/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Fri Mar  9 13:21:34 2012
@@ -1 +1 @@
-/camel/trunk/components/camel-solr:1227197-1298579,1298795
+/camel/trunk/components/camel-solr:1227197-1298579,1298795,1298821


Reply via email to