001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.model.loadbalancer;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import javax.xml.bind.annotation.XmlAccessType;
023import javax.xml.bind.annotation.XmlAccessorType;
024import javax.xml.bind.annotation.XmlAttribute;
025import javax.xml.bind.annotation.XmlRootElement;
026
027import org.apache.camel.model.LoadBalancerDefinition;
028import org.apache.camel.processor.loadbalancer.LoadBalancer;
029import org.apache.camel.processor.loadbalancer.WeightedLoadBalancer;
030import org.apache.camel.processor.loadbalancer.WeightedRandomLoadBalancer;
031import org.apache.camel.processor.loadbalancer.WeightedRoundRobinLoadBalancer;
032import org.apache.camel.spi.Metadata;
033import org.apache.camel.spi.RouteContext;
034import org.apache.camel.util.ObjectHelper;
035
036/**
037 * Weighted load balancer
038 *
039 * The weighted load balancing policy allows you to specify a processing load distribution ratio for each server
040 * with respect to others. In addition to the weight, endpoint selection is then further refined using
041 * random distribution based on weight.
042 */
043@Metadata(label = "eip,routing,loadbalance")
044@XmlRootElement(name = "weighted")
045@XmlAccessorType(XmlAccessType.FIELD)
046public class WeightedLoadBalancerDefinition extends LoadBalancerDefinition {
047    @XmlAttribute
048    private Boolean roundRobin;
049    @XmlAttribute(required = true)
050    private String distributionRatio;
051    @XmlAttribute @Metadata(defaultValue = ",")
052    private String distributionRatioDelimiter;
053
054    public WeightedLoadBalancerDefinition() {
055    }
056
057    @Override
058    protected LoadBalancer createLoadBalancer(RouteContext routeContext) {
059        WeightedLoadBalancer loadBalancer;
060        List<Integer> distributionRatioList = new ArrayList<>();
061        
062        try {
063            String[] ratios = distributionRatio.split(getDistributionRatioDelimiter());
064            for (String ratio : ratios) {
065                distributionRatioList.add(new Integer(ratio.trim()));
066            }
067
068            boolean isRoundRobin = getRoundRobin() != null && getRoundRobin();
069            if (isRoundRobin) {
070                loadBalancer = new WeightedRoundRobinLoadBalancer(distributionRatioList);
071            } else {
072                loadBalancer = new WeightedRandomLoadBalancer(distributionRatioList);
073            }
074        } catch (Exception e) {
075            throw ObjectHelper.wrapRuntimeCamelException(e);
076        }
077
078        return loadBalancer;
079    }
080
081    public Boolean getRoundRobin() {
082        return roundRobin;
083    }
084
085    /**
086     * To enable round robin mode. By default the weighted distribution mode is used.
087     * <p/>
088     * The default value is false.
089     */
090    public void setRoundRobin(Boolean roundRobin) {
091        this.roundRobin = roundRobin;
092    }
093
094    public String getDistributionRatio() {
095        return distributionRatio;
096    }
097
098    /**
099     * The distribution ratio is a delimited String consisting on integer weights separated by delimiters for example "2,3,5".
100     * The distributionRatio must match the number of endpoints and/or processors specified in the load balancer list.
101     */
102    public void setDistributionRatio(String distributionRatio) {
103        this.distributionRatio = distributionRatio;
104    }
105
106    public String getDistributionRatioDelimiter() {
107        return distributionRatioDelimiter == null ? "," : distributionRatioDelimiter;
108    }
109
110    /**
111     * Delimiter used to specify the distribution ratio.
112     * <p/>
113     * The default value is ,
114     */
115    public void setDistributionRatioDelimiter(String distributionRatioDelimiter) {
116        this.distributionRatioDelimiter = distributionRatioDelimiter;
117    }
118
119    @Override
120    public String toString() {
121        boolean isRoundRobin = getRoundRobin() != null && getRoundRobin();
122        if (isRoundRobin) {
123            return "WeightedRoundRobinLoadBalancer[" + distributionRatio + "]";
124        } else {
125            return "WeightedRandomLoadBalancer[" + distributionRatio + "]";
126        }
127    }
128}