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