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