CAMEL-9879: Circuit Breaker EIP - That is using hystrix
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ca8f07c6 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ca8f07c6 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ca8f07c6 Branch: refs/heads/master Commit: ca8f07c6995198938297d254eb13b7b02b9f2120 Parents: a6369c6 Author: Claus Ibsen <[email protected]> Authored: Tue Apr 19 13:32:32 2016 +0200 Committer: Claus Ibsen <[email protected]> Committed: Tue Apr 19 18:16:45 2016 +0200 ---------------------------------------------------------------------- .../processor/HystrixProcessorCommand.java | 9 ++- .../HystrixRouteFallbackDisabledTest.java | 58 ++++++++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/ca8f07c6/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessorCommand.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessorCommand.java b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessorCommand.java index c728d6a..4af9377 100644 --- a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessorCommand.java +++ b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessorCommand.java @@ -93,11 +93,14 @@ public class HystrixProcessorCommand extends HystrixCommand<Exchange> { exchange.setException(e); } - // if we failed then throw an exception - if (exchange.getException() != null) { + // is fallback enabled + Boolean fallbackEnabled = getProperties().fallbackEnabled().get(); + + // if we failed then throw an exception if fallback is enabled + if (fallbackEnabled == null || fallbackEnabled && exchange.getException() != null) { throw exchange.getException(); } - // no errors we are done + // no fallback then we are done try { LOG.debug("Running processor: {} with exchange: {} done", processor, exchange); exchange.removeProperty(Exchange.TRY_ROUTE_BLOCK); http://git-wip-us.apache.org/repos/asf/camel/blob/ca8f07c6/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteFallbackDisabledTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteFallbackDisabledTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteFallbackDisabledTest.java new file mode 100644 index 0000000..9e81ce9 --- /dev/null +++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteFallbackDisabledTest.java @@ -0,0 +1,58 @@ +/** + * 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.component.hystrix.processor; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class HystrixRouteFallbackDisabledTest extends CamelTestSupport { + + @Test + public void testHystrix() throws Exception { + getMockEndpoint("mock:result").expectedMessageCount(0); + + try { + template.sendBody("direct:start", "Hello World"); + fail("Should have thrown exception"); + } catch (Exception e) { + assertIsInstanceOf(IllegalArgumentException.class, e.getCause()); + } + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .hystrix().configure().fallbackEnabled(false).end() + .to("direct:foo") + .fallback() + .transform().constant("Fallback message") + .end() + .to("mock:result"); + + from("direct:foo").errorHandler(noErrorHandler()) + .throwException(new IllegalArgumentException("Forced")); + } + }; + } + +}
