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