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/14 11:23:01 UTC

[1/2] camel git commit: Improved camel-hystrix

Repository: camel
Updated Branches:
  refs/heads/master 09f6c50fe -> 0fa360171


Improved camel-hystrix


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c3a34a7f
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c3a34a7f
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c3a34a7f

Branch: refs/heads/master
Commit: c3a34a7f55545a7a03d70bc553270dd96dea2ccd
Parents: 09f6c50
Author: Claus Ibsen <da...@apache.org>
Authored: Thu Apr 14 11:18:04 2016 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Apr 14 11:18:04 2016 +0200

----------------------------------------------------------------------
 .../component/hystrix/HystrixConfiguration.java | 14 +++++-----
 .../component/hystrix/HystrixConstants.java     |  1 +
 .../component/hystrix/HystrixProducer.java      | 28 ++++++++++++++++++--
 .../component/hystrix/HystrixComponentBase.java | 11 --------
 .../hystrix/HystrixComponentCacheTest.java      |  2 +-
 .../hystrix/HystrixComponentFallbackTest.java   |  2 +-
 .../HystrixComponentOverrideRunTest.java        |  2 +-
 .../HystrixComponentRequestContextTest.java     |  2 +-
 8 files changed, 37 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/c3a34a7f/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixConfiguration.java b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixConfiguration.java
index d28f92a..7b93019 100644
--- a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixConfiguration.java
+++ b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixConfiguration.java
@@ -16,7 +16,6 @@
  */
 package org.apache.camel.component.hystrix;
 
-import org.apache.camel.Expression;
 import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriParams;
@@ -45,10 +44,10 @@ public class HystrixConfiguration {
     private String threadPoolKey;
 
     /**
-     * Specifies the cacheKeyExpression to use
+     * Specifies the cache key to use
      */
     @UriParam(label = "producer")
-    private Expression cacheKeyExpression;
+    private String cacheKey;
 
     /**
      * Specifies the initializeRequestContext to use
@@ -127,15 +126,14 @@ public class HystrixConfiguration {
         this.fallbackEndpoint = fallbackEndpoint;
     }
 
-    public Expression getCacheKeyExpression() {
-        return cacheKeyExpression;
+    public String getCacheKey() {
+        return cacheKey;
     }
 
-    public void setCacheKeyExpression(Expression cacheKeyExpression) {
-        this.cacheKeyExpression = cacheKeyExpression;
+    public void setCacheKey(String cacheKey) {
+        this.cacheKey = cacheKey;
     }
 
-
     public Boolean getInitializeRequestContext() {
         return initializeRequestContext;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/c3a34a7f/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixConstants.java
----------------------------------------------------------------------
diff --git a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixConstants.java b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixConstants.java
index e800722..eeefe09 100644
--- a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixConstants.java
+++ b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixConstants.java
@@ -21,6 +21,7 @@ public interface HystrixConstants {
     // in message header
     String CAMEL_HYSTRIX_RUN_ENDPOINT = "CamelHystrixRunEndpoint";
     String CAMEL_HYSTRIX_FALLBACK_ENDPOINT = "CamelHystrixFallbackEndpoint";
+    String CAMEL_HYSTRIX_CACHE_KEY = "CamelHystrixCacheKey";
     String CAMEL_HYSTRIX_CLEAR_CACHE_FIRST = "CamelHystrixClearCacheFirst";
     String CAMEL_HYSTRIX_REQUEST_CONTEXT = "CamelHystrixRequestContex";
     String CAMEL_HYSTRIX_GROUP_KEY = "CamelHystrixGroupKey";

http://git-wip-us.apache.org/repos/asf/camel/blob/c3a34a7f/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProducer.java b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProducer.java
index 75f07e1..09ac7b2 100644
--- a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProducer.java
+++ b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProducer.java
@@ -29,8 +29,11 @@ import com.netflix.hystrix.strategy.HystrixPlugins;
 import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
 import org.apache.camel.impl.DefaultProducer;
 import org.apache.camel.impl.ProducerCache;
+import org.apache.camel.util.CamelContextHelper;
+import org.apache.camel.util.EndpointHelper;
 import org.apache.camel.util.ServiceHelper;
 
 /**
@@ -280,8 +283,29 @@ public class HystrixProducer extends DefaultProducer {
     }
 
     private String getCacheKey(Exchange exchange) {
-        return configuration.getCacheKeyExpression() != null
-                ? configuration.getCacheKeyExpression().evaluate(exchange, String.class) : null;
+        Object cacheKey = exchange.getIn().getHeader(HystrixConstants.CAMEL_HYSTRIX_CACHE_KEY, configuration.getCacheKey(), Object.class);
+        if (cacheKey == null) {
+            return null;
+        }
+
+        String answer;
+        Expression expression;
+        if (cacheKey instanceof Expression) {
+            // it may be an expression already
+            expression = (Expression) cacheKey;
+        } else {
+            // otherwise its a string that either refer to an expression to lookup or use the simple languagae
+            String key = cacheKey.toString();
+            if (EndpointHelper.isReferenceParameter(key)) {
+                expression = CamelContextHelper.mandatoryLookup(exchange.getContext(), key.substring(1), Expression.class);
+            } else {
+                // use simple language as default for the expression
+                expression = exchange.getContext().resolveLanguage("simple").createExpression(key);
+            }
+        }
+
+        answer = expression.evaluate(exchange, String.class);
+        return answer;
     }
 
     private synchronized void checkRequestContextPresent(Exchange exchange) {

http://git-wip-us.apache.org/repos/asf/camel/blob/c3a34a7f/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentBase.java
----------------------------------------------------------------------
diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentBase.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentBase.java
index 000414b..acd8240 100644
--- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentBase.java
+++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentBase.java
@@ -16,14 +16,10 @@
  */
 package org.apache.camel.component.hystrix;
 
-import org.apache.camel.CamelContext;
 import org.apache.camel.EndpointInject;
 import org.apache.camel.Produce;
 import org.apache.camel.ProducerTemplate;
-import org.apache.camel.builder.ExpressionBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.impl.DefaultCamelContext;
-import org.apache.camel.impl.SimpleRegistry;
 import org.apache.camel.test.junit4.CamelTestSupport;
 
 public class HystrixComponentBase extends CamelTestSupport {
@@ -37,12 +33,5 @@ public class HystrixComponentBase extends CamelTestSupport {
     @EndpointInject(uri = "mock:error")
     protected MockEndpoint errorEndpoint;
 
-    @Override
-    protected CamelContext createCamelContext() throws Exception {
-        SimpleRegistry registry = new SimpleRegistry();
-        CamelContext context = new DefaultCamelContext(registry);
-        registry.put("headerExpression", ExpressionBuilder.headerExpression("key"));
-        return context;
-    }
 }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/c3a34a7f/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentCacheTest.java
----------------------------------------------------------------------
diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentCacheTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentCacheTest.java
index 947da03..4a84c18 100644
--- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentCacheTest.java
+++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentCacheTest.java
@@ -76,7 +76,7 @@ public class HystrixComponentCacheTest extends HystrixComponentBase {
                         .to("mock:result");
 
                 from("direct:start")
-                        .to("hystrix:testKey?runEndpoint=direct:run&fallbackEndpoint=direct:fallback&cacheKeyExpression=#headerExpression&initializeRequestContext=true");
+                        .to("hystrix:testKey?runEndpoint=direct:run&fallbackEndpoint=direct:fallback&cacheKey=header.key&initializeRequestContext=true");
             }
         };
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/c3a34a7f/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentFallbackTest.java
----------------------------------------------------------------------
diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentFallbackTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentFallbackTest.java
index bff30f3..293aec9 100644
--- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentFallbackTest.java
+++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentFallbackTest.java
@@ -90,7 +90,7 @@ public class HystrixComponentFallbackTest extends HystrixComponentBase {
                         .to("mock:result");
 
                 from("direct:start")
-                        .to("hystrix:testKey?runEndpoint=direct:run&fallbackEndpoint=direct:fallback&cacheKeyExpression=#headerExpression&initializeRequestContext=true");
+                        .to("hystrix:testKey?runEndpoint=direct:run&fallbackEndpoint=direct:fallback&cacheKey=header.key&initializeRequestContext=true");
             }
         };
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/c3a34a7f/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentOverrideRunTest.java
----------------------------------------------------------------------
diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentOverrideRunTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentOverrideRunTest.java
index 2c2ebdd..263c636 100644
--- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentOverrideRunTest.java
+++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentOverrideRunTest.java
@@ -60,7 +60,7 @@ public class HystrixComponentOverrideRunTest extends HystrixComponentBase {
                         .to("mock:run2");
 
                 from("direct:start")
-                        .to("hystrix:testKey?runEndpoint=direct:run&fallbackEndpoint=direct:fallback&cacheKeyExpression=#headerExpression&initializeRequestContext=true");
+                        .to("hystrix:testKey?runEndpoint=direct:run&fallbackEndpoint=direct:fallback&cacheKey=header.key&initializeRequestContext=true");
             }
         };
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/c3a34a7f/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentRequestContextTest.java
----------------------------------------------------------------------
diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentRequestContextTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentRequestContextTest.java
index bacb8c7..acc3c1f 100644
--- a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentRequestContextTest.java
+++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixComponentRequestContextTest.java
@@ -93,7 +93,7 @@ public class HystrixComponentRequestContextTest extends HystrixComponentBase {
                         .to("mock:result");
 
                 from("direct:start")
-                        .to("hystrix:testKey?runEndpoint=direct:run&fallbackEndpoint=direct:fallback&cacheKeyExpression=#headerExpression");
+                        .to("hystrix:testKey?runEndpoint=direct:run&fallbackEndpoint=direct:fallback&cacheKey=header.key");
             }
         };
     }


[2/2] camel git commit: Improved camel-hystrix

Posted by da...@apache.org.
Improved camel-hystrix


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/0fa36017
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0fa36017
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0fa36017

Branch: refs/heads/master
Commit: 0fa360171b927995476283082bc9bef70468c368
Parents: c3a34a7
Author: Claus Ibsen <da...@apache.org>
Authored: Thu Apr 14 11:22:32 2016 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Apr 14 11:22:32 2016 +0200

----------------------------------------------------------------------
 .../camel/component/hystrix/HystrixConfiguration.java  | 13 +++++++------
 .../camel/component/hystrix/HystrixProducer.java       |  2 +-
 2 files changed, 8 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/0fa36017/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixConfiguration.java b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixConfiguration.java
index 7b93019..74b1892 100644
--- a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixConfiguration.java
+++ b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixConfiguration.java
@@ -44,7 +44,8 @@ public class HystrixConfiguration {
     private String threadPoolKey;
 
     /**
-     * Specifies the cache key to use
+     * Specifies the cache key to use.
+     * Uses the simple language as the expression. But you can refer to an existing expression using # lookup.
      */
     @UriParam(label = "producer")
     private String cacheKey;
@@ -73,7 +74,7 @@ public class HystrixConfiguration {
      * Whether to include a number of headers with metrics details of the circuit breaker utilization
      */
     @UriParam(label = "producer")
-    private boolean metrics;
+    private boolean metricsEnabled;
 
     private Integer coreSize;
     private Integer keepAliveTime;
@@ -390,11 +391,11 @@ public class HystrixConfiguration {
         this.requestLogEnabled = requestLogEnabled;
     }
 
-    public boolean isMetrics() {
-        return metrics;
+    public boolean isMetricsEnabled() {
+        return metricsEnabled;
     }
 
-    public void setMetrics(boolean metrics) {
-        this.metrics = metrics;
+    public void setMetricsEnabled(boolean metricsEnabled) {
+        this.metricsEnabled = metricsEnabled;
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/0fa36017/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProducer.java b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProducer.java
index 09ac7b2..ba88c8c 100644
--- a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProducer.java
+++ b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProducer.java
@@ -73,7 +73,7 @@ public class HystrixProducer extends DefaultProducer {
         clearCache(camelHystrixCommand.getCommandKey(), exchange);
         camelHystrixCommand.execute();
 
-        if (configuration.isMetrics()) {
+        if (configuration.isMetricsEnabled()) {
             populateWithMetrics(exchange, camelHystrixCommand);
         }
     }