001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.processor.loadbalancer;
019    
020    import org.apache.camel.Exchange;
021    import org.apache.camel.Expression;
022    import org.apache.camel.Processor;
023    
024    import java.util.HashMap;
025    import java.util.List;
026    import java.util.Map;
027    import java.util.Set;
028    import java.util.Iterator;
029    
030    /**
031     * Implements a sticky load balancer using an {@link Expression} to calculate
032     * a correlation key to perform the sticky load balancing; rather like jsessionid in the web
033     * or JMSXGroupID in JMS.
034     *
035     * @version $Revision: 1.1 $
036     */
037    public class StickyLoadBalancer extends QueueLoadBalancer {
038        private Expression<Exchange> correlationExpression;
039        private QueueLoadBalancer loadBalancer;
040        private int numberOfHashGroups = 64 * 1024;
041        private Map<Object, Processor> stickyMap = new HashMap<Object, Processor>();
042    
043        public StickyLoadBalancer(Expression<Exchange> correlationExpression) {
044            this(correlationExpression, new RoundRobinLoadBalancer());
045        }
046    
047        public StickyLoadBalancer(Expression<Exchange> correlationExpression, QueueLoadBalancer loadBalancer) {
048            this.correlationExpression = correlationExpression;
049            this.loadBalancer = loadBalancer;
050        }
051    
052        protected synchronized Processor chooseProcessor(List<Processor> processors, Exchange exchange) {
053            Object value = correlationExpression.evaluate(exchange);
054            Object key = getStickyKey(value);
055    
056            Processor processor;
057            synchronized (stickyMap) {
058                processor = stickyMap.get(key);
059                if (processor == null) {
060                    processor = loadBalancer.chooseProcessor(processors, exchange);
061                    stickyMap.put(key, processor);
062                }
063            }
064            return processor;
065        }
066    
067        @Override
068        public void removeProcessor(Processor processor) {
069            synchronized (stickyMap) {
070                Iterator<Map.Entry<Object,Processor>> iter = stickyMap.entrySet().iterator();
071                while (iter.hasNext()) {
072                    Map.Entry<Object, Processor> entry = iter.next();
073                    if (processor.equals(entry.getValue())) {
074                        iter.remove();
075                    }
076                }
077            }
078            super.removeProcessor(processor);
079        }
080    
081    
082        // Properties
083        //-------------------------------------------------------------------------
084        public int getNumberOfHashGroups() {
085            return numberOfHashGroups;
086        }
087    
088        public void setNumberOfHashGroups(int numberOfHashGroups) {
089            this.numberOfHashGroups = numberOfHashGroups;
090        }
091    
092        // Implementation methods
093        //-------------------------------------------------------------------------
094    
095        /**
096         * A strategy to create the key for the sticky load balancing map.
097         * The default implementation uses the hash code of the value
098         * then modulos by the numberOfHashGroups to avoid the sticky map getting too big
099         *
100         * @param value the correlation value
101         * @return the key to be used in the sticky map
102         */
103        protected Object getStickyKey(Object value) {
104            int hashCode = 37;
105            if (value != null) {
106                hashCode = value.hashCode();
107            }
108            if (numberOfHashGroups > 0) {
109                hashCode = hashCode % numberOfHashGroups;
110            }
111            return hashCode;
112        }
113    }