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