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