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