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/62bbf9b2 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/62bbf9b2 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/62bbf9b2 Branch: refs/heads/master Commit: 62bbf9b2393a450b97fb2782dcdf905bf029a6a2 Parents: d14dfbd Author: Claus Ibsen <[email protected]> Authored: Tue Apr 19 16:38:59 2016 +0200 Committer: Claus Ibsen <[email protected]> Committed: Tue Apr 19 18:16:45 2016 +0200 ---------------------------------------------------------------------- .../model/HystrixConfigurationDefinition.java | 39 +++++++++++++++++++- .../apache/camel/model/HystrixDefinition.java | 28 +------------- .../processor/HystrixProcessorFactory.java | 37 +++++++++++++------ .../processor/HystrixRouteConfigTest.java | 2 +- .../processor/SpringHystrixRouteConfigTest.xml | 2 +- 5 files changed, 67 insertions(+), 41 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/62bbf9b2/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 a47b347..d4e9f52 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 @@ -33,9 +33,14 @@ import org.apache.camel.spi.Metadata; @XmlAccessorType(XmlAccessType.FIELD) public class HystrixConfigurationDefinition extends IdentifiedType { + public static final String DEFAULT_GROUP_KEY = "CamelHystrix"; + @XmlTransient private HystrixDefinition parent; - + @XmlAttribute @Metadata(defaultValue = "CamelHystrix") + private String groupKey; + @XmlAttribute @Metadata(defaultValue = "CamelHystrix") + private String threadPoolKey; @XmlAttribute @Metadata(label = "command", defaultValue = "true") private Boolean circuitBreakerEnabled; @@ -134,6 +139,22 @@ public class HystrixConfigurationDefinition extends IdentifiedType { // Getter/Setter // ------------------------------------------------------------------------- + public String getGroupKey() { + return groupKey; + } + + public void setGroupKey(String groupKey) { + this.groupKey = groupKey; + } + + public String getThreadPoolKey() { + return threadPoolKey; + } + + public void setThreadPoolKey(String threadPoolKey) { + this.threadPoolKey = threadPoolKey; + } + public Boolean getCircuitBreakerEnabled() { return circuitBreakerEnabled; } @@ -363,6 +384,22 @@ public class HystrixConfigurationDefinition extends IdentifiedType { // ------------------------------------------------------------------------- /** + * Sets the group key to use. The default value is CamelHystrix. + */ + public HystrixConfigurationDefinition groupKey(String groupKey) { + setGroupKey(groupKey); + return this; + } + + /** + * Sets the thread pool key to use. Will by default use the same value as groupKey has been configured to use. + */ + public HystrixConfigurationDefinition threadPoolKey(String threadPoolKey) { + setThreadPoolKey(threadPoolKey); + return this; + } + + /** * Whether to use a HystrixCircuitBreaker or not. If false no circuit-breaker logic will be used and all requests permitted. * <p> * This is similar in effect to circuitBreakerForceClosed() except that continues tracking metrics and knowing whether it http://git-wip-us.apache.org/repos/asf/camel/blob/62bbf9b2/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 b5fc6a8..3ca72f5 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 @@ -36,13 +36,6 @@ import org.apache.camel.spi.RouteContext; @XmlAccessorType(XmlAccessType.FIELD) public class HystrixDefinition extends ProcessorDefinition<HystrixDefinition> { - public static final String DEFAULT_GROUP_KEY = "CamelHystrix"; - public static final String DEFAULT_THREAD_POOL_KEY = "CamelHystrix"; - - @XmlAttribute @Metadata(defaultValue = "CamelHystrix") - private String groupKey; - @XmlAttribute @Metadata(defaultValue = "CamelHystrix") - private String threadPoolKey; @XmlElement private HystrixConfigurationDefinition hystrixConfiguration; @XmlElement @@ -127,23 +120,6 @@ public class HystrixDefinition extends ProcessorDefinition<HystrixDefinition> { // Getter/Setter // ------------------------------------------------------------------------- - - public String getGroupKey() { - return groupKey; - } - - public void setGroupKey(String groupKey) { - this.groupKey = groupKey; - } - - public String getThreadPoolKey() { - return threadPoolKey; - } - - public void setThreadPoolKey(String threadPoolKey) { - this.threadPoolKey = threadPoolKey; - } - public HystrixConfigurationDefinition getHystrixConfiguration() { return hystrixConfiguration; } @@ -183,7 +159,7 @@ public class HystrixDefinition extends ProcessorDefinition<HystrixDefinition> { * Sets the group key to use. The default value is CamelHystrix. */ public HystrixDefinition groupKey(String groupKey) { - setGroupKey(groupKey); + configure().groupKey(groupKey); return this; } @@ -191,7 +167,7 @@ public class HystrixDefinition extends ProcessorDefinition<HystrixDefinition> { * Sets the thread pool key to use. The default value is CamelHystrix. */ public HystrixDefinition threadPoolKey(String threadPoolKey) { - setThreadPoolKey(threadPoolKey); + configure().threadPoolKey(threadPoolKey); return this; } http://git-wip-us.apache.org/repos/asf/camel/blob/62bbf9b2/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessorFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessorFactory.java b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessorFactory.java index 5317db0..26ea912 100644 --- a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessorFactory.java +++ b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessorFactory.java @@ -53,14 +53,29 @@ public class HystrixProcessorFactory implements ProcessorFactory { fallback = cb.getFallback().createProcessor(routeContext); } - // group and thread pool keys to use - String groupKey = HystrixDefinition.DEFAULT_GROUP_KEY; - String threadPoolKey = HystrixDefinition.DEFAULT_THREAD_POOL_KEY; - if (cb.getGroupKey() != null) { - groupKey = cb.getGroupKey(); + HystrixConfigurationDefinition config = cb.getHystrixConfiguration(); + HystrixConfigurationDefinition configRef = null; + if (cb.getHystrixConfigurationRef() != null) { + configRef = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), cb.getHystrixConfigurationRef(), HystrixConfigurationDefinition.class); + } + + // group and thread pool keys to use they can be configured on configRef and config, so look there first, and if none then use default + String groupKey = null; + String threadPoolKey = null; + if (configRef != null) { + groupKey = configRef.getGroupKey(); + threadPoolKey = configRef.getThreadPoolKey(); + } + if (config != null && config.getGroupKey() != null) { + groupKey = config.getGroupKey(); + threadPoolKey = config.getThreadPoolKey(); } - if (cb.getThreadPoolKey() != null) { - threadPoolKey = cb.getThreadPoolKey(); + if (groupKey == null) { + groupKey = HystrixConfigurationDefinition.DEFAULT_GROUP_KEY; + } + if (threadPoolKey == null) { + // thread pool key should use same as group key as default + threadPoolKey = groupKey; } // create setter using the default options @@ -73,13 +88,11 @@ public class HystrixProcessorFactory implements ProcessorFactory { setter.andThreadPoolPropertiesDefaults(threadPool); // at first configure any shared options - if (cb.getHystrixConfigurationRef() != null) { - HystrixConfigurationDefinition config = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), cb.getHystrixConfigurationRef(), HystrixConfigurationDefinition.class); - configureHystrix(command, threadPool, config); + if (configRef != null) { + configureHystrix(command, threadPool, configRef); } // then any local configured can override - if (cb.getHystrixConfiguration() != null) { - HystrixConfigurationDefinition config = cb.getHystrixConfiguration(); + if (config != null) { configureHystrix(command, threadPool, config); } http://git-wip-us.apache.org/repos/asf/camel/blob/62bbf9b2/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 index 5faefb9..ebfa660 100644 --- 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 @@ -37,7 +37,7 @@ public class HystrixRouteConfigTest extends CamelTestSupport { @Override public void configure() throws Exception { from("direct:start") - .hystrix().configure().requestCacheEnabled(false).corePoolSize(5).end() + .hystrix().configure().groupKey("myCamelApp").requestCacheEnabled(false).corePoolSize(5).end() .to("direct:foo") .fallback() .transform().constant("Fallback message") http://git-wip-us.apache.org/repos/asf/camel/blob/62bbf9b2/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 index e7e964a..40a8ccb 100644 --- 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 @@ -25,7 +25,7 @@ <route> <from uri="direct:start"/> <hystrix> - <hystrixConfiguration requestCacheEnabled="false" corePoolSize="5"/> + <hystrixConfiguration groupKey="myCamelApp" requestCacheEnabled="false" corePoolSize="5"/> <to uri="direct:foo"/> <fallback> <transform>
