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