This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-14182 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 478fed0387ae73f74e955a534992bd4cdf7f7a9a Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Fri Nov 15 09:59:04 2019 +0100 CAMEL-14182: Make Hystrix EIP general as Circuit Breaker EIP and allow to plugin other implementations. WIP --- ...finition.java => CircuitBreakerDefinition.java} | 84 ++++++++-------------- .../model/HystrixConfigurationDefinition.java | 6 +- .../apache/camel/model/ProcessorDefinition.java | 20 +++--- ...trixReifier.java => CircuitBreakerReifier.java} | 10 +-- .../org/apache/camel/reifier/ProcessorReifier.java | 16 ++--- 5 files changed, 55 insertions(+), 81 deletions(-) diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/model/HystrixDefinition.java b/core/camel-core-engine/src/main/java/org/apache/camel/model/CircuitBreakerDefinition.java similarity index 69% rename from core/camel-core-engine/src/main/java/org/apache/camel/model/HystrixDefinition.java rename to core/camel-core-engine/src/main/java/org/apache/camel/model/CircuitBreakerDefinition.java index 4087543..c16e1b0 100644 --- a/core/camel-core-engine/src/main/java/org/apache/camel/model/HystrixDefinition.java +++ b/core/camel-core-engine/src/main/java/org/apache/camel/model/CircuitBreakerDefinition.java @@ -1,13 +1,13 @@ -/* +/** * 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 - * + * <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. @@ -19,7 +19,6 @@ package org.apache.camel.model; import java.util.ArrayList; import java.util.Iterator; import java.util.List; - import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; @@ -30,39 +29,36 @@ import javax.xml.bind.annotation.XmlTransient; import org.apache.camel.spi.Metadata; -/** - * Hystrix Circuit Breaker EIP - */ @Metadata(label = "eip,routing,circuitbreaker") -@XmlRootElement(name = "hystrix") +@XmlRootElement(name = "circuitBreaker") @XmlAccessorType(XmlAccessType.FIELD) -public class HystrixDefinition extends ProcessorDefinition<HystrixDefinition> implements OutputNode { +public class CircuitBreakerDefinition extends ProcessorDefinition<CircuitBreakerDefinition> implements OutputNode { @XmlElement private HystrixConfigurationDefinition hystrixConfiguration; + @XmlAttribute + private String configurationRef; @XmlElementRef private List<ProcessorDefinition<?>> outputs = new ArrayList<>(); @XmlTransient private OnFallbackDefinition onFallback; - @XmlAttribute - private String hystrixConfigurationRef; - public HystrixDefinition() { + public CircuitBreakerDefinition() { } @Override public String toString() { - return "Hystrix[" + getOutputs() + "]"; + return "CircuitBreaker[" + getOutputs() + "]"; } @Override public String getShortName() { - return "hystrix"; + return "circuitBreaker"; } @Override public String getLabel() { - return "hystrix"; + return "circuitBreaker"; } @Override @@ -126,15 +122,16 @@ public class HystrixDefinition extends ProcessorDefinition<HystrixDefinition> im this.hystrixConfiguration = hystrixConfiguration; } - public String getHystrixConfigurationRef() { - return hystrixConfigurationRef; + public String getConfigurationRef() { + return configurationRef; } /** - * Refers to a Hystrix configuration to use for configuring the Hystrix EIP. + * Refers to a circuit breaker configuration (such as hystrix, resillient4j, or microprofile-fault-tolerance) + * to use for configuring the circuit breaker EIP. */ - public void setHystrixConfigurationRef(String hystrixConfigurationRef) { - this.hystrixConfigurationRef = hystrixConfigurationRef; + public void setConfigurationRef(String configurationRef) { + this.configurationRef = configurationRef; } public OnFallbackDefinition getOnFallback() { @@ -149,26 +146,10 @@ public class HystrixDefinition extends ProcessorDefinition<HystrixDefinition> im // ------------------------------------------------------------------------- /** - * Sets the group key to use. The default value is CamelHystrix. - */ - public HystrixDefinition groupKey(String groupKey) { - hystrixConfiguration().groupKey(groupKey); - return this; - } - - /** - * Sets the thread pool key to use. The default value is CamelHystrix. - */ - public HystrixDefinition threadPoolKey(String threadPoolKey) { - hystrixConfiguration().threadPoolKey(threadPoolKey); - return this; - } - - /** - * Configures the Hystrix EIP + * Configures the circuit breaker to use Hystrix. * <p/> * Use <tt>end</tt> when configuration is complete, to return back to the - * Hystrix EIP. + * Circuit Breaker EIP. */ public HystrixConfigurationDefinition hystrixConfiguration() { hystrixConfiguration = hystrixConfiguration == null ? new HystrixConfigurationDefinition(this) : hystrixConfiguration; @@ -176,46 +157,41 @@ public class HystrixDefinition extends ProcessorDefinition<HystrixDefinition> im } /** - * Configures the Hystrix EIP using the given configuration + * Configures the circuit breaker to use Hystrix with the given configuration. */ - public HystrixDefinition hystrixConfiguration(HystrixConfigurationDefinition configuration) { + public CircuitBreakerDefinition hystrixConfiguration(HystrixConfigurationDefinition configuration) { hystrixConfiguration = configuration; return this; } /** - * Refers to a Hystrix configuration to use for configuring the Hystrix EIP. + * Refers to a configuration to use for configuring the circuit breaker. */ - public HystrixDefinition hystrixConfiguration(String ref) { - hystrixConfigurationRef = ref; + public CircuitBreakerDefinition configuration(String ref) { + configurationRef = ref; return this; } /** - * The Hystrix fallback route path to execute that does <b>not</b> go over + * The fallback route path to execute that does <b>not</b> go over * the network. * <p> * This should be a static or cached result that can immediately be returned * upon failure. If the fallback requires network connection then use * {@link #onFallbackViaNetwork()}. */ - public HystrixDefinition onFallback() { + public CircuitBreakerDefinition onFallback() { onFallback = new OnFallbackDefinition(); onFallback.setParent(this); return this; } /** - * The Hystrix fallback route path to execute that will go over the network. + * The fallback route path to execute that will go over the network. * <p/> - * If the fallback will go over the network it is another possible point of - * failure and so it also needs to be wrapped by a HystrixCommand. It is - * important to execute the fallback command on a separate thread-pool, - * otherwise if the main command were to become latent and fill the - * thread-pool this would prevent the fallback from running if the two - * commands share the same pool. + * If the fallback will go over the network it is another possible point of failure. */ - public HystrixDefinition onFallbackViaNetwork() { + public CircuitBreakerDefinition onFallbackViaNetwork() { onFallback = new OnFallbackDefinition(); onFallback.setFallbackViaNetwork(true); onFallback.setParent(this); diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/model/HystrixConfigurationDefinition.java b/core/camel-core-engine/src/main/java/org/apache/camel/model/HystrixConfigurationDefinition.java index a3ff058..75b9f7d 100644 --- a/core/camel-core-engine/src/main/java/org/apache/camel/model/HystrixConfigurationDefinition.java +++ b/core/camel-core-engine/src/main/java/org/apache/camel/model/HystrixConfigurationDefinition.java @@ -35,12 +35,12 @@ public class HystrixConfigurationDefinition extends HystrixConfigurationCommon { public static final String DEFAULT_GROUP_KEY = "CamelHystrix"; @XmlTransient - private HystrixDefinition parent; + private CircuitBreakerDefinition parent; public HystrixConfigurationDefinition() { } - public HystrixConfigurationDefinition(HystrixDefinition parent) { + public HystrixConfigurationDefinition(CircuitBreakerDefinition parent) { this.parent = parent; } @@ -375,7 +375,7 @@ public class HystrixConfigurationDefinition extends HystrixConfigurationCommon { /** * End of configuration. */ - public HystrixDefinition end() { + public CircuitBreakerDefinition end() { return parent; } diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/model/ProcessorDefinition.java b/core/camel-core-engine/src/main/java/org/apache/camel/model/ProcessorDefinition.java index 03fa644..d28282f 100644 --- a/core/camel-core-engine/src/main/java/org/apache/camel/model/ProcessorDefinition.java +++ b/core/camel-core-engine/src/main/java/org/apache/camel/model/ProcessorDefinition.java @@ -1188,22 +1188,22 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type> } /** - * Ends the current block and returns back to the {@link HystrixDefinition - * hystrix()} DSL. + * Ends the current block and returns back to the {@link CircuitBreakerDefinition + * circuitBreaker()} DSL. * * @return the builder */ - public HystrixDefinition endHystrix() { + public CircuitBreakerDefinition endCircuitBreaker() { ProcessorDefinition<?> def = this; // are we already a try? - if (def instanceof HystrixDefinition) { - return (HystrixDefinition)def; + if (def instanceof CircuitBreakerDefinition) { + return (CircuitBreakerDefinition)def; } // okay end this and get back to the try def = end(); - return (HystrixDefinition)def; + return (CircuitBreakerDefinition)def; } /** @@ -1351,14 +1351,14 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type> } /** - * Creates a Hystrix Circuit Breaker EIP. + * Creates a Circuit Breaker EIP. * <p/> - * This requires having camel-hystrix on the classpath. + * This requires having an implementation on the classpath such as camel-hystrix, or camel-microprofile-fault-tolerance. * * @return the builder */ - public HystrixDefinition hystrix() { - HystrixDefinition answer = new HystrixDefinition(); + public CircuitBreakerDefinition circuitBreaker() { + CircuitBreakerDefinition answer = new CircuitBreakerDefinition(); addOutput(answer); return answer; } diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/reifier/HystrixReifier.java b/core/camel-core-engine/src/main/java/org/apache/camel/reifier/CircuitBreakerReifier.java similarity index 77% rename from core/camel-core-engine/src/main/java/org/apache/camel/reifier/HystrixReifier.java rename to core/camel-core-engine/src/main/java/org/apache/camel/reifier/CircuitBreakerReifier.java index 464f027..830b0e7 100644 --- a/core/camel-core-engine/src/main/java/org/apache/camel/reifier/HystrixReifier.java +++ b/core/camel-core-engine/src/main/java/org/apache/camel/reifier/CircuitBreakerReifier.java @@ -17,19 +17,19 @@ package org.apache.camel.reifier; import org.apache.camel.Processor; -import org.apache.camel.model.HystrixDefinition; +import org.apache.camel.model.CircuitBreakerDefinition; import org.apache.camel.model.ProcessorDefinition; import org.apache.camel.spi.RouteContext; -public class HystrixReifier extends ProcessorReifier<HystrixDefinition> { +public class CircuitBreakerReifier extends ProcessorReifier<CircuitBreakerDefinition> { - public HystrixReifier(ProcessorDefinition<?> definition) { - super(HystrixDefinition.class.cast(definition)); + public CircuitBreakerReifier(ProcessorDefinition<?> definition) { + super(CircuitBreakerDefinition.class.cast(definition)); } @Override public Processor createProcessor(RouteContext routeContext) throws Exception { - throw new IllegalStateException("Cannot find camel-hystrix on the classpath."); + throw new IllegalStateException("Cannot find camel-hystrix or camel-microprofile-fault-tolerance on the classpath."); } } diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/reifier/ProcessorReifier.java b/core/camel-core-engine/src/main/java/org/apache/camel/reifier/ProcessorReifier.java index 8fc7382..3c327ef 100644 --- a/core/camel-core-engine/src/main/java/org/apache/camel/reifier/ProcessorReifier.java +++ b/core/camel-core-engine/src/main/java/org/apache/camel/reifier/ProcessorReifier.java @@ -31,6 +31,7 @@ import org.apache.camel.model.AggregateDefinition; import org.apache.camel.model.BeanDefinition; import org.apache.camel.model.CatchDefinition; import org.apache.camel.model.ChoiceDefinition; +import org.apache.camel.model.CircuitBreakerDefinition; import org.apache.camel.model.ClaimCheckDefinition; import org.apache.camel.model.ConvertBodyDefinition; import org.apache.camel.model.DelayDefinition; @@ -39,7 +40,6 @@ import org.apache.camel.model.EnrichDefinition; import org.apache.camel.model.ExpressionNode; import org.apache.camel.model.FilterDefinition; import org.apache.camel.model.FinallyDefinition; -import org.apache.camel.model.HystrixDefinition; import org.apache.camel.model.IdempotentConsumerDefinition; import org.apache.camel.model.InOnlyDefinition; import org.apache.camel.model.InOutDefinition; @@ -120,6 +120,7 @@ public abstract class ProcessorReifier<T extends ProcessorDefinition<?>> { map.put(BeanDefinition.class, BeanReifier::new); map.put(CatchDefinition.class, CatchReifier::new); map.put(ChoiceDefinition.class, ChoiceReifier::new); + map.put(CircuitBreakerDefinition.class, CircuitBreakerReifier::new); map.put(ClaimCheckDefinition.class, ClaimCheckReifier::new); map.put(ConvertBodyDefinition.class, ConvertBodyReifier::new); map.put(DelayDefinition.class, DelayReifier::new); @@ -127,7 +128,6 @@ public abstract class ProcessorReifier<T extends ProcessorDefinition<?>> { map.put(EnrichDefinition.class, EnrichReifier::new); map.put(FilterDefinition.class, FilterReifier::new); map.put(FinallyDefinition.class, FinallyReifier::new); - map.put(HystrixDefinition.class, HystrixReifier::new); map.put(IdempotentConsumerDefinition.class, IdempotentConsumerReifier::new); map.put(InOnlyDefinition.class, SendReifier::new); map.put(InOutDefinition.class, SendReifier::new); @@ -351,16 +351,14 @@ public abstract class ProcessorReifier<T extends ProcessorDefinition<?>> { log.trace("{} is part of OnException so no error handler is applied", definition); // do not use error handler for onExceptions blocks as it will // handle errors itself - } else if (definition instanceof HystrixDefinition || ProcessorDefinitionHelper.isParentOfType(HystrixDefinition.class, definition, true)) { - // do not use error handler for hystrix as it offers circuit - // breaking with fallback for its outputs - // however if inherit error handler is enabled, we need to wrap an - // error handler on the hystrix parent + } else if (definition instanceof CircuitBreakerDefinition || ProcessorDefinitionHelper.isParentOfType(CircuitBreakerDefinition.class, definition, true)) { + // do not use error handler for circuit breaker + // however if inherit error handler is enabled, we need to wrap an error handler on the parent if (inheritErrorHandler != null && inheritErrorHandler && child == null) { - // only wrap the parent (not the children of the hystrix) + // only wrap the parent (not the children of the circuit breaker) wrap = true; } else { - log.trace("{} is part of HystrixCircuitBreaker so no error handler is applied", definition); + log.trace("{} is part of CircuitBreaker so no error handler is applied", definition); } } else if (definition instanceof MulticastDefinition) { // do not use error handler for multicast as it offers fine grained