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/89635686 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/89635686 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/89635686 Branch: refs/heads/master Commit: 896356867058a7ce282ca8972d73e9d77e6c7437 Parents: a192ff68 Author: Claus Ibsen <[email protected]> Authored: Tue Apr 19 14:02:26 2016 +0200 Committer: Claus Ibsen <[email protected]> Committed: Tue Apr 19 18:16:45 2016 +0200 ---------------------------------------------------------------------- .../model/HystrixConfigurationDefinition.java | 2 +- .../blueprint/CamelContextFactoryBean.java | 2 + .../processor/HystrixRouteConfigTest.java | 53 ++++++++++++++++++++ .../SpringHystrixRouteConfigRefTest.java | 40 +++++++++++++++ .../processor/SpringHystrixRouteConfigTest.java | 40 +++++++++++++++ .../SpringHystrixRouteConfigRefTest.xml | 49 ++++++++++++++++++ .../processor/SpringHystrixRouteConfigTest.xml | 47 +++++++++++++++++ .../camel/spring/CamelContextFactoryBean.java | 2 + .../spring/handler/CamelNamespaceHandler.java | 4 +- 9 files changed, 237 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/89635686/camel-core/src/main/java/org/apache/camel/model/HystrixConfigurationDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/HystrixConfigurationDefinition.java b/camel-core/src/main/java/org/apache/camel/model/HystrixConfigurationDefinition.java index 5ea084e..c20d3ce 100644 --- a/camel-core/src/main/java/org/apache/camel/model/HystrixConfigurationDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/HystrixConfigurationDefinition.java @@ -31,7 +31,7 @@ import org.apache.camel.spi.Metadata; @Metadata(label = "eip,routing,circuitbreaker") @XmlRootElement(name = "hystrixConfiguration") @XmlAccessorType(XmlAccessType.FIELD) -public class HystrixConfigurationDefinition { +public class HystrixConfigurationDefinition extends IdentifiedType { @XmlTransient private HystrixDefinition parent; http://git-wip-us.apache.org/repos/asf/camel/blob/89635686/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java ---------------------------------------------------------------------- diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java index aaebc7f..0c6e832 100644 --- a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java +++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java @@ -44,6 +44,7 @@ import org.apache.camel.core.xml.CamelPropertyPlaceholderDefinition; import org.apache.camel.core.xml.CamelServiceExporterDefinition; import org.apache.camel.core.xml.CamelStreamCachingStrategyDefinition; import org.apache.camel.model.ContextScanDefinition; +import org.apache.camel.model.HystrixConfigurationDefinition; import org.apache.camel.model.InterceptDefinition; import org.apache.camel.model.InterceptFromDefinition; import org.apache.camel.model.InterceptSendToEndpointDefinition; @@ -140,6 +141,7 @@ public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<Blu @XmlElement(name = "streamCaching", type = CamelStreamCachingStrategyDefinition.class, required = false) private CamelStreamCachingStrategyDefinition camelStreamCachingStrategy; @XmlElements({ + @XmlElement(name = "hystrixConfiguration", type = HystrixConfigurationDefinition.class, required = false), @XmlElement(name = "template", type = CamelProducerTemplateFactoryBean.class, required = false), @XmlElement(name = "consumerTemplate", type = CamelConsumerTemplateFactoryBean.class, required = false), @XmlElement(name = "proxy", type = CamelProxyFactoryBean.class, required = false), http://git-wip-us.apache.org/repos/asf/camel/blob/89635686/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteConfigTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteConfigTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteConfigTest.java new file mode 100644 index 0000000..5faefb9 --- /dev/null +++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixRouteConfigTest.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.processor; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class HystrixRouteConfigTest extends CamelTestSupport { + + @Test + public void testHystrix() 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") + .hystrix().configure().requestCacheEnabled(false).corePoolSize(5).end() + .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/89635686/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/SpringHystrixRouteConfigRefTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/SpringHystrixRouteConfigRefTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/SpringHystrixRouteConfigRefTest.java new file mode 100644 index 0000000..dcf9dc0 --- /dev/null +++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/SpringHystrixRouteConfigRefTest.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.processor; + +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 SpringHystrixRouteConfigRefTest extends CamelSpringTestSupport { + + @Override + protected AbstractApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/component/hystrix/processor/SpringHystrixRouteConfigRefTest.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/89635686/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/SpringHystrixRouteConfigTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/SpringHystrixRouteConfigTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/SpringHystrixRouteConfigTest.java new file mode 100644 index 0000000..d0a52b1 --- /dev/null +++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/SpringHystrixRouteConfigTest.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.processor; + +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 SpringHystrixRouteConfigTest extends CamelSpringTestSupport { + + @Override + protected AbstractApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/component/hystrix/processor/SpringHystrixRouteConfigTest.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/89635686/components/camel-hystrix/src/test/resources/org/apache/camel/component/hystrix/processor/SpringHystrixRouteConfigRefTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/resources/org/apache/camel/component/hystrix/processor/SpringHystrixRouteConfigRefTest.xml b/components/camel-hystrix/src/test/resources/org/apache/camel/component/hystrix/processor/SpringHystrixRouteConfigRefTest.xml new file mode 100644 index 0000000..887e81e --- /dev/null +++ b/components/camel-hystrix/src/test/resources/org/apache/camel/component/hystrix/processor/SpringHystrixRouteConfigRefTest.xml @@ -0,0 +1,49 @@ +<?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"> + + <hystrixConfiguration id="hysConfig" requestCacheEnabled="false" corePoolSize="5"/> + + <route> + <from uri="direct:start"/> + <hystrix hystrixConfigurationRef="hysConfig"> + <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 http://git-wip-us.apache.org/repos/asf/camel/blob/89635686/components/camel-hystrix/src/test/resources/org/apache/camel/component/hystrix/processor/SpringHystrixRouteConfigTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/resources/org/apache/camel/component/hystrix/processor/SpringHystrixRouteConfigTest.xml b/components/camel-hystrix/src/test/resources/org/apache/camel/component/hystrix/processor/SpringHystrixRouteConfigTest.xml new file mode 100644 index 0000000..e7e964a --- /dev/null +++ b/components/camel-hystrix/src/test/resources/org/apache/camel/component/hystrix/processor/SpringHystrixRouteConfigTest.xml @@ -0,0 +1,47 @@ +<?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> + <hystrixConfiguration requestCacheEnabled="false" corePoolSize="5"/> + <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 http://git-wip-us.apache.org/repos/asf/camel/blob/89635686/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java index 4c6af83..6179724 100644 --- a/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java +++ b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java @@ -45,6 +45,7 @@ import org.apache.camel.core.xml.CamelProxyFactoryDefinition; import org.apache.camel.core.xml.CamelServiceExporterDefinition; import org.apache.camel.core.xml.CamelStreamCachingStrategyDefinition; import org.apache.camel.model.ContextScanDefinition; +import org.apache.camel.model.HystrixConfigurationDefinition; import org.apache.camel.model.InterceptDefinition; import org.apache.camel.model.InterceptFromDefinition; import org.apache.camel.model.InterceptSendToEndpointDefinition; @@ -150,6 +151,7 @@ public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<Spr @XmlElement(name = "jmxAgent", type = CamelJMXAgentDefinition.class, required = false) private CamelJMXAgentDefinition camelJMXAgent; @XmlElements({ + @XmlElement(name = "hystrixConfiguration", type = HystrixConfigurationDefinition.class, required = false), @XmlElement(name = "template", type = CamelProducerTemplateFactoryBean.class, required = false), @XmlElement(name = "consumerTemplate", type = CamelConsumerTemplateFactoryBean.class, required = false), @XmlElement(name = "proxy", type = CamelProxyFactoryDefinition.class, required = false), http://git-wip-us.apache.org/repos/asf/camel/blob/89635686/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java b/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java index 4979254..c6b938d 100644 --- a/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java +++ b/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java @@ -26,6 +26,7 @@ import javax.xml.bind.Binder; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; +import org.apache.camel.model.HystrixConfigurationDefinition; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; @@ -138,10 +139,11 @@ public class CamelNamespaceHandler extends NamespaceHandlerSupport { addBeanDefinitionParser("threadPool", CamelThreadPoolFactoryBean.class, true, true); addBeanDefinitionParser("redeliveryPolicyProfile", CamelRedeliveryPolicyFactoryBean.class, true, true); - // jmx agent, stream caching, and property placeholder cannot be used outside of the camel context + // jmx agent, stream caching, hystrix configuration and property placeholder cannot be used outside of the camel context addBeanDefinitionParser("jmxAgent", CamelJMXAgentDefinition.class, false, false); addBeanDefinitionParser("streamCaching", CamelStreamCachingStrategyDefinition.class, false, false); addBeanDefinitionParser("propertyPlaceholder", CamelPropertyPlaceholderDefinition.class, false, false); + addBeanDefinitionParser("hystrixConfiguration", HystrixConfigurationDefinition.class, false, false); // errorhandler could be the sub element of camelContext or defined outside camelContext BeanDefinitionParser errorHandlerParser = new ErrorHandlerDefinitionParser();
