camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1151362 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/impl/MDCUnitOfWork.java test/java/org/apache/camel/processor/MDCResetTest.java
Date Wed, 27 Jul 2011 07:25:23 GMT
Author: davsclaus
Date: Wed Jul 27 07:25:23 2011
New Revision: 1151362

URL: http://svn.apache.org/viewvc?rev=1151362&view=rev
Log:
CAMEL-4274: Fixed issue with MDC not remebering old values when using a producer template
to send in messages during routing. Thanks to Yaytay for the patch.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/MDCResetTest.java
      - copied, changed from r1151359, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/MDCTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MDCUnitOfWork.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MDCUnitOfWork.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MDCUnitOfWork.java?rev=1151362&r1=1151361&r2=1151362&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MDCUnitOfWork.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MDCUnitOfWork.java Wed Jul
27 07:25:23 2011
@@ -36,8 +36,22 @@ public class MDCUnitOfWork extends Defau
     public static final String MDC_ROUTE_ID = "routeId";
     public static final String MDC_TRANSACTION_KEY = "transactionKey";
 
+    private final String originalBreadcrumbId;
+    private final String originalExchangeId;
+    private final String originalCorrelationId;
+    private final String originalRouteId;
+    private final String originalTransactionKey;
+
     public MDCUnitOfWork(Exchange exchange) {
         super(exchange);
+
+        // remember existing values
+        this.originalExchangeId = MDC.get(MDC_EXCHANGE_ID);
+        this.originalBreadcrumbId = MDC.get(MDC_BREADCRUMB_ID);
+        this.originalCorrelationId = MDC.get(MDC_CORRELATION_ID);
+        this.originalRouteId = MDC.get(MDC_ROUTE_ID);
+        this.originalTransactionKey = MDC.get(MDC_TRANSACTION_KEY);
+
         // must add exchange id in constructor
         MDC.put(MDC_EXCHANGE_ID, exchange.getExchangeId());
         // and add optional correlation id
@@ -107,11 +121,31 @@ public class MDCUnitOfWork extends Defau
      * Clears information put on the MDC by this {@link MDCUnitOfWork}
      */
     public void clear() {
-        MDC.remove(MDC_BREADCRUMB_ID);
-        MDC.remove(MDC_EXCHANGE_ID);
-        MDC.remove(MDC_CORRELATION_ID);
-        MDC.remove(MDC_ROUTE_ID);
-        MDC.remove(MDC_TRANSACTION_KEY);
+        if (this.originalBreadcrumbId != null) {
+            MDC.put(MDC_BREADCRUMB_ID, originalBreadcrumbId);
+        } else {
+            MDC.remove(MDC_BREADCRUMB_ID);
+        }
+        if (this.originalExchangeId != null) {
+            MDC.put(MDC_EXCHANGE_ID, originalExchangeId);
+        } else {
+            MDC.remove(MDC_EXCHANGE_ID);
+        }
+        if (this.originalCorrelationId != null) {
+            MDC.put(MDC_CORRELATION_ID, originalCorrelationId);
+        } else {
+            MDC.remove(MDC_CORRELATION_ID);
+        }
+        if (this.originalRouteId != null) {
+            MDC.put(MDC_ROUTE_ID, originalRouteId);
+        } else {
+            MDC.remove(MDC_ROUTE_ID);
+        }
+        if (this.originalTransactionKey != null) {
+            MDC.put(MDC_TRANSACTION_KEY, originalTransactionKey);
+        } else {
+            MDC.remove(MDC_TRANSACTION_KEY);
+        }
     }
 
     @Override

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/MDCResetTest.java
(from r1151359, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/MDCTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/MDCResetTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/MDCResetTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/MDCTest.java&r1=1151359&r2=1151362&rev=1151362&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/MDCTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/MDCResetTest.java Wed
Jul 27 07:25:23 2011
@@ -24,13 +24,16 @@ import org.apache.camel.component.mock.M
 import org.slf4j.MDC;
 
 /**
+ * Tests that MDC works as a stack remembering old values
+ * when using a producer template to send in new messages during routing.
+ *
  * @version 
  */
-public class MDCTest extends ContextTestSupport {
+public class MDCResetTest extends ContextTestSupport {
 
     public void testMDC() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.expectedBodiesReceived("Hello World");
+        mock.expectedBodiesReceived("Good Afternoon World");
 
         template.sendBody("direct:a", "Hello World");
 
@@ -39,7 +42,7 @@ public class MDCTest extends ContextTest
 
     public void testMDCTwoMessages() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.expectedBodiesReceived("Hello World", "Bye World");
+        mock.expectedBodiesReceived("Good Afternoon World", "Good Afternoon World");
 
         template.sendBody("direct:a", "Hello World");
         template.sendBody("direct:a", "Bye World");
@@ -62,16 +65,33 @@ public class MDCTest extends ContextTest
                                 assertEquals(exchange.getExchangeId(), MDC.get("exchangeId"));
                             }
                         })
-                        .to("log:foo").to("direct:b");
+                        .to("log:foo").to("direct:b")
+                        .process(new Processor() {
+                            public void process(Exchange exchange) throws Exception {
+                                String body = exchange.getIn().getBody(String.class);
+                                // use a producer template to send to b, instead of in the
route DSL
+                                body = template.requestBody("direct:b", body, String.class);
+                                exchange.getOut().setBody(body);
+                            }
+                        })
+                        .process(new Processor() {
+                            public void process(Exchange exchange) throws Exception {
+                                assertEquals("route-a", MDC.get("routeId"));
+                                assertEquals(exchange.getExchangeId(), MDC.get("exchangeId"));
+                            }
+                        })
+                        .to("log:result").to("mock:result");
 
                 from("direct:b").routeId("route-b")
                         .process(new Processor() {
                             public void process(Exchange exchange) throws Exception {
                                 assertEquals("route-b", MDC.get("routeId"));
                                 assertEquals(exchange.getExchangeId(), MDC.get("exchangeId"));
+
+                                exchange.getOut().setBody("Good Afternoon World");
                             }
                         })
-                        .to("log:bar").to("mock:result");
+                        .to("log:bar");
             }
         };
     }



Mime
View raw message