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.processor.loadbalancer; 018 019import java.util.ArrayList; 020import java.util.List; 021 022public abstract class WeightedLoadBalancer extends QueueLoadBalancer { 023 transient int lastIndex; 024 025 private List<Integer> distributionRatioList = new ArrayList<>(); 026 private List<DistributionRatio> runtimeRatios = new ArrayList<>(); 027 028 029 public WeightedLoadBalancer(List<Integer> distributionRatios) { 030 deepCloneDistributionRatios(distributionRatios); 031 loadRuntimeRatios(distributionRatios); 032 } 033 034 protected void deepCloneDistributionRatios(List<Integer> distributionRatios) { 035 for (Integer value : distributionRatios) { 036 this.distributionRatioList.add(value); 037 } 038 } 039 040 public int getLastChosenProcessorIndex() { 041 return lastIndex; 042 } 043 044 @Override 045 protected void doStart() throws Exception { 046 super.doStart(); 047 if (getProcessors().size() != getDistributionRatioList().size()) { 048 throw new IllegalArgumentException("Loadbalacing with " + getProcessors().size() 049 + " should match number of distributions " + getDistributionRatioList().size()); 050 } 051 } 052 053 protected void loadRuntimeRatios(List<Integer> distributionRatios) { 054 int position = 0; 055 056 for (Integer value : distributionRatios) { 057 runtimeRatios.add(new DistributionRatio(position++, value.intValue())); 058 } 059 } 060 061 protected boolean isRuntimeRatiosZeroed() { 062 boolean cleared = true; 063 064 for (DistributionRatio runtimeRatio : runtimeRatios) { 065 if (runtimeRatio.getRuntimeWeight() > 0) { 066 cleared = false; 067 } 068 } 069 return cleared; 070 } 071 072 protected void resetRuntimeRatios() { 073 for (DistributionRatio runtimeRatio : runtimeRatios) { 074 runtimeRatio.setRuntimeWeight(runtimeRatio.getDistributionWeight()); 075 } 076 } 077 078 public List<Integer> getDistributionRatioList() { 079 return distributionRatioList; 080 } 081 082 public void setDistributionRatioList(List<Integer> distributionRatioList) { 083 this.distributionRatioList = distributionRatioList; 084 } 085 086 public List<DistributionRatio> getRuntimeRatios() { 087 return runtimeRatios; 088 } 089 090 public void setRuntimeRatios(ArrayList<DistributionRatio> runtimeRatios) { 091 this.runtimeRatios = runtimeRatios; 092 } 093 094}