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