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/c89b23aa Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c89b23aa Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c89b23aa Branch: refs/heads/master Commit: c89b23aadcf226f41ce8e7d65bec53a998eaee28 Parents: 930d49a Author: Claus Ibsen <[email protected]> Authored: Mon Apr 18 16:37:51 2016 +0200 Committer: Claus Ibsen <[email protected]> Committed: Tue Apr 19 18:16:45 2016 +0200 ---------------------------------------------------------------------- .../apache/camel/model/FallbackDefinition.java | 60 ++++++++++++ .../model/HystrixCircuitBreakerDefinition.java | 96 ++++++++++++++++++++ .../apache/camel/model/ProcessorDefinition.java | 11 +++ .../CircuitBreakerLoadBalancerDefinition.java | 2 +- .../HystrixCircuitBreakerProcessor.java | 96 ++++++++++++++++++++ .../resources/org/apache/camel/model/jaxb.index | 2 + .../processor/HystrixCircuitBreakerTest.java | 50 ++++++++++ 7 files changed, 316 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/c89b23aa/camel-core/src/main/java/org/apache/camel/model/FallbackDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/FallbackDefinition.java b/camel-core/src/main/java/org/apache/camel/model/FallbackDefinition.java new file mode 100644 index 0000000..301c6d1 --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/model/FallbackDefinition.java @@ -0,0 +1,60 @@ +/** + * 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.model; + +import java.util.List; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + +import org.apache.camel.Processor; +import org.apache.camel.spi.Metadata; +import org.apache.camel.spi.RouteContext; +import org.apache.camel.util.CollectionStringBuffer; + +/** + * Route to be executed when circuit breaker executes fallback + */ +@Metadata(label = "eip,routing,circuitbreaker") +@XmlRootElement(name = "fallback") +@XmlAccessorType(XmlAccessType.FIELD) +public class FallbackDefinition extends OutputDefinition<FallbackDefinition> { + + public FallbackDefinition() { + } + + @Override + public String toString() { + return "Fallback[" + getOutputs() + "]"; + } + + @Override + public Processor createProcessor(RouteContext routeContext) throws Exception { + return this.createChildProcessor(routeContext, false); + } + + @Override + public String getLabel() { + CollectionStringBuffer buffer = new CollectionStringBuffer("fallback["); + List<ProcessorDefinition<?>> list = getOutputs(); + for (ProcessorDefinition<?> type : list) { + buffer.append(type.getLabel()); + } + buffer.append("]"); + return buffer.toString(); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/c89b23aa/camel-core/src/main/java/org/apache/camel/model/HystrixCircuitBreakerDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/HystrixCircuitBreakerDefinition.java b/camel-core/src/main/java/org/apache/camel/model/HystrixCircuitBreakerDefinition.java new file mode 100644 index 0000000..c7b4cd4 --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/model/HystrixCircuitBreakerDefinition.java @@ -0,0 +1,96 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.model; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +import org.apache.camel.Processor; +import org.apache.camel.processor.HystrixCircuitBreakerProcessor; +import org.apache.camel.spi.Metadata; +import org.apache.camel.spi.RouteContext; + +@Metadata(label = "eip,routing,circuitbreaker") +@XmlRootElement(name = "hystrixCircuitBreaker") +@XmlAccessorType(XmlAccessType.FIELD) +public class HystrixCircuitBreakerDefinition extends OutputDefinition<HystrixCircuitBreakerDefinition> { + + // TODO: we can rename to circuitBreaker and then deprecated the CB in the load balancer + // the trick is to avoid a clash in the generated xml schema + // so for know we call it hystrixCircuitBreaker + + @XmlElement + private FallbackDefinition fallback; + + public HystrixCircuitBreakerDefinition() { + } + + @Override + public String toString() { + return "HystrixCircuitBreaker[" + getOutputs() + "]"; + } + + @Override + public String getLabel() { + return "hystrixCircuitBreaker"; + } + + @Override + public Processor createProcessor(RouteContext routeContext) throws Exception { + Processor children = this.createChildProcessor(routeContext, true); + + Processor fallbackProcessor = null; + if (fallback != null) { + fallbackProcessor = createProcessor(routeContext, fallback); + } + return new HystrixCircuitBreakerProcessor(children, fallbackProcessor); + } + + @Override + public void addOutput(ProcessorDefinition<?> output) { + if (fallback != null) { + fallback.addOutput(output); + } else { + super.addOutput(output); + } + } + + @Override + public ProcessorDefinition<?> end() { + if (fallback != null) { + // end fallback as well + fallback.end(); + } + return super.end(); + } + + // Fluent API + // ------------------------------------------------------------------------- + + /** + * Sets the otherwise node + * + * @return the builder + */ + public HystrixCircuitBreakerDefinition fallback() { + fallback = new FallbackDefinition(); + return this; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/c89b23aa/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java b/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java index 1ba4bb5..aa82e88 100644 --- a/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java @@ -1470,6 +1470,17 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type> } /** + * Creates a Circuit Breaker EIP that is using Hystrix. + * + * @return the builder + */ + public HystrixCircuitBreakerDefinition hystrixCircuitBreaker() { + HystrixCircuitBreakerDefinition answer = new HystrixCircuitBreakerDefinition(); + addOutput(answer); + return answer; + } + + /** * <a href="http://camel.apache.org/load-balancer.html">Load Balancer EIP:</a> * Creates a loadbalance * http://git-wip-us.apache.org/repos/asf/camel/blob/c89b23aa/camel-core/src/main/java/org/apache/camel/model/loadbalancer/CircuitBreakerLoadBalancerDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/loadbalancer/CircuitBreakerLoadBalancerDefinition.java b/camel-core/src/main/java/org/apache/camel/model/loadbalancer/CircuitBreakerLoadBalancerDefinition.java index 963339e..75b0e26 100644 --- a/camel-core/src/main/java/org/apache/camel/model/loadbalancer/CircuitBreakerLoadBalancerDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/loadbalancer/CircuitBreakerLoadBalancerDefinition.java @@ -41,7 +41,7 @@ import org.apache.camel.util.ObjectHelper; * timeout is reached. After this timeout is reached, if there is a new call, it will pass and if the result is * success the Circuit Breaker will move to closed state, or to open state if there was an error. */ -@Metadata(label = "eip,routing,loadbalance") +@Metadata(label = "eip,routing,loadbalance,circuitbreaker") @XmlRootElement(name = "circuitBreaker") @XmlAccessorType(XmlAccessType.FIELD) public class CircuitBreakerLoadBalancerDefinition extends LoadBalancerDefinition { http://git-wip-us.apache.org/repos/asf/camel/blob/c89b23aa/camel-core/src/main/java/org/apache/camel/processor/HystrixCircuitBreakerProcessor.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/processor/HystrixCircuitBreakerProcessor.java b/camel-core/src/main/java/org/apache/camel/processor/HystrixCircuitBreakerProcessor.java new file mode 100644 index 0000000..79d5037 --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/processor/HystrixCircuitBreakerProcessor.java @@ -0,0 +1,96 @@ +/** + * 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.ArrayList; +import java.util.List; + +import org.apache.camel.AsyncCallback; +import org.apache.camel.AsyncProcessor; +import org.apache.camel.Exchange; +import org.apache.camel.Navigate; +import org.apache.camel.Processor; +import org.apache.camel.spi.IdAware; +import org.apache.camel.support.ServiceSupport; +import org.apache.camel.util.AsyncProcessorHelper; + +public class HystrixCircuitBreakerProcessor extends ServiceSupport implements AsyncProcessor, Navigate<Processor>, org.apache.camel.Traceable, IdAware { + + private String id; + private final Processor processor; + private final Processor fallback; + + public HystrixCircuitBreakerProcessor(Processor processor, Processor fallback) { + this.processor = processor; + this.fallback = fallback; + } + + @Override + public void setId(String id) { + this.id = id; + } + + @Override + public String getId() { + return id; + } + + @Override + public String getTraceLabel() { + return "hystrixCircuitBreaker"; + } + + @Override + public List<Processor> next() { + if (!hasNext()) { + return null; + } + List<Processor> answer = new ArrayList<Processor>(); + answer.add(processor); + if (fallback != null) { + answer.add(fallback); + } + return answer; + } + + @Override + public boolean hasNext() { + return true; + } + + @Override + public void process(Exchange exchange) throws Exception { + AsyncProcessorHelper.process(this, exchange); + } + + @Override + public boolean process(Exchange exchange, AsyncCallback callback) { + // TODO: use camel-hystrix to process this + callback.done(true); + return true; + } + + @Override + protected void doStart() throws Exception { + // noop + } + + @Override + protected void doStop() throws Exception { + // noop + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/c89b23aa/camel-core/src/main/resources/org/apache/camel/model/jaxb.index ---------------------------------------------------------------------- diff --git a/camel-core/src/main/resources/org/apache/camel/model/jaxb.index b/camel-core/src/main/resources/org/apache/camel/model/jaxb.index index 0fb9ac1..6232753 100644 --- a/camel-core/src/main/resources/org/apache/camel/model/jaxb.index +++ b/camel-core/src/main/resources/org/apache/camel/model/jaxb.index @@ -27,9 +27,11 @@ DescriptionDefinition DynamicRouterDefinition EnrichDefinition ExpressionSubElementDefinition +FallbackDefinition FilterDefinition FinallyDefinition FromDefinition +HystrixCircuitBreakerDefinition IdempotentConsumerDefinition InOnlyDefinition InOutDefinition http://git-wip-us.apache.org/repos/asf/camel/blob/c89b23aa/camel-core/src/test/java/org/apache/camel/processor/HystrixCircuitBreakerTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/processor/HystrixCircuitBreakerTest.java b/camel-core/src/test/java/org/apache/camel/processor/HystrixCircuitBreakerTest.java new file mode 100644 index 0000000..a44ddd5 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/processor/HystrixCircuitBreakerTest.java @@ -0,0 +1,50 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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 org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; + +public class HystrixCircuitBreakerTest extends ContextTestSupport { + + public void testHystrixCircuitBreaker() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Hello 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") + .throwException(new IllegalArgumentException("Forced")); + } + }; + } +}
