You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ra...@apache.org on 2014/09/09 15:04:37 UTC
git commit: CAMEL-7795 Fix Regression: MDC may lose values after when
Async Routing Engine is used.
Repository: camel
Updated Branches:
refs/heads/master 6f2437e6f -> 9ef4be547
CAMEL-7795 Fix Regression: MDC may lose values after when Async Routing Engine is used.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/9ef4be54
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/9ef4be54
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/9ef4be54
Branch: refs/heads/master
Commit: 9ef4be547f9b75357fffc5d18bec80eeac708354
Parents: 6f2437e
Author: Raul Kripalani <ra...@apache.org>
Authored: Tue Sep 9 13:56:34 2014 +0100
Committer: Raul Kripalani <ra...@apache.org>
Committed: Tue Sep 9 14:04:26 2014 +0100
----------------------------------------------------------------------
.../org/apache/camel/impl/MDCUnitOfWork.java | 44 ++++--
.../apache/camel/processor/MDCAsyncTest.java | 157 +++++++++++++++++++
2 files changed, 191 insertions(+), 10 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/9ef4be54/camel-core/src/main/java/org/apache/camel/impl/MDCUnitOfWork.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/MDCUnitOfWork.java b/camel-core/src/main/java/org/apache/camel/impl/MDCUnitOfWork.java
index 4f5c0d8..62f6b16 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/MDCUnitOfWork.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/MDCUnitOfWork.java
@@ -118,13 +118,7 @@ public class MDCUnitOfWork extends DefaultUnitOfWork {
@Override
public AsyncCallback beforeProcess(Processor processor, Exchange exchange, AsyncCallback callback) {
- String routeId = MDC.get(MDC_ROUTE_ID);
- if (routeId != null) {
- // only need MDC callback if we have a route id
- return new MDCCallback(callback, routeId);
- } else {
- return callback;
- }
+ return new MDCCallback(callback);
}
@Override
@@ -190,16 +184,46 @@ public class MDCUnitOfWork extends DefaultUnitOfWork {
private static final class MDCCallback implements AsyncCallback {
private final AsyncCallback delegate;
+ private final String breadcrumbId;
+ private final String exchangeId;
+ private final String messageId;
+ private final String correlationId;
private final String routeId;
+ private final String camelContextId;
- private MDCCallback(AsyncCallback delegate, String routeId) {
+ private MDCCallback(AsyncCallback delegate) {
this.delegate = delegate;
- this.routeId = routeId;
+ this.exchangeId = MDC.get(MDC_EXCHANGE_ID);
+ this.messageId = MDC.get(MDC_MESSAGE_ID);
+ this.breadcrumbId = MDC.get(MDC_BREADCRUMB_ID);
+ this.correlationId = MDC.get(MDC_CORRELATION_ID);
+ this.camelContextId = MDC.get(MDC_CAMEL_CONTEXT_ID);
+ this.routeId = MDC.get(MDC_ROUTE_ID);
}
public void done(boolean doneSync) {
try {
- MDC.put(MDC_ROUTE_ID, routeId);
+ if (!doneSync) {
+ // when done asynchronously then restore information from previous thread
+ if (breadcrumbId != null) {
+ MDC.put(MDC_BREADCRUMB_ID, breadcrumbId);
+ }
+ if (exchangeId != null) {
+ MDC.put(MDC_EXCHANGE_ID, exchangeId);
+ }
+ if (messageId != null) {
+ MDC.put(MDC_MESSAGE_ID, messageId);
+ }
+ if (correlationId != null) {
+ MDC.put(MDC_CORRELATION_ID, correlationId);
+ }
+ if (routeId != null) {
+ MDC.put(MDC_ROUTE_ID, routeId);
+ }
+ if (camelContextId != null) {
+ MDC.put(MDC_CAMEL_CONTEXT_ID, camelContextId);
+ }
+ }
} finally {
// muse ensure delegate is invoked
delegate.done(doneSync);
http://git-wip-us.apache.org/repos/asf/camel/blob/9ef4be54/camel-core/src/test/java/org/apache/camel/processor/MDCAsyncTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/processor/MDCAsyncTest.java b/camel-core/src/test/java/org/apache/camel/processor/MDCAsyncTest.java
new file mode 100644
index 0000000..ee9f9cb
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/processor/MDCAsyncTest.java
@@ -0,0 +1,157 @@
+/**
+ * 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 java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import org.apache.camel.AsyncCallback;
+import org.apache.camel.AsyncProcessor;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+import org.slf4j.MDC;
+
+import static org.junit.Assert.assertNotEquals;
+
+/**
+ * @version
+ */
+public class MDCAsyncTest extends ContextTestSupport {
+
+ public void testMdcPreservedAfterAsyncEndpoint() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:end");
+ mock.expectedMessageCount(1);
+
+ template.sendBody("direct:a", "Hello World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ // enable MDC
+ context.setUseMDCLogging(true);
+
+ MdcCheckerProcessor checker = new MdcCheckerProcessor();
+
+ from("direct:a").routeId("route-async")
+ .process(checker)
+ .to("log:foo")
+ .process(new MyAsyncProcessor())
+ .process(checker)
+ .to("mock:end");
+
+ }
+ };
+ }
+
+ private static class MyAsyncProcessor implements AsyncProcessor {
+
+ private static final ExecutorService EXECUTOR = Executors.newFixedThreadPool(1);
+
+ public MyAsyncProcessor() {
+ // submit a Runnable that does nothing just to initialise the threads
+ EXECUTOR.submit(new Runnable() {
+ @Override
+ public void run() {
+ // do nothing
+ }
+ });
+ }
+
+ @Override
+ public void process(Exchange exchange) throws Exception {
+ throw new RuntimeCamelException("This processor does not support the sync pattern.");
+ }
+
+ @Override
+ public boolean process(Exchange exchange, final AsyncCallback callback) {
+ EXECUTOR.submit(new Runnable() {
+ @Override
+ public void run() {
+ callback.done(false);
+ }
+ });
+
+ return false;
+ }
+ }
+
+ /**
+ * Stores values from the first invocation to compare them with the second invocation later.
+ */
+ private static class MdcCheckerProcessor implements Processor {
+
+ private String routeId = "route-async";
+ private String exchangeId;
+ private String messageId;
+ private String breadcrumbId;
+ private String contextId;
+ private Long threadId;
+
+ @Override
+ public void process(Exchange exchange) throws Exception {
+ if (threadId != null) {
+ assertNotEquals(threadId, Long.valueOf(Thread.currentThread().getId()));
+ } else {
+ threadId = Long.valueOf(Thread.currentThread().getId());
+ }
+
+ if (routeId != null) {
+ assertEquals(routeId, MDC.get("camel.routeId"));
+ }
+
+ if (exchangeId != null) {
+ assertEquals(exchangeId, MDC.get("camel.exchangeId"));
+ } else {
+ exchangeId = MDC.get("camel.exchangeId");
+ assertTrue(exchangeId != null && exchangeId.length() > 0);
+ }
+
+ if (messageId != null) {
+ assertEquals(messageId, MDC.get("camel.messageId"));
+ } else {
+ messageId = MDC.get("camel.messageId");
+ assertTrue(messageId != null && messageId.length() > 0);
+ }
+
+ if (breadcrumbId != null) {
+ assertEquals(breadcrumbId, MDC.get("camel.breadcrumbId"));
+ } else {
+ breadcrumbId = MDC.get("camel.breadcrumbId");
+ assertTrue(breadcrumbId != null && breadcrumbId.length() > 0);
+ }
+
+ if (contextId != null) {
+ assertEquals(contextId, MDC.get("camel.contextId"));
+ } else {
+ contextId = MDC.get("camel.contextId");
+ assertTrue(contextId != null && contextId.length() > 0);
+ }
+
+ }
+ }
+
+}