CAMEL-6889: CBR - Should break out if exception was thrown when evaluating predicate
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c4b37647 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c4b37647 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c4b37647 Branch: refs/heads/camel-2.12.x Commit: c4b376472c71febc05e40542ea40720c5153179c Parents: 5d6c303 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:33 2013 +0200 ---------------------------------------------------------------------- .../apache/camel/processor/ChoiceProcessor.java | 16 ++- .../CBRPredicateBeanThrowExceptionTest.java | 126 +++++++++++++++++++ 2 files changed, 138 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/c4b37647/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 0310c9a..44f4b10 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 @@ -30,6 +30,10 @@ import org.apache.camel.support.ServiceSupport; import org.apache.camel.util.AsyncProcessorConverterHelper; import org.apache.camel.util.AsyncProcessorHelper; 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 @@ -39,6 +43,7 @@ import org.apache.camel.util.ServiceHelper; * @version */ public class ChoiceProcessor extends ServiceSupport implements AsyncProcessor, Navigate<Processor>, Traceable { + private static final Logger LOG = LoggerFactory.getLogger(ChoiceProcessor.class); private final List<Processor> filters; private final Processor otherwise; @@ -84,13 +89,16 @@ public class ChoiceProcessor extends ServiceSupport implements AsyncProcessor, N try { matches = filter.getPredicate().matches(exchange); exchange.setProperty(Exchange.FILTER_MATCHED, matches); + // as we have pre evaluated the predicate then use its processor directly when routing + processor = filter.getProcessor(); } catch (Throwable e) { exchange.setException(e); - choiceCallback.done(true); - return true; } - // as we have pre evaluated the predicate then use its processor directly when routing - processor = filter.getProcessor(); + } + + // check for error if so we should break out + if (!continueProcessing(exchange, "so breaking out of choice", LOG)) { + break; } // if we did not match then continue to next filter http://git-wip-us.apache.org/repos/asf/camel/blob/c4b37647/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"); + } + } +} +