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/37d3315a Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/37d3315a Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/37d3315a Branch: refs/heads/master Commit: 37d3315affdc23862b2c9c5c608b205fced6e4c2 Parents: f180fbe Author: Claus Ibsen <[email protected]> Authored: Tue Apr 19 09:38:51 2016 +0200 Committer: Claus Ibsen <[email protected]> Committed: Tue Apr 19 18:16:45 2016 +0200 ---------------------------------------------------------------------- .../apache/camel/model/HystrixDefinition.java | 24 +++++- components/camel-hystrix/pom.xml | 88 ++++++++++---------- .../hystrix/SpringHystrixRouteFallbackTest.java | 40 +++++++++ .../hystrix/SpringHystrixRouteOkTest.java | 40 +++++++++ .../hystrix/SpringHystrixRouteFallbackTest.xml | 44 ++++++++++ .../hystrix/SpringHystrixRouteOkTest.xml | 46 ++++++++++ 6 files changed, 237 insertions(+), 45 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/37d3315a/camel-core/src/main/java/org/apache/camel/model/HystrixDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/HystrixDefinition.java b/camel-core/src/main/java/org/apache/camel/model/HystrixDefinition.java index 054aa1c..c8abd53 100644 --- a/camel-core/src/main/java/org/apache/camel/model/HystrixDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/HystrixDefinition.java @@ -16,6 +16,7 @@ */ package org.apache.camel.model; +import java.util.Iterator; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; @@ -53,10 +54,14 @@ public class HystrixDefinition extends OutputDefinition<HystrixDefinition> { @Override public void addOutput(ProcessorDefinition<?> output) { - if (fallback != null) { - fallback.addOutput(output); + if (output instanceof FallbackDefinition) { + fallback = (FallbackDefinition) output; } else { - super.addOutput(output); + if (fallback != null) { + fallback.addOutput(output); + } else { + super.addOutput(output); + } } } @@ -77,6 +82,19 @@ public class HystrixDefinition extends OutputDefinition<HystrixDefinition> { this.fallback = fallback; } + protected void preCreateProcessor() { + // move the fallback from outputs to fallback which we need to ensure + // such as when using the XML DSL + Iterator<ProcessorDefinition<?>> it = outputs.iterator(); + while (it.hasNext()) { + ProcessorDefinition<?> out = it.next(); + if (out instanceof FallbackDefinition) { + fallback = (FallbackDefinition) out; + it.remove(); + } + } + } + // Fluent API // ------------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/37d3315a/components/camel-hystrix/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/pom.xml b/components/camel-hystrix/pom.xml index 64ece05..72c38ab 100644 --- a/components/camel-hystrix/pom.xml +++ b/components/camel-hystrix/pom.xml @@ -15,46 +15,50 @@ See the License for the specific language governing permissions and limitations under the License. --> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> - <modelVersion>4.0.0</modelVersion> - - <parent> - <groupId>org.apache.camel</groupId> - <artifactId>components</artifactId> - <version>2.18-SNAPSHOT</version> - </parent> - - <artifactId>camel-hystrix</artifactId> - <packaging>jar</packaging> - <name>Camel :: Hystrix</name> - <description>Camel Hystrix support</description> - - <properties> - <camel.osgi.export.pkg>org.apache.camel.component.hystrix.*</camel.osgi.export.pkg> - <camel.osgi.export.service>org.apache.camel.spi.ComponentResolver;component=hystrix</camel.osgi.export.service> - </properties> - - <dependencies> - <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-core</artifactId> - </dependency> - <dependency> - <groupId>com.netflix.hystrix</groupId> - <artifactId>hystrix-core</artifactId> - <version>${hystrix-version}</version> - </dependency> - - <!-- for testing --> - <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-test</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-log4j12</artifactId> - <scope>test</scope> - </dependency> - </dependencies> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.camel</groupId> + <artifactId>components</artifactId> + <version>2.18-SNAPSHOT</version> + </parent> + + <artifactId>camel-hystrix</artifactId> + <packaging>jar</packaging> + <name>Camel :: Hystrix</name> + <description>Camel Hystrix support</description> + + <properties> + <camel.osgi.export.pkg>org.apache.camel.component.hystrix.*</camel.osgi.export.pkg> + <camel.osgi.export.service>org.apache.camel.spi.ComponentResolver;component=hystrix</camel.osgi.export.service> + </properties> + + <dependencies> + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-core</artifactId> + </dependency> + + <dependency> + <groupId>com.netflix.hystrix</groupId> + <artifactId>hystrix-core</artifactId> + <version>${hystrix-version}</version> + </dependency> + + <!-- for testing --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-spring</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + </project> http://git-wip-us.apache.org/repos/asf/camel/blob/37d3315a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/SpringHystrixRouteFallbackTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/SpringHystrixRouteFallbackTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/SpringHystrixRouteFallbackTest.java new file mode 100644 index 0000000..263ab47 --- /dev/null +++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/SpringHystrixRouteFallbackTest.java @@ -0,0 +1,40 @@ +/** + * 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.test.spring.CamelSpringTestSupport; +import org.junit.Test; +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class SpringHystrixRouteFallbackTest extends CamelSpringTestSupport { + + @Override + protected AbstractApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/component/hystrix/SpringHystrixRouteFallbackTest.xml"); + } + + @Test + public void testHystrix() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Fallback message"); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/37d3315a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/SpringHystrixRouteOkTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/SpringHystrixRouteOkTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/SpringHystrixRouteOkTest.java new file mode 100644 index 0000000..f459a7f --- /dev/null +++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/SpringHystrixRouteOkTest.java @@ -0,0 +1,40 @@ +/** + * 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.test.spring.CamelSpringTestSupport; +import org.junit.Test; +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class SpringHystrixRouteOkTest extends CamelSpringTestSupport { + + @Override + protected AbstractApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/component/hystrix/SpringHystrixRouteOkTest.xml"); + } + + @Test + public void testHystrix() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Bye World"); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/37d3315a/components/camel-hystrix/src/test/resources/org/apache/camel/component/hystrix/SpringHystrixRouteFallbackTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/resources/org/apache/camel/component/hystrix/SpringHystrixRouteFallbackTest.xml b/components/camel-hystrix/src/test/resources/org/apache/camel/component/hystrix/SpringHystrixRouteFallbackTest.xml new file mode 100644 index 0000000..441b8b6 --- /dev/null +++ b/components/camel-hystrix/src/test/resources/org/apache/camel/component/hystrix/SpringHystrixRouteFallbackTest.xml @@ -0,0 +1,44 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="direct:start"/> + <hystrix> + <to uri="direct:foo"/> + <fallback> + <transform> + <constant>Fallback message</constant> + </transform> + </fallback> + </hystrix> + <to uri="mock:result"/> + </route> + + <route> + <from uri="direct:foo"/> + <throwException exceptionType="java.lang.IllegalArgumentException" message="Forced"/> + </route> + </camelContext> + +</beans> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/37d3315a/components/camel-hystrix/src/test/resources/org/apache/camel/component/hystrix/SpringHystrixRouteOkTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/resources/org/apache/camel/component/hystrix/SpringHystrixRouteOkTest.xml b/components/camel-hystrix/src/test/resources/org/apache/camel/component/hystrix/SpringHystrixRouteOkTest.xml new file mode 100644 index 0000000..26fdabc --- /dev/null +++ b/components/camel-hystrix/src/test/resources/org/apache/camel/component/hystrix/SpringHystrixRouteOkTest.xml @@ -0,0 +1,46 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="direct:start"/> + <hystrix> + <to uri="direct:foo"/> + <fallback> + <transform> + <constant>Fallback message</constant> + </transform> + </fallback> + </hystrix> + <to uri="mock:result"/> + </route> + + <route> + <from uri="direct:foo"/> + <transform> + <constant>Bye World</constant> + </transform> + </route> + </camelContext> + +</beans> \ No newline at end of file
