You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/04/19 18:19:21 UTC

[02/24] camel git commit: CAMEL-9879: Circuit Breaker EIP - That is using hystrix

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/4f16c26a
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/4f16c26a
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/4f16c26a

Branch: refs/heads/hys
Commit: 4f16c26a7c534da945919cc93f3ed5bf5532af3b
Parents: e325750
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Apr 19 16:07:29 2016 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Apr 19 16:07:29 2016 +0200

----------------------------------------------------------------------
 .../apache/camel/model/HystrixDefinition.java   | 58 ++++++++++++++------
 .../hystrix/processor/HystrixProcessor.java     |  7 ++-
 .../processor/HystrixProcessorCommand.java      | 20 ++++++-
 .../processor/HystrixProcessorFactory.java      | 11 +++-
 4 files changed, 73 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/4f16c26a/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 91a8367..fa91362 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
@@ -26,6 +26,7 @@ import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlElementRef;
 import javax.xml.bind.annotation.XmlRootElement;
 
+import org.apache.camel.Expression;
 import org.apache.camel.Processor;
 import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.RouteContext;
@@ -37,6 +38,8 @@ public class HystrixDefinition extends ProcessorDefinition<HystrixDefinition> {
 
     @XmlElement
     private HystrixConfigurationDefinition hystrixConfiguration;
+    @XmlElement
+    private ExpressionSubElementDefinition cacheKey;
     @XmlElementRef
     private List<ProcessorDefinition<?>> outputs = new ArrayList<ProcessorDefinition<?>>();
     @XmlElement
@@ -117,14 +120,6 @@ public class HystrixDefinition extends ProcessorDefinition<HystrixDefinition> {
     // Getter/Setter
     // -------------------------------------------------------------------------
 
-    public FallbackDefinition getFallback() {
-        return fallback;
-    }
-
-    public void setFallback(FallbackDefinition fallback) {
-        this.fallback = fallback;
-    }
-
     public HystrixConfigurationDefinition getHystrixConfiguration() {
         return hystrixConfiguration;
     }
@@ -141,18 +136,25 @@ public class HystrixDefinition extends ProcessorDefinition<HystrixDefinition> {
         this.hystrixConfigurationRef = hystrixConfigurationRef;
     }
 
-    // Fluent API
-    // -------------------------------------------------------------------------
+    public ExpressionSubElementDefinition getCacheKey() {
+        return cacheKey;
+    }
 
-    /**
-     * Sets the fallback node
-     */
-    public HystrixDefinition fallback() {
-        fallback = new FallbackDefinition();
-        fallback.setParent(this);
-        return this;
+    public void setCacheKey(ExpressionSubElementDefinition cacheKey) {
+        this.cacheKey = cacheKey;
+    }
+
+    public FallbackDefinition getFallback() {
+        return fallback;
     }
 
+    public void setFallback(FallbackDefinition fallback) {
+        this.fallback = fallback;
+    }
+
+    // Fluent API
+    // -------------------------------------------------------------------------
+
     /**
      * Configures the Hystrix EIP
      * <p/>
@@ -179,4 +181,26 @@ public class HystrixDefinition extends ProcessorDefinition<HystrixDefinition> {
         return this;
     }
 
+    /**
+     * Sets the expression to use for generating the cache key.
+     * <p/>
+     * Key to be used for request caching.
+     * By default this returns null which means "do not cache".
+     * To enable caching set an expression that returns a string key uniquely representing the state of a command instance.
+     * If multiple command instances in the same request scope match keys then only the first will be executed and all others returned from cache.
+     */
+    public HystrixDefinition cacheKey(Expression expression) {
+        setCacheKey(new ExpressionSubElementDefinition(expression));
+        return this;
+    }
+
+    /**
+     * Sets the fallback node
+     */
+    public HystrixDefinition fallback() {
+        fallback = new FallbackDefinition();
+        fallback.setParent(this);
+        return this;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/4f16c26a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessor.java
----------------------------------------------------------------------
diff --git a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessor.java b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessor.java
index bc54b82..8f836c0 100644
--- a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessor.java
+++ b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessor.java
@@ -23,6 +23,7 @@ import com.netflix.hystrix.HystrixCommand;
 import org.apache.camel.AsyncCallback;
 import org.apache.camel.AsyncProcessor;
 import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
 import org.apache.camel.Navigate;
 import org.apache.camel.Processor;
 import org.apache.camel.spi.IdAware;
@@ -39,12 +40,14 @@ public class HystrixProcessor extends ServiceSupport implements AsyncProcessor,
     private final HystrixCommand.Setter setter;
     private final AsyncProcessor processor;
     private final AsyncProcessor fallback;
+    private final Expression cacheKey;
 
-    public HystrixProcessor(String id, HystrixCommand.Setter setter, Processor processor, Processor fallback) {
+    public HystrixProcessor(String id, HystrixCommand.Setter setter, Processor processor, Processor fallback, Expression cacheKey) {
         this.id = id;
         this.setter = setter;
         this.processor = AsyncProcessorConverterHelper.convert(processor);
         this.fallback = AsyncProcessorConverterHelper.convert(fallback);
+        this.cacheKey = cacheKey;
     }
 
     @Override
@@ -87,7 +90,7 @@ public class HystrixProcessor extends ServiceSupport implements AsyncProcessor,
 
     @Override
     public boolean process(Exchange exchange, AsyncCallback callback) {
-        HystrixProcessorCommand command = new HystrixProcessorCommand(setter, exchange, callback, processor, fallback);
+        HystrixProcessorCommand command = new HystrixProcessorCommand(setter, exchange, callback, processor, fallback, cacheKey);
         try {
             command.queue();
         } catch (Throwable e) {

http://git-wip-us.apache.org/repos/asf/camel/blob/4f16c26a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessorCommand.java
----------------------------------------------------------------------
diff --git a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessorCommand.java b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessorCommand.java
index acb8605..0ba6158 100644
--- a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessorCommand.java
+++ b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessorCommand.java
@@ -17,10 +17,10 @@
 package org.apache.camel.component.hystrix.processor;
 
 import com.netflix.hystrix.HystrixCommand;
-import com.netflix.hystrix.HystrixCommandGroupKey;
 import org.apache.camel.AsyncCallback;
 import org.apache.camel.AsyncProcessor;
 import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -34,13 +34,29 @@ public class HystrixProcessorCommand extends HystrixCommand<Exchange> {
     private final AsyncCallback callback;
     private final AsyncProcessor processor;
     private final AsyncProcessor fallback;
+    private final Expression cacheKey;
 
-    public HystrixProcessorCommand(Setter setter, Exchange exchange, AsyncCallback callback, AsyncProcessor processor, AsyncProcessor fallback) {
+    public HystrixProcessorCommand(Setter setter, Exchange exchange, AsyncCallback callback, AsyncProcessor processor, AsyncProcessor fallback, Expression cacheKey) {
         super(setter);
         this.exchange = exchange;
         this.callback = callback;
         this.processor = processor;
         this.fallback = fallback;
+        this.cacheKey = cacheKey;
+    }
+
+    @Override
+    protected String getCacheKey() {
+        // TODO: require https://github.com/Netflix/Hystrix/wiki/How-To-Use#Caching
+        if (cacheKey != null) {
+            try {
+                return cacheKey.evaluate(exchange, String.class);
+            } catch (Throwable e) {
+                // ignore
+                LOG.debug("Error evaluating cache key. This exception is ignored.", e);
+            }
+        }
+        return null;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/4f16c26a/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 276275d..1389438 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
@@ -20,6 +20,7 @@ import com.netflix.hystrix.HystrixCommand;
 import com.netflix.hystrix.HystrixCommandGroupKey;
 import com.netflix.hystrix.HystrixCommandProperties;
 import com.netflix.hystrix.HystrixThreadPoolProperties;
+import org.apache.camel.Expression;
 import org.apache.camel.Processor;
 import org.apache.camel.model.HystrixConfigurationDefinition;
 import org.apache.camel.model.HystrixDefinition;
@@ -45,7 +46,7 @@ public class HystrixProcessorFactory implements ProcessorFactory {
             HystrixDefinition cb = (HystrixDefinition) definition;
             String id = cb.idOrCreate(routeContext.getCamelContext().getNodeIdFactory());
 
-            // create the regular processor
+            // create the regular and fallback processors
             Processor processor = cb.createChildProcessor(routeContext, true);
             Processor fallback = null;
             if (cb.getFallback() != null) {
@@ -70,7 +71,13 @@ public class HystrixProcessorFactory implements ProcessorFactory {
                 configureHystrix(command, threadPool, config);
             }
 
-            return new HystrixProcessor(id, setter, processor, fallback);
+            // optional cache-key from expression
+            Expression cacheKey = null;
+            if (cb.getCacheKey() != null) {
+                cacheKey = cb.getCacheKey().createExpression(routeContext);
+            }
+
+            return new HystrixProcessor(id, setter, processor, fallback, cacheKey);
         } else {
             return null;
         }