CAMEL-7707: OnCompletion - Should route even if original exchange has route stop / exception handled
Conflicts: camel-core/src/main/java/org/apache/camel/processor/OnCompletionProcessor.java Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/3bca492f Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/3bca492f Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/3bca492f Branch: refs/heads/camel-2.12.x Commit: 3bca492fcd4542da1bb72eacc05f6d65295b7dea Parents: c8c7474 Author: Claus Ibsen <davscl...@apache.org> Authored: Sat Aug 16 11:11:03 2014 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Sat Aug 16 11:15:06 2014 +0200 ---------------------------------------------------------------------- .../camel/processor/OnCompletionProcessor.java | 27 ++++++++ .../camel/issues/OnCompletionIssueTest.java | 71 ++++++++++++++++++++ 2 files changed, 98 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/3bca492f/camel-core/src/main/java/org/apache/camel/processor/OnCompletionProcessor.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/processor/OnCompletionProcessor.java b/camel-core/src/main/java/org/apache/camel/processor/OnCompletionProcessor.java index 3604a5c..235a513 100644 --- a/camel-core/src/main/java/org/apache/camel/processor/OnCompletionProcessor.java +++ b/camel-core/src/main/java/org/apache/camel/processor/OnCompletionProcessor.java @@ -111,10 +111,37 @@ public class OnCompletionProcessor extends ServiceSupport implements AsyncProces * @param exchange the exchange */ protected static void doProcess(Processor processor, Exchange exchange) { + // must remember some properties which we cannot use during onCompletion processing + // as otherwise we may cause issues + Object stop = exchange.removeProperty(Exchange.ROUTE_STOP); + Object failureHandled = exchange.removeProperty(Exchange.FAILURE_HANDLED); + Object caught = exchange.removeProperty(Exchange.EXCEPTION_CAUGHT); + Object errorhandlerHandled = exchange.removeProperty(Exchange.ERRORHANDLER_HANDLED); + + Exception cause = exchange.getException(); + exchange.setException(null); + try { processor.process(exchange); } catch (Exception e) { exchange.setException(e); + } finally { + // restore the options + if (stop != null) { + exchange.setProperty(Exchange.ROUTE_STOP, stop); + } + if (failureHandled != null) { + exchange.setProperty(Exchange.FAILURE_HANDLED, failureHandled); + } + if (caught != null) { + exchange.setProperty(Exchange.EXCEPTION_CAUGHT, caught); + } + if (errorhandlerHandled != null) { + exchange.setProperty(Exchange.ERRORHANDLER_HANDLED, errorhandlerHandled); + } + if (cause != null) { + exchange.setException(cause); + } } } http://git-wip-us.apache.org/repos/asf/camel/blob/3bca492f/camel-core/src/test/java/org/apache/camel/issues/OnCompletionIssueTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/issues/OnCompletionIssueTest.java b/camel-core/src/test/java/org/apache/camel/issues/OnCompletionIssueTest.java new file mode 100644 index 0000000..5fa5df0 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/issues/OnCompletionIssueTest.java @@ -0,0 +1,71 @@ +/** + * 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.issues; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; + +public class OnCompletionIssueTest extends ContextTestSupport { + + public void testOnCompletionIssue() throws Exception { + MockEndpoint end = getMockEndpoint("mock:end"); + end.expectedMessageCount(1); + + MockEndpoint complete = getMockEndpoint("mock:complete"); + complete.expectedBodiesReceived("finish", "stop", "faulted", "except"); + + template.sendBody("direct:input", "finish"); + template.sendBody("direct:input", "stop"); + template.sendBody("direct:input", "fault"); + template.sendBody("direct:input", "except"); + + setAssertPeriod(2000); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + onCompletion() + .log("completing ${body}") + .to("mock:complete"); + + from("direct:input") + .onException(Exception.class) + .handled(true) + .end() + .choice() + .when(simple("${body} == 'stop'")) + .log("stopping") + .stop() + .when(simple("${body} == 'fault'")) + .log("faulting") + .setFaultBody(constant("faulted")) + .when(simple("${body} == 'except'")) + .log("excepting") + .throwException(new Exception("Exception requested")) + .end() + .log("finishing") + .to("mock:end"); + } + }; + } +}