All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.camel.processor.loadbalancer.WeightedLoadBalancer Maven / Gradle / Ivy

There is a newer version: 4.6.0
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.camel.processor.loadbalancer;

import java.util.ArrayList;
import java.util.List;

public abstract class WeightedLoadBalancer extends QueueLoadBalancer {
    transient int lastIndex;

    private List distributionRatioList = new ArrayList();
    private List runtimeRatios = new ArrayList();

    
    public WeightedLoadBalancer(List distributionRatios) {
        deepCloneDistributionRatios(distributionRatios);
        loadRuntimeRatios(distributionRatios);
    }
    
    protected void deepCloneDistributionRatios(List distributionRatios) {
        for (Integer value : distributionRatios) {
            this.distributionRatioList.add(value);
        }
    }

    public int getLastChosenProcessorIndex() {
        return lastIndex;
    }

    @Override
    protected void doStart() throws Exception {
        super.doStart();
        if (getProcessors().size() != getDistributionRatioList().size()) {
            throw new IllegalArgumentException("Loadbalacing with " + getProcessors().size()
                + " should match number of distributions " + getDistributionRatioList().size());
        }
    }

    protected void loadRuntimeRatios(List distributionRatios) {
        int position = 0;
        
        for (Integer value : distributionRatios) {
            runtimeRatios.add(new DistributionRatio(position++, value.intValue()));
        }
    }
    
    protected boolean isRuntimeRatiosZeroed() {
        boolean cleared = true;
        
        for (DistributionRatio runtimeRatio : runtimeRatios) {
            if (runtimeRatio.getRuntimeWeight() > 0) {
                cleared = false;
            }
        }        
        return cleared; 
    }
    
    protected void resetRuntimeRatios() {
        for (DistributionRatio runtimeRatio : runtimeRatios) {
            runtimeRatio.setRuntimeWeight(runtimeRatio.getDistributionWeight());
        }
    }

    public List getDistributionRatioList() {
        return distributionRatioList;
    }

    public void setDistributionRatioList(List distributionRatioList) {
        this.distributionRatioList = distributionRatioList;
    }

    public List getRuntimeRatios() {
        return runtimeRatios;
    }

    public void setRuntimeRatios(ArrayList runtimeRatios) {
        this.runtimeRatios = runtimeRatios;
    }    
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy