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.HashMap;
020import java.util.Iterator;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.camel.Exchange;
025import org.apache.camel.Expression;
026import org.apache.camel.Processor;
027
028/**
029 * Implements a sticky load balancer using an {@link Expression} to calculate
030 * a correlation key to perform the sticky load balancing; rather like jsessionid in the web
031 * or JMSXGroupID in JMS.
032 *
033 * @version 
034 */
035public class StickyLoadBalancer extends QueueLoadBalancer {
036    private Expression correlationExpression;
037    private QueueLoadBalancer loadBalancer;
038    private int numberOfHashGroups = 64 * 1024;
039    private final Map<Object, Processor> stickyMap = new HashMap<Object, Processor>();
040
041    public StickyLoadBalancer(Expression correlationExpression) {
042        this(correlationExpression, new RoundRobinLoadBalancer());
043    }
044
045    public StickyLoadBalancer(Expression correlationExpression, QueueLoadBalancer loadBalancer) {
046        this.correlationExpression = correlationExpression;
047        this.loadBalancer = loadBalancer;
048    }
049    
050    public Expression getCorrelationExpression() {
051        return correlationExpression;
052    }
053
054    protected synchronized Processor chooseProcessor(List<Processor> processors, Exchange exchange) {
055        Object value = correlationExpression.evaluate(exchange, Object.class);
056        Object key = getStickyKey(value);
057
058        Processor processor;
059        synchronized (stickyMap) {
060            processor = stickyMap.get(key);
061            if (processor == null) {
062                processor = loadBalancer.chooseProcessor(processors, exchange);
063                stickyMap.put(key, processor);
064            }
065        }
066        return processor;
067    }
068
069    @Override
070    public void removeProcessor(Processor processor) {
071        synchronized (stickyMap) {
072            Iterator<Map.Entry<Object, Processor>> iter = stickyMap.entrySet().iterator();
073            while (iter.hasNext()) {
074                Map.Entry<Object, Processor> entry = iter.next();
075                if (processor.equals(entry.getValue())) {
076                    iter.remove();
077                }
078            }
079        }
080        super.removeProcessor(processor);
081    }
082
083
084    // Properties
085    //-------------------------------------------------------------------------
086    public int getNumberOfHashGroups() {
087        return numberOfHashGroups;
088    }
089
090    public void setNumberOfHashGroups(int numberOfHashGroups) {
091        this.numberOfHashGroups = numberOfHashGroups;
092    }
093
094    // Implementation methods
095    //-------------------------------------------------------------------------
096
097    /**
098     * A strategy to create the key for the sticky load balancing map.
099     * The default implementation uses the hash code of the value
100     * then modulos by the numberOfHashGroups to avoid the sticky map getting too big
101     *
102     * @param value the correlation value
103     * @return the key to be used in the sticky map
104     */
105    protected Object getStickyKey(Object value) {
106        int hashCode = 37;
107        if (value != null) {
108            hashCode = value.hashCode();
109        }
110        if (numberOfHashGroups > 0) {
111            hashCode = hashCode % numberOfHashGroups;
112        }
113        return hashCode;
114    }
115
116    public String toString() {
117        return "StickyLoadBalancer";
118    }
119
120}