You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2023/02/22 12:41:27 UTC

[camel] branch camel-3.x updated: CAMEL-19066: Do not copy correlationId after Enricher

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

acosentino pushed a commit to branch camel-3.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-3.x by this push:
     new e05169869d2 CAMEL-19066: Do not copy correlationId after Enricher
e05169869d2 is described below

commit e05169869d251949e02eb841c366cdaf6d597fc9
Author: Henning Garus <Âhenning.garusÂ@gmaiÃl.com>
AuthorDate: Mon Feb 20 20:37:53 2023 +0100

    CAMEL-19066: Do not copy correlationId after Enricher
---
 .../java/org/apache/camel/processor/Enricher.java  | 14 +++--
 .../enricher/EnricherCorrelationIdTest.java        | 60 ++++++++++++++++++++++
 2 files changed, 71 insertions(+), 3 deletions(-)

diff --git a/core/camel-core-processor/src/main/java/org/apache/camel/processor/Enricher.java b/core/camel-core-processor/src/main/java/org/apache/camel/processor/Enricher.java
index 63901a20827..07047c31349 100644
--- a/core/camel-core-processor/src/main/java/org/apache/camel/processor/Enricher.java
+++ b/core/camel-core-processor/src/main/java/org/apache/camel/processor/Enricher.java
@@ -162,7 +162,7 @@ public class Enricher extends AsyncProcessorSupport implements IdAware, RouteIdA
             public void done(boolean doneSync) {
                 if (!isAggregateOnException() && resourceExchange.isFailed()) {
                     // copy resource exchange onto original exchange (preserving pattern)
-                    copyResultsPreservePattern(exchange, resourceExchange);
+                    copyResultsWithoutCorrelationId(exchange, resourceExchange);
                 } else {
                     prepareResult(exchange);
                     try {
@@ -173,7 +173,7 @@ public class Enricher extends AsyncProcessorSupport implements IdAware, RouteIdA
                         Exchange aggregatedExchange = aggregationStrategy.aggregate(exchange, resourceExchange);
                         if (aggregatedExchange != null) {
                             // copy aggregation result onto original exchange (preserving pattern)
-                            copyResultsPreservePattern(exchange, aggregatedExchange);
+                            copyResultsWithoutCorrelationId(exchange, aggregatedExchange);
                             // handover any synchronization (if unit of work is not shared)
                             if (resourceExchange != null && !isShareUnitOfWork()) {
                                 resourceExchange.adapt(ExtendedExchange.class).handoverCompletions(exchange);
@@ -262,12 +262,20 @@ public class Enricher extends AsyncProcessorSupport implements IdAware, RouteIdA
         ServiceHelper.stopService(aggregationStrategy, processorExchangeFactory, sendDynamicProcessor);
     }
 
+    private static void copyResultsWithoutCorrelationId(Exchange target, Exchange source) {
+        Object correlationId = source.removeProperty(ExchangePropertyKey.CORRELATION_ID);
+        copyResultsPreservePattern(target, source);
+        if (correlationId != null) {
+            source.setProperty(ExchangePropertyKey.CORRELATION_ID, correlationId);
+        }
+    }
+
     private static class CopyAggregationStrategy implements AggregationStrategy {
 
         @Override
         public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
             if (newExchange != null) {
-                copyResultsPreservePattern(oldExchange, newExchange);
+                copyResultsWithoutCorrelationId(oldExchange, newExchange);
             }
             return oldExchange;
         }
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherCorrelationIdTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherCorrelationIdTest.java
new file mode 100644
index 00000000000..69ecd2a0a88
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherCorrelationIdTest.java
@@ -0,0 +1,60 @@
+/*
+ * 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.enricher;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+
+import static org.apache.camel.Exchange.CORRELATION_ID;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class EnricherCorrelationIdTest extends ContextTestSupport {
+
+    @Test
+    void testCorrelationIdIsNotOverwrittenByEnricher() {
+        String originalCorrelationId = "SOME_ID";
+        Exchange exchange = template.request("direct:start", e -> e.setProperty(CORRELATION_ID, originalCorrelationId));
+
+        assertEquals(originalCorrelationId, exchange.getProperty(CORRELATION_ID));
+    }
+
+    @Test
+    void testCorrelationIdIsNotOverwrittenByEnricherfailedResource() {
+        String originalCorrelationId = "SOME_ID";
+        Exchange exchange
+                = template.request("direct:failed-resource", e -> e.setProperty(CORRELATION_ID, originalCorrelationId));
+
+        assertEquals(originalCorrelationId, exchange.getProperty(CORRELATION_ID));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:start").enrich("direct:enrich");
+                from("direct:enrich").setBody(constant("enrichment"));
+
+                from("direct:failed-resource").enrich("direct:failure");
+                from("direct:failure").throwException(new RuntimeException("Fail"));
+            }
+        };
+    }
+
+}