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/ad18d616 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ad18d616 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ad18d616 Branch: refs/heads/master Commit: ad18d61688042c8b46de34518389fb902d4df73f Parents: 425d124 Author: Claus Ibsen <[email protected]> Authored: Mon Apr 18 18:15:28 2016 +0200 Committer: Claus Ibsen <[email protected]> Committed: Tue Apr 19 18:16:45 2016 +0200 ---------------------------------------------------------------------- .../component/hystrix/HystrixProcessor.java | 6 ++- .../HystrixCircuitBreakerFallbackTest.java | 53 ++++++++++++++++++++ .../hystrix/HystrixCircuitBreakerOkTest.java | 53 ++++++++++++++++++++ .../hystrix/HystrixCircuitBreakerTest.java | 50 ------------------ 4 files changed, 111 insertions(+), 51 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/ad18d616/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProcessor.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProcessor.java b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProcessor.java index 6690234..0a9f708 100644 --- a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProcessor.java +++ b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProcessor.java @@ -82,8 +82,10 @@ public class HystrixProcessor extends ServiceSupport implements AsyncProcessor, @Override public boolean process(Exchange exchange, AsyncCallback callback) { - HystrixCommandGroupKey key = HystrixCommandGroupKey.Factory.asKey(id); + // run this as if we run inside try .. catch so there is no regular Camel error handler + exchange.setProperty(Exchange.TRY_ROUTE_BLOCK, true); + HystrixCommandGroupKey key = HystrixCommandGroupKey.Factory.asKey(id); HystrixProcessorCommand command = new HystrixProcessorCommand(key, exchange, callback, processor, fallback); try { command.execute(); @@ -91,6 +93,8 @@ public class HystrixProcessor extends ServiceSupport implements AsyncProcessor, exchange.setException(e); } + exchange.removeProperty(Exchange.TRY_ROUTE_BLOCK); + callback.done(true); return true; } http://git-wip-us.apache.org/repos/asf/camel/blob/ad18d616/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixCircuitBreakerFallbackTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixCircuitBreakerFallbackTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixCircuitBreakerFallbackTest.java new file mode 100644 index 0000000..3c7e45a --- /dev/null +++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixCircuitBreakerFallbackTest.java @@ -0,0 +1,53 @@ +/** + * 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; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class HystrixCircuitBreakerFallbackTest extends CamelTestSupport { + + @Test + public void testHystrixCircuitBreaker() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Fallback message"); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .hystrixCircuitBreaker() + .to("direct:foo") + .fallback() + .transform().constant("Fallback message") + .end() + .to("mock:result"); + + from("direct:foo") + .throwException(new IllegalArgumentException("Forced")); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/ad18d616/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixCircuitBreakerOkTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixCircuitBreakerOkTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixCircuitBreakerOkTest.java new file mode 100644 index 0000000..b34c6c2 --- /dev/null +++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixCircuitBreakerOkTest.java @@ -0,0 +1,53 @@ +/** + * 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; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class HystrixCircuitBreakerOkTest extends CamelTestSupport { + + @Test + public void testHystrixCircuitBreaker() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Bye World"); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .hystrixCircuitBreaker() + .to("direct:foo") + .fallback() + .transform().constant("Fallback message") + .end() + .to("mock:result"); + + from("direct:foo") + .transform().constant("Bye World"); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/ad18d616/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixCircuitBreakerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixCircuitBreakerTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixCircuitBreakerTest.java deleted file mode 100644 index f4c035d..0000000 --- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixCircuitBreakerTest.java +++ /dev/null @@ -1,50 +0,0 @@ -/** - * 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; - -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.test.junit4.CamelTestSupport; -import org.junit.Test; - -public class HystrixCircuitBreakerTest extends CamelTestSupport { - - @Test - public void testHystrixCircuitBreaker() throws Exception { - getMockEndpoint("mock:result").expectedBodiesReceived("Fallback message"); - - template.sendBody("direct:start", "Hello World"); - - assertMockEndpointsSatisfied(); - } - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - @Override - public void configure() throws Exception { - from("direct:start") - .hystrixCircuitBreaker() - .throwException(new IllegalArgumentException("Forced")) - .fallback() - .transform().constant("Fallback message") - .end() - .to("mock:result"); - } - }; - } - -}
