CAMEL-11287: MDC routeId value is lost after calling a direct route from a transacted route
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/bf40b387 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/bf40b387 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/bf40b387 Branch: refs/heads/camel-2.18.x Commit: bf40b3874501cbca0f1cc666fb26428e8289d373 Parents: 94ff981 Author: Claus Ibsen <davscl...@apache.org> Authored: Tue May 16 16:46:36 2017 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Tue May 16 16:53:12 2017 +0200 ---------------------------------------------------------------------- .../java/org/apache/camel/impl/MDCUnitOfWork.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/bf40b387/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 1726b80..f0f834a 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 @@ -94,14 +94,25 @@ public class MDCUnitOfWork extends DefaultUnitOfWork { @Override public void pushRouteContext(RouteContext routeContext) { - MDC.put(MDC_ROUTE_ID, routeContext.getRoute().getId()); super.pushRouteContext(routeContext); + MDC.put(MDC_ROUTE_ID, routeContext.getRoute().getId()); } @Override public RouteContext popRouteContext() { - MDC.remove(MDC_ROUTE_ID); - return super.popRouteContext(); + RouteContext answer = super.popRouteContext(); + + // restore old route id back again after we have popped + RouteContext previous = getRouteContext(); + if (previous != null) { + // restore old route id back again + MDC.put(MDC_ROUTE_ID, previous.getRoute().getId()); + } else { + // not running in route, so clear (should ideally not happen) + MDC.remove(MDC_ROUTE_ID); + } + + return answer; } @Override