[camel] branch master updated: CAMEL-12709: Fixed UseOriginalAggregationStrategy concurrency issue with splitter. Thanks to Matthias Humbert for reporting and test case.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[camel] branch master updated: CAMEL-12709: Fixed UseOriginalAggregationStrategy concurrency issue with splitter. Thanks to Matthias Humbert for reporting and test case.

davsclaus-2
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new a4fbf95  CAMEL-12709: Fixed UseOriginalAggregationStrategy concurrency issue with splitter. Thanks to Matthias Humbert for reporting and test case.
a4fbf95 is described below

commit a4fbf950fd80c0edaa18651353676e116bf73c93
Author: Claus Ibsen <[hidden email]>
AuthorDate: Tue Aug 7 15:38:47 2018 +0200

    CAMEL-12709: Fixed UseOriginalAggregationStrategy concurrency issue with splitter. Thanks to Matthias Humbert for reporting and test case.
---
 .../java/org/apache/camel/processor/Splitter.java  |  6 +--
 .../aggregate/UseOriginalAggregationStrategy.java  | 16 +++++--
 .../processor/SplitterUseOriginalLoopTest.java     | 56 ++++++++++++++++++++++
 3 files changed, 71 insertions(+), 7 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/processor/Splitter.java b/camel-core/src/main/java/org/apache/camel/processor/Splitter.java
index 1833cd5..610c266 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/Splitter.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/Splitter.java
@@ -100,10 +100,10 @@ public class Splitter extends MulticastProcessor implements AsyncProcessor, Trac
 
         // set original exchange if not already pre-configured
         if (strategy instanceof UseOriginalAggregationStrategy) {
+            // need to create a new private instance, as we can also have concurrency issue so we cannot store state
             UseOriginalAggregationStrategy original = (UseOriginalAggregationStrategy) strategy;
-            if (original.getOriginal() == null) {
-                original.setOriginal(exchange);
-            }
+            UseOriginalAggregationStrategy clone = original.newInstance(exchange);
+            setAggregationStrategyOnExchange(exchange, clone);
         }
 
         // if no custom aggregation strategy is being used then fallback to keep the original
diff --git a/camel-core/src/main/java/org/apache/camel/processor/aggregate/UseOriginalAggregationStrategy.java b/camel-core/src/main/java/org/apache/camel/processor/aggregate/UseOriginalAggregationStrategy.java
index 5b3ac53..e659026 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/aggregate/UseOriginalAggregationStrategy.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/aggregate/UseOriginalAggregationStrategy.java
@@ -24,11 +24,11 @@ import org.apache.camel.Exchange;
  * and then you may want to keep routing using the original {@link Exchange}.
  *
  * @see org.apache.camel.processor.Splitter
- * @version
+ * @version
  */
 public class UseOriginalAggregationStrategy implements AggregationStrategy {
 
-    private Exchange original;
+    private final Exchange original;
     private final boolean propagateException;
 
     public UseOriginalAggregationStrategy() {
@@ -36,7 +36,7 @@ public class UseOriginalAggregationStrategy implements AggregationStrategy {
     }
 
     public UseOriginalAggregationStrategy(boolean propagateException) {
-        this.propagateException = propagateException;
+        this(null, propagateException);
     }
 
     public UseOriginalAggregationStrategy(Exchange original, boolean propagateException) {
@@ -44,6 +44,13 @@ public class UseOriginalAggregationStrategy implements AggregationStrategy {
         this.propagateException = propagateException;
     }
 
+    /**
+     * Creates a new instance as a clone of this strategy with the new given original.
+     */
+    public UseOriginalAggregationStrategy newInstance(Exchange original) {
+        return new UseOriginalAggregationStrategy(original, propagateException);
+    }
+
     public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
         if (propagateException) {
             Exception exception = checkException(oldExchange, newExchange);
@@ -72,8 +79,9 @@ public class UseOriginalAggregationStrategy implements AggregationStrategy {
         return original;
     }
 
+    @Deprecated
     public void setOriginal(Exchange original) {
-        this.original = original;
+        throw new UnsupportedOperationException("This method is deprecated");
     }
 
     @Override
diff --git a/camel-core/src/test/java/org/apache/camel/processor/SplitterUseOriginalLoopTest.java b/camel-core/src/test/java/org/apache/camel/processor/SplitterUseOriginalLoopTest.java
new file mode 100644
index 0000000..55c31ae
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/processor/SplitterUseOriginalLoopTest.java
@@ -0,0 +1,56 @@
+/**
+ * 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.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.processor.aggregate.UseOriginalAggregationStrategy;
+
+public class SplitterUseOriginalLoopTest extends ContextTestSupport {
+
+    public void testUseOriginalLoop() throws Exception {
+        getMockEndpoint("mock:line").expectedMessageCount(6);
+        getMockEndpoint("mock:result").expectedBodiesReceived("Hello,World");
+        getMockEndpoint("mock:result").expectedHeaderReceived("looping", 2);
+        getMockEndpoint("mock:result").message(0).header("myHeader").isNull();
+
+        template.sendBody("direct:start", "Hello,World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .loop(3)
+                        .setHeader("looping", exchangeProperty(Exchange.LOOP_INDEX))
+                        .split(body(), new UseOriginalAggregationStrategy(null, false))
+                            .setHeader("myHeader", exchangeProperty(Exchange.LOOP_INDEX))
+                            .to("mock:line")
+                        .end()
+                    .end()
+                    .log("${headers}")
+                    .to("mock:result");
+            }
+        };
+    }
+
+}