CAMEL-6889: CBR - Should break out if exception was thrown when evaluating predicate
Conflicts: camel-core/src/main/java/org/apache/camel/processor/ChoiceProcessor.java Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/d5663eb2 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d5663eb2 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d5663eb2 Branch: refs/heads/camel-2.11.x Commit: d5663eb2c004600dc499f1af47338e7bf199f5c9 Parents: ce35930 Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Oct 23 16:09:04 2013 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Oct 23 16:19:05 2013 +0200 ---------------------------------------------------------------------- .../apache/camel/processor/ChoiceProcessor.java | 7 ++ .../CBRPredicateBeanThrowExceptionTest.java | 126 +++++++++++++++++++ 2 files changed, 133 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/d5663eb2/camel-core/src/main/java/org/apache/camel/processor/ChoiceProcessor.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/processor/ChoiceProcessor.java b/camel-core/src/main/java/org/apache/camel/processor/ChoiceProcessor.java index 7d87ef5..748bb4d 100644 --- a/camel-core/src/main/java/org/apache/camel/processor/ChoiceProcessor.java +++ b/camel-core/src/main/java/org/apache/camel/processor/ChoiceProcessor.java @@ -33,6 +33,8 @@ import org.apache.camel.util.ServiceHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static org.apache.camel.processor.PipelineHelper.continueProcessing; + /** * Implements a Choice structure where one or more predicates are used which if * they are true their processors are used, with a default otherwise clause used @@ -71,6 +73,11 @@ public class ChoiceProcessor extends ServiceSupport implements AsyncProcessor, N return true; } + if (!continueProcessing(exchange, "so breaking out of choice", LOG)) { + callback.done(true); + return true; + } + if (LOG.isDebugEnabled()) { LOG.debug("#{} - {} matches: {} for: {}", new Object[]{i, predicate, matches, exchange}); } http://git-wip-us.apache.org/repos/asf/camel/blob/d5663eb2/camel-core/src/test/java/org/apache/camel/processor/CBRPredicateBeanThrowExceptionTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/processor/CBRPredicateBeanThrowExceptionTest.java b/camel-core/src/test/java/org/apache/camel/processor/CBRPredicateBeanThrowExceptionTest.java new file mode 100644 index 0000000..6888df1 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/processor/CBRPredicateBeanThrowExceptionTest.java @@ -0,0 +1,126 @@ +/** + * 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.atomic.AtomicBoolean; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.impl.JndiRegistry; + +/** + * @version + */ +public class CBRPredicateBeanThrowExceptionTest extends ContextTestSupport { + + private static AtomicBoolean check = new AtomicBoolean(); + private static AtomicBoolean check2 = new AtomicBoolean(); + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + jndi.bind("cbrBean", new MyCBRBean()); + return jndi; + } + + public void testCBR() throws Exception { + check.set(false); + check2.set(false); + + getMockEndpoint("mock:dead").expectedMessageCount(0); + getMockEndpoint("mock:foo").expectedBodiesReceived("Hello Foo"); + getMockEndpoint("mock:bar").expectedBodiesReceived("Hello Bar"); + + template.sendBodyAndHeader("direct:start", "Hello Foo", "foo", "bar"); + template.sendBodyAndHeader("direct:start", "Hello Bar", "foo", "other"); + + assertMockEndpointsSatisfied(); + + assertTrue(check.get()); + assertTrue(check2.get()); + } + + public void testCBRKaboom() throws Exception { + check.set(false); + check2.set(false); + + getMockEndpoint("mock:foo").expectedMessageCount(0); + getMockEndpoint("mock:foo2").expectedMessageCount(0); + getMockEndpoint("mock:bar").expectedMessageCount(0); + getMockEndpoint("mock:dead").expectedMessageCount(1); + + template.sendBodyAndHeader("direct:start", "Hello Foo", "foo", "Kaboom"); + + assertMockEndpointsSatisfied(); + + assertTrue(check.get()); + assertFalse(check2.get()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + errorHandler(deadLetterChannel("mock:dead")); + + from("direct:start") + .choice() + .when().method("cbrBean", "checkHeader") + .to("mock:foo") + .when().method("cbrBean", "checkHeader2") + .to("mock:foo2") + .otherwise() + .to("mock:bar") + .end(); + } + }; + } + + public static class MyCBRBean { + + public boolean checkHeader(Exchange exchange) { + check.set(true); + + Message inMsg = exchange.getIn(); + String foo = (String) inMsg.getHeader("foo"); + + if ("Kaboom".equalsIgnoreCase(foo)) { + throw new IllegalArgumentException("Forced"); + } + + return foo.equals("bar"); + } + + public boolean checkHeader2(Exchange exchange) { + check2.set(true); + + Message inMsg = exchange.getIn(); + String foo = (String) inMsg.getHeader("foo"); + + if ("Kaboom".equalsIgnoreCase(foo)) { + throw new IllegalArgumentException("Forced"); + } + + return foo.equals("bar"); + } + } +} +