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 2011/02/28 16:20:05 UTC

svn commit: r1075372 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/processor/ test/java/org/apache/camel/processor/

Author: davsclaus
Date: Mon Feb 28 15:20:04 2011
New Revision: 1075372

URL: http://svn.apache.org/viewvc?rev=1075372&view=rev
Log:
CAMEL-3727: Fixed recipient list in parallel mode creating a new thread pool for aggregator background task.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RecipientListParallelAggregateThreadPoolIssueTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/MulticastProcessor.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RecipientList.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/MulticastProcessor.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/MulticastProcessor.java?rev=1075372&r1=1075371&r2=1075372&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/MulticastProcessor.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/MulticastProcessor.java Mon Feb 28 15:20:04 2011
@@ -888,11 +888,22 @@ public class MulticastProcessor extends 
             // keep at least one thread in the pool so we re-use the thread avoiding to create new threads because
             // the pool shrank to zero.
             String name = getClass().getSimpleName() + "-AggregateTask";
-            aggregateExecutorService = camelContext.getExecutorServiceStrategy().newThreadPool(this, name, 1, Integer.MAX_VALUE);
+            aggregateExecutorService = createAggregateExecutorService(name);
         }
         ServiceHelper.startServices(processors);
     }
 
+    /**
+     * Strategy to create the thread pool for the aggregator background task which waits for and aggregates
+     * completed tasks when running in parallel mode.
+     *
+     * @param name  the suggested name for the background thread
+     * @return the thread pool
+     */
+    protected ExecutorService createAggregateExecutorService(String name) {
+        return camelContext.getExecutorServiceStrategy().newThreadPool(this, name, 1, Integer.MAX_VALUE);
+    }
+
     protected void doStop() throws Exception {
         ServiceHelper.stopServices(processors);
         errorHandlers.clear();

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RecipientList.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RecipientList.java?rev=1075372&r1=1075371&r2=1075372&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RecipientList.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RecipientList.java Mon Feb 28 15:20:04 2011
@@ -55,6 +55,7 @@ public class RecipientList extends Servi
     private boolean streaming;
     private long timeout;
     private ExecutorService executorService;
+    private ExecutorService aggregateExecutorService;
     private AggregationStrategy aggregationStrategy = new UseLatestAggregationStrategy();
 
     public RecipientList(CamelContext camelContext) {
@@ -108,7 +109,16 @@ public class RecipientList extends Servi
         Iterator<Object> iter = ObjectHelper.createIterator(recipientList, delimiter);
 
         RecipientListProcessor rlp = new RecipientListProcessor(exchange.getContext(), producerCache, iter, getAggregationStrategy(),
-                                                                isParallelProcessing(), getExecutorService(), isStreaming(), isStopOnException(), getTimeout());
+                                                                isParallelProcessing(), getExecutorService(), isStreaming(), isStopOnException(), getTimeout()) {
+            @Override
+            protected ExecutorService createAggregateExecutorService(String name) {
+                // use a shared executor service to avoid creating new thread pools
+                if (aggregateExecutorService == null) {
+                    aggregateExecutorService = super.createAggregateExecutorService("RecipientList-AggregateTask");
+                }
+                return aggregateExecutorService;
+            }
+        };
         rlp.setIgnoreInvalidEndpoints(isIgnoreInvalidEndpoints());
 
         // start the service
@@ -144,7 +154,7 @@ public class RecipientList extends Servi
     protected void doStop() throws Exception {
         ServiceHelper.stopService(producerCache);
     }
-    
+
     public boolean isStreaming() {
         return streaming;
     }
@@ -200,4 +210,5 @@ public class RecipientList extends Servi
     public void setTimeout(long timeout) {
         this.timeout = timeout;
     }
+
 }

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RecipientListParallelAggregateThreadPoolIssueTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RecipientListParallelAggregateThreadPoolIssueTest.java?rev=1075372&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RecipientListParallelAggregateThreadPoolIssueTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RecipientListParallelAggregateThreadPoolIssueTest.java Mon Feb 28 15:20:04 2011
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.processor;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * @version
+ */
+public class RecipientListParallelAggregateThreadPoolIssueTest extends ContextTestSupport {
+
+    public void testRecipientListParallelALot() throws Exception {
+        String before = context.getExecutorServiceStrategy().getThreadName("foo");
+
+        for (int i = 0; i < 10; i++) {
+            MockEndpoint mock = getMockEndpoint("mock:result");
+            mock.reset();
+            mock.expectedBodiesReceivedInAnyOrder("c", "b", "a");
+
+            template.sendBodyAndHeader("direct:start", "Hello World", "foo", "direct:a,direct:b,direct:c");
+
+            assertMockEndpointsSatisfied();
+        }
+
+        String after = context.getExecutorServiceStrategy().getThreadName("foo");
+        int num1 = context.getTypeConverter().convertTo(int.class, before);
+        int num2 = context.getTypeConverter().convertTo(int.class, after);
+        int diff = num2 - num1;
+        // should be 10 + 1 other threads (10 in parallel pool + 1 in aggregate pool)
+        assertTrue("There should be 12 threads in diff, was: " + diff, diff == 12);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                context.getExecutorServiceStrategy().setThreadNamePattern("${counter}");
+
+                from("direct:start")
+                        .recipientList(header("foo")).parallelProcessing();
+
+                from("direct:a").to("log:a").transform(constant("a")).to("mock:result");
+                from("direct:b").to("log:b").transform(constant("b")).to("mock:result");
+                from("direct:c").to("log:c").transform(constant("c")).to("mock:result");
+            }
+        };
+    }
+}