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 javax.xml.bind.annotation.XmlAccessType;
020import javax.xml.bind.annotation.XmlAccessorType;
021import javax.xml.bind.annotation.XmlAttribute;
022import javax.xml.bind.annotation.XmlRootElement;
023
024import org.apache.camel.model.LoadBalancerDefinition;
025import org.apache.camel.spi.Metadata;
026
027/**
028 * Weighted load balancer
029 *
030 * The weighted load balancing policy allows you to specify a processing load distribution ratio for each server
031 * with respect to others. In addition to the weight, endpoint selection is then further refined using
032 * random distribution based on weight.
033 */
034@Metadata(label = "eip,routing,loadbalance")
035@XmlRootElement(name = "weighted")
036@XmlAccessorType(XmlAccessType.FIELD)
037public class WeightedLoadBalancerDefinition extends LoadBalancerDefinition {
038    @XmlAttribute
039    private Boolean roundRobin;
040    @XmlAttribute(required = true)
041    private String distributionRatio;
042    @XmlAttribute @Metadata(defaultValue = ",")
043    private String distributionRatioDelimiter;
044
045    public WeightedLoadBalancerDefinition() {
046    }
047
048    public Boolean getRoundRobin() {
049        return roundRobin;
050    }
051
052    /**
053     * To enable round robin mode. By default the weighted distribution mode is used.
054     * <p/>
055     * The default value is false.
056     */
057    public void setRoundRobin(Boolean roundRobin) {
058        this.roundRobin = roundRobin;
059    }
060
061    public String getDistributionRatio() {
062        return distributionRatio;
063    }
064
065    /**
066     * The distribution ratio is a delimited String consisting on integer weights separated by delimiters for example "2,3,5".
067     * The distributionRatio must match the number of endpoints and/or processors specified in the load balancer list.
068     */
069    public void setDistributionRatio(String distributionRatio) {
070        this.distributionRatio = distributionRatio;
071    }
072
073    public String getDistributionRatioDelimiter() {
074        return distributionRatioDelimiter == null ? "," : distributionRatioDelimiter;
075    }
076
077    /**
078     * Delimiter used to specify the distribution ratio.
079     * <p/>
080     * The default value is ,
081     */
082    public void setDistributionRatioDelimiter(String distributionRatioDelimiter) {
083        this.distributionRatioDelimiter = distributionRatioDelimiter;
084    }
085
086    @Override
087    public String toString() {
088        boolean isRoundRobin = getRoundRobin() != null && getRoundRobin();
089        if (isRoundRobin) {
090            return "WeightedRoundRobinLoadBalancer[" + distributionRatio + "]";
091        } else {
092            return "WeightedRandomLoadBalancer[" + distributionRatio + "]";
093        }
094    }
095}