Repository: camel Updated Branches: refs/heads/camel-2.12.x c8c747429 -> 3bca492fc refs/heads/camel-2.13.x 7779ceeca -> 3fe46305d refs/heads/master b42eeb60d -> 75b2c0a24
CAMEL-7707: OnCompletion - Should route even if original exchange has route stop / exception handled Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/75b2c0a2 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/75b2c0a2 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/75b2c0a2 Branch: refs/heads/master Commit: 75b2c0a24b6986cff994f72eb9fbe8b623d96250 Parents: b42eeb6 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:11:03 2014 +0200 ---------------------------------------------------------------------- .../camel/processor/OnCompletionProcessor.java | 29 +++++++- .../camel/issues/OnCompletionIssueTest.java | 71 ++++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/75b2c0a2/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 b925c29..abbdcd0 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 @@ -125,10 +125,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); + } } } @@ -151,7 +178,7 @@ public class OnCompletionProcessor extends ServiceSupport implements AsyncProces answer.setIn(answer.getOut()); answer.setOut(null); } - // set MEP to InOnly as this wire tap is a fire and forget + // set MEP to InOnly as this onCompletion is a fire and forget answer.setPattern(ExchangePattern.InOnly); } else { // use the exchange as-is http://git-wip-us.apache.org/repos/asf/camel/blob/75b2c0a2/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..0a5bdfa --- /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().parallelProcessing() + .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"); + } + }; + } +}