CAMEL-9683: Started on camel-ribbon

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

Branch: refs/heads/kube-lb
Commit: 1a8aef640d1b3f50bf7471ddda7c5ca1dd4d590e
Parents: 6abe42e
Author: Claus Ibsen <davscl...@apache.org>
Authored: Mon May 16 10:46:26 2016 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Mon May 16 10:56:43 2016 +0200

----------------------------------------------------------------------
 .../apache/camel/model/ProcessorDefinition.java | 15 +++++
 .../camel/model/ServiceCallDefinition.java      | 14 +++++
 .../ribbon/processor/RibbonLoadBalancer.java    | 62 --------------------
 .../processor/RibbonProcessorFactory.java       | 39 +++++-------
 .../processor/RibbonServiceCallProcessor.java   | 20 +++----
 .../RibbonServiceCallKubernetesRouteTest.java   |  2 -
 .../processor/RibbonServiceCallRouteTest.java   |  1 -
 .../src/test/resources/log4j.properties         |  2 +-
 8 files changed, 52 insertions(+), 103 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/1a8aef64/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java 
b/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
index 4849b9a..75526c3 100644
--- a/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
@@ -657,7 +657,22 @@ public abstract class ProcessorDefinition<Type extends 
ProcessorDefinition<Type>
      * Calls the service
      *
      * @param name the service name
+     * @return the builder
+     */
+    @SuppressWarnings("unchecked")
+    public ServiceCallDefinition serviceCall(String name) {
+        ServiceCallDefinition answer = new ServiceCallDefinition();
+        answer.setName(name);
+        addOutput(answer);
+        return answer;
+    }
+
+    /**
+     * Calls the service
+     *
+     * @param name the service name
      * @param uri  the endpoint uri to use for calling the service
+     * @param configuration the configuration to use
      * @return the builder
      */
     @SuppressWarnings("unchecked")

http://git-wip-us.apache.org/repos/asf/camel/blob/1a8aef64/camel-core/src/main/java/org/apache/camel/model/ServiceCallDefinition.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/model/ServiceCallDefinition.java 
b/camel-core/src/main/java/org/apache/camel/model/ServiceCallDefinition.java
index c276659..f381fa0 100644
--- a/camel-core/src/main/java/org/apache/camel/model/ServiceCallDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/ServiceCallDefinition.java
@@ -51,6 +51,7 @@ public class ServiceCallDefinition extends 
NoOutputDefinition<ServiceCallDefinit
     private String serviceCallConfigurationRef;
     @XmlAttribute
     private String loadBalancerRef;
+    // TODO: allow to use custom type as load balancer
     @XmlTransient
     private ServiceCallLoadBalancer loadBalancer;
     @XmlAttribute
@@ -108,6 +109,14 @@ public class ServiceCallDefinition extends 
NoOutputDefinition<ServiceCallDefinit
     }
 
     /**
+     * Sets the uri of the service to use
+     */
+    public ServiceCallDefinition uri(String uri) {
+        setUri(uri);
+        return this;
+    }
+
+    /**
      * Sets the discovery provided to use.
      * <p/>
      * Use kubernetes to use kubernetes.
@@ -118,6 +127,11 @@ public class ServiceCallDefinition extends 
NoOutputDefinition<ServiceCallDefinit
         return this;
     }
 
+    /**
+     * Configures the Service Call EIP
+     * <p/>
+     * Use <tt>end</tt> when configuration is complete, to return back to the 
Service Call EIP.
+     */
     public ServiceCallConfigurationDefinition serviceCallConfiguration() {
         serviceCallConfiguration = new 
ServiceCallConfigurationDefinition(this);
         return serviceCallConfiguration;

http://git-wip-us.apache.org/repos/asf/camel/blob/1a8aef64/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/processor/RibbonLoadBalancer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/processor/RibbonLoadBalancer.java
 
b/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/processor/RibbonLoadBalancer.java
deleted file mode 100644
index 54da6b4..0000000
--- 
a/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/processor/RibbonLoadBalancer.java
+++ /dev/null
@@ -1,62 +0,0 @@
-/**
- * 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.ribbon.processor;
-
-import java.util.Collection;
-
-import com.netflix.loadbalancer.ILoadBalancer;
-import com.netflix.loadbalancer.IRule;
-import com.netflix.loadbalancer.Server;
-import org.apache.camel.spi.ServiceCallLoadBalancer;
-
-/**
- * Ribbon {@link IRule} based load balancer.
- */
-@Deprecated
-public class RibbonLoadBalancer implements 
ServiceCallLoadBalancer<RibbonServer>, IRule {
-
-    private final IRule rule;
-
-    public RibbonLoadBalancer(IRule rule) {
-        this.rule = rule;
-    }
-
-    @Override
-    public RibbonServer chooseServer(Collection<RibbonServer> servers) {
-        Server server = choose(null);
-        if (server != null) {
-            return new RibbonServer(server.getHost(), server.getPort());
-        } else {
-            return null;
-        }
-    }
-
-    @Override
-    public Server choose(Object key) {
-        return rule.choose(key);
-    }
-
-    @Override
-    public void setLoadBalancer(ILoadBalancer lb) {
-        rule.setLoadBalancer(lb);
-    }
-
-    @Override
-    public ILoadBalancer getLoadBalancer() {
-        return rule.getLoadBalancer();
-    }
-}

http://git-wip-us.apache.org/repos/asf/camel/blob/1a8aef64/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/processor/RibbonProcessorFactory.java
----------------------------------------------------------------------
diff --git 
a/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/processor/RibbonProcessorFactory.java
 
b/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/processor/RibbonProcessorFactory.java
index 2779734..15ebef5 100644
--- 
a/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/processor/RibbonProcessorFactory.java
+++ 
b/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/processor/RibbonProcessorFactory.java
@@ -19,8 +19,7 @@ package org.apache.camel.component.ribbon.processor;
 import java.util.HashMap;
 import java.util.Map;
 
-import com.netflix.loadbalancer.RandomRule;
-import com.netflix.loadbalancer.RoundRobinRule;
+import com.netflix.loadbalancer.IRule;
 import org.apache.camel.ExchangePattern;
 import org.apache.camel.Processor;
 import org.apache.camel.component.ribbon.RibbonConfiguration;
@@ -29,7 +28,6 @@ import 
org.apache.camel.model.ServiceCallConfigurationDefinition;
 import org.apache.camel.model.ServiceCallDefinition;
 import org.apache.camel.spi.ProcessorFactory;
 import org.apache.camel.spi.RouteContext;
-import org.apache.camel.spi.ServiceCallLoadBalancer;
 import org.apache.camel.spi.ServiceCallServerListStrategy;
 import org.apache.camel.util.CamelContextHelper;
 import org.apache.camel.util.IntrospectionSupport;
@@ -85,7 +83,7 @@ public class RibbonProcessorFactory implements 
ProcessorFactory {
             }
 
             // lookup the load balancer to use (configured on EIP takes 
precedence vs configured on configuration)
-            ServiceCallLoadBalancer lb = configureLoadBalancer(routeContext, 
sc);
+            Object lb = configureLoadBalancer(routeContext, sc);
             if (lb == null && config != null) {
                 lb = configureLoadBalancer(routeContext, config);
             }
@@ -102,8 +100,13 @@ public class RibbonProcessorFactory implements 
ProcessorFactory {
                 sl = configureServerListStrategy(routeContext, configRef);
             }
 
+            // must be a ribbon load balancer
+            if (lb != null && !(lb instanceof IRule)) {
+                throw new IllegalArgumentException("Load balancer must be of 
type: " + IRule.class + " but is of type: " + lb.getClass().getName());
+            }
+
             RibbonServiceCallProcessor processor = new 
RibbonServiceCallProcessor(name, namespace, uri, mep, rc);
-            processor.setLoadBalancer(lb);
+            processor.setRule((IRule) lb);
             processor.setServerListStrategy(sl);
             return processor;
         } else {
@@ -111,39 +114,25 @@ public class RibbonProcessorFactory implements 
ProcessorFactory {
         }
     }
 
-    private ServiceCallLoadBalancer configureLoadBalancer(RouteContext 
routeContext, ServiceCallDefinition sd) {
-        ServiceCallLoadBalancer lb = null;
+    private Object configureLoadBalancer(RouteContext routeContext, 
ServiceCallDefinition sd) {
+        Object lb = null;
 
         if (sd != null) {
             lb = sd.getLoadBalancer();
             if (lb == null && sd.getLoadBalancerRef() != null) {
                 String ref = sd.getLoadBalancerRef();
-                // special for ref is referring to built-in
-                if ("random".equalsIgnoreCase(ref)) {
-                    lb = new RibbonLoadBalancer(new RandomRule());
-                } else if ("roundrobin".equalsIgnoreCase(ref)) {
-                    lb = new RibbonLoadBalancer(new RoundRobinRule());
-                } else {
-                    lb = 
CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), ref, 
ServiceCallLoadBalancer.class);
-                }
+                lb = 
CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), ref);
             }
         }
 
         return lb;
     }
 
-    private ServiceCallLoadBalancer configureLoadBalancer(RouteContext 
routeContext, ServiceCallConfigurationDefinition config) {
-        ServiceCallLoadBalancer lb = config.getLoadBalancer();
+    private Object configureLoadBalancer(RouteContext routeContext, 
ServiceCallConfigurationDefinition config) {
+        Object lb = config.getLoadBalancer();
         if (lb == null && config.getLoadBalancerRef() != null) {
             String ref = config.getLoadBalancerRef();
-            // special for ref is referring to built-in
-            if ("random".equalsIgnoreCase(ref)) {
-                lb = new RibbonLoadBalancer(new RandomRule());
-            } else if ("roundrobin".equalsIgnoreCase(ref)) {
-                lb = new RibbonLoadBalancer(new RoundRobinRule());
-            } else {
-                lb = 
CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), ref, 
ServiceCallLoadBalancer.class);
-            }
+            lb = 
CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), ref);
         }
         return lb;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/1a8aef64/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/processor/RibbonServiceCallProcessor.java
----------------------------------------------------------------------
diff --git 
a/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/processor/RibbonServiceCallProcessor.java
 
b/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/processor/RibbonServiceCallProcessor.java
index 1dbeaed..5e77c51 100644
--- 
a/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/processor/RibbonServiceCallProcessor.java
+++ 
b/components/camel-ribbon/src/main/java/org/apache/camel/component/ribbon/processor/RibbonServiceCallProcessor.java
@@ -35,7 +35,6 @@ import org.apache.camel.component.ribbon.RibbonConfiguration;
 import org.apache.camel.component.ribbon.RibbonConstants;
 import org.apache.camel.processor.SendDynamicProcessor;
 import org.apache.camel.spi.IdAware;
-import org.apache.camel.spi.ServiceCallLoadBalancer;
 import org.apache.camel.spi.ServiceCallServerListStrategy;
 import org.apache.camel.support.ServiceSupport;
 import org.apache.camel.util.AsyncProcessorHelper;
@@ -61,14 +60,11 @@ public class RibbonServiceCallProcessor extends 
ServiceSupport implements AsyncP
     private final ExchangePattern exchangePattern;
     private final RibbonConfiguration configuration;
     private ServiceCallServerListStrategy<RibbonServer> serverListStrategy;
-    private ServiceCallLoadBalancer<RibbonServer> loadBalancer;
     private ZoneAwareLoadBalancer<RibbonServer> ribbonLoadBalancer;
     private IRule rule;
     private final RibbonServiceCallExpression serviceCallExpression;
     private SendDynamicProcessor processor;
 
-    // TODO: allow to plugin custom load balancer like ribbon
-
     public RibbonServiceCallProcessor(String name, String namespace, String 
uri, ExchangePattern exchangePattern, RibbonConfiguration configuration) {
         // setup from the provided name which can contain scheme and 
context-path information as well
         String serviceName;
@@ -157,14 +153,6 @@ public class RibbonServiceCallProcessor extends 
ServiceSupport implements AsyncP
         return "kubernetes";
     }
 
-    public ServiceCallLoadBalancer<RibbonServer> getLoadBalancer() {
-        return loadBalancer;
-    }
-
-    public void setLoadBalancer(ServiceCallLoadBalancer<RibbonServer> 
loadBalancer) {
-        this.loadBalancer = loadBalancer;
-    }
-
     public ServiceCallServerListStrategy getServerListStrategy() {
         return serverListStrategy;
     }
@@ -173,6 +161,14 @@ public class RibbonServiceCallProcessor extends 
ServiceSupport implements AsyncP
         this.serverListStrategy = serverListStrategy;
     }
 
+    public IRule getRule() {
+        return rule;
+    }
+
+    public void setRule(IRule rule) {
+        this.rule = rule;
+    }
+
     @Override
     @SuppressWarnings("unchecked")
     protected void doStart() throws Exception {

http://git-wip-us.apache.org/repos/asf/camel/blob/1a8aef64/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/processor/RibbonServiceCallKubernetesRouteTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/processor/RibbonServiceCallKubernetesRouteTest.java
 
b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/processor/RibbonServiceCallKubernetesRouteTest.java
index 97fe567..db67030 100644
--- 
a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/processor/RibbonServiceCallKubernetesRouteTest.java
+++ 
b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/processor/RibbonServiceCallKubernetesRouteTest.java
@@ -45,8 +45,6 @@ public class RibbonServiceCallKubernetesRouteTest extends 
CamelTestSupport {
                 config.setUsername("admin");
                 config.setPassword("admin");
                 config.setNamespace("default");
-                // lets use the built-in round robin (random is default)
-                config.setLoadBalancerRef("roundrobin");
 
                 from("direct:start")
                         .serviceCall("cdi-camel-jetty", null, config)

http://git-wip-us.apache.org/repos/asf/camel/blob/1a8aef64/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/processor/RibbonServiceCallRouteTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/processor/RibbonServiceCallRouteTest.java
 
b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/processor/RibbonServiceCallRouteTest.java
index e58f7c6..52b631b 100644
--- 
a/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/processor/RibbonServiceCallRouteTest.java
+++ 
b/components/camel-ribbon/src/test/java/org/apache/camel/component/ribbon/processor/RibbonServiceCallRouteTest.java
@@ -55,7 +55,6 @@ public class RibbonServiceCallRouteTest extends 
CamelTestSupport {
                 // configure camel service call
                 ServiceCallConfigurationDefinition config = new 
ServiceCallConfigurationDefinition();
                 config.setServerListStrategy(list);
-//                config.setLoadBalancerRef("roundrobin");
 
                 from("direct:start")
                         .serviceCall("myService", null, config)

http://git-wip-us.apache.org/repos/asf/camel/blob/1a8aef64/components/camel-ribbon/src/test/resources/log4j.properties
----------------------------------------------------------------------
diff --git a/components/camel-ribbon/src/test/resources/log4j.properties 
b/components/camel-ribbon/src/test/resources/log4j.properties
index 98c945f..f138e1d 100644
--- a/components/camel-ribbon/src/test/resources/log4j.properties
+++ b/components/camel-ribbon/src/test/resources/log4j.properties
@@ -18,7 +18,7 @@
 #
 # The logging properties used
 #
-log4j.rootLogger=INFO, out
+log4j.rootLogger=INFO, file
 
 log4j.logger.org.apache.camel.component.kubernetes=DEBUG
 log4j.logger.org.apache.camel.component.ribbon=DEBUG

Reply via email to