Coverage Report - org.apache.camel.processor.loadbalancer.StickyLoadBalancer
 
Classes in this File Line Coverage Branch Coverage Complexity
StickyLoadBalancer
0% 
0% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.processor.loadbalancer;
 19  
 
 20  
 import org.apache.camel.Exchange;
 21  
 import org.apache.camel.Expression;
 22  
 import org.apache.camel.Processor;
 23  
 
 24  
 import java.util.HashMap;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 import java.util.Set;
 28  
 import java.util.Iterator;
 29  
 
 30  
 /**
 31  
  * Implements a sticky load balancer using an {@link Expression} to calculate
 32  
  * a correlation key to perform the sticky load balancing; rather like jsessionid in the web
 33  
  * or JMSXGroupID in JMS.
 34  
  *
 35  
  * @version $Revision: 1.1 $
 36  
  */
 37  
 public class StickyLoadBalancer extends QueueLoadBalancer {
 38  
     private Expression<Exchange> correlationExpression;
 39  
     private QueueLoadBalancer loadBalancer;
 40  0
     private int numberOfHashGroups = 64 * 1024;
 41  0
     private Map<Object, Processor> stickyMap = new HashMap<Object, Processor>();
 42  
 
 43  
     public StickyLoadBalancer(Expression<Exchange> correlationExpression) {
 44  0
         this(correlationExpression, new RoundRobinLoadBalancer());
 45  0
     }
 46  
 
 47  0
     public StickyLoadBalancer(Expression<Exchange> correlationExpression, QueueLoadBalancer loadBalancer) {
 48  0
         this.correlationExpression = correlationExpression;
 49  0
         this.loadBalancer = loadBalancer;
 50  0
     }
 51  
 
 52  
     protected synchronized Processor chooseProcessor(List<Processor> processors, Exchange exchange) {
 53  0
         Object value = correlationExpression.evaluate(exchange);
 54  0
         Object key = getStickyKey(value);
 55  
 
 56  
         Processor processor;
 57  0
         synchronized (stickyMap) {
 58  0
             processor = stickyMap.get(key);
 59  0
             if (processor == null) {
 60  0
                 processor = loadBalancer.chooseProcessor(processors, exchange);
 61  0
                 stickyMap.put(key, processor);
 62  
             }
 63  0
         }
 64  0
         return processor;
 65  
     }
 66  
 
 67  
     @Override
 68  
     public void removeProcessor(Processor processor) {
 69  0
         synchronized (stickyMap) {
 70  0
             Iterator<Map.Entry<Object,Processor>> iter = stickyMap.entrySet().iterator();
 71  0
             while (iter.hasNext()) {
 72  0
                 Map.Entry<Object, Processor> entry = iter.next();
 73  0
                 if (processor.equals(entry.getValue())) {
 74  0
                     iter.remove();
 75  
                 }
 76  0
             }
 77  0
         }
 78  0
         super.removeProcessor(processor);
 79  0
     }
 80  
 
 81  
 
 82  
     // Properties
 83  
     //-------------------------------------------------------------------------
 84  
     public int getNumberOfHashGroups() {
 85  0
         return numberOfHashGroups;
 86  
     }
 87  
 
 88  
     public void setNumberOfHashGroups(int numberOfHashGroups) {
 89  0
         this.numberOfHashGroups = numberOfHashGroups;
 90  0
     }
 91  
 
 92  
     // Implementation methods
 93  
     //-------------------------------------------------------------------------
 94  
 
 95  
     /**
 96  
      * A strategy to create the key for the sticky load balancing map.
 97  
      * The default implementation uses the hash code of the value
 98  
      * then modulos by the numberOfHashGroups to avoid the sticky map getting too big
 99  
      *
 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  0
         int hashCode = 37;
 105  0
         if (value != null) {
 106  0
             hashCode = value.hashCode();
 107  
         }
 108  0
         if (numberOfHashGroups > 0) {
 109  0
             hashCode = hashCode % numberOfHashGroups;
 110  
         }
 111  0
         return hashCode;
 112  
     }
 113  
 }