Home » synapse-1.2-src » org.apache.synapse.endpoints.algorithms » [javadoc | source]

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with 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,
   13    *  software distributed under the License is distributed on an
   14    *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    *  KIND, either express or implied.  See the License for the
   16    *  specific language governing permissions and limitations
   17    *  under the License.
   18    */
   19   
   20   package org.apache.synapse.endpoints.algorithms;
   21   
   22   import org.apache.commons.logging.Log;
   23   import org.apache.commons.logging.LogFactory;
   24   import org.apache.synapse.MessageContext;
   25   import org.apache.synapse.endpoints.Endpoint;
   26   
   27   import java.util.ArrayList;
   28   
   29   /**
   30    * This is the implementation of the round robin load balancing algorithm. It simply iterates
   31    * through the endpoint list one by one for until an active endpoint is found.
   32    */
   33   public class RoundRobin implements LoadbalanceAlgorithm {
   34   
   35       private static final Log log = LogFactory.getLog(RoundRobin.class);
   36   
   37       /**
   38        * Endpoints list for the round robin algorithm
   39        */
   40       private ArrayList endpoints = null;
   41   
   42       public RoundRobin(ArrayList endpoints) {
   43           this.endpoints = endpoints;
   44       }
   45   
   46       /**
   47        * Choose an active endpoint using the round robin algorithm. If there are no active endpoints
   48        * available, returns null.
   49        *
   50        * @param synapseMessageContext MessageContext instance which holds all per-message properties
   51        * @param  algorithmContext The context in which holds run time states related to the algorithm
   52        * @return endpoint to send the next message
   53        */
   54       public Endpoint getNextEndpoint(MessageContext synapseMessageContext,
   55           AlgorithmContext algorithmContext) {
   56   
   57           if (log.isDebugEnabled()) {
   58               log.debug("Using the Round Robin loadbalancing algorithm to select the next endpoint");
   59           }
   60   
   61           Endpoint nextEndpoint;
   62           int attempts = 0;
   63           int currentEPR = algorithmContext.getCurrentEndpointIndex();
   64           do {
   65               // two successive clients could get the same endpoint if not synchronized.
   66               synchronized (this) {
   67                   nextEndpoint = (Endpoint) endpoints.get(currentEPR);
   68   
   69                   if (currentEPR == endpoints.size() - 1) {
   70                       currentEPR = 0;
   71                   } else {
   72                       currentEPR++;
   73                   }
   74                   algorithmContext.setCurrentEPR(currentEPR);
   75               }
   76   
   77               attempts++;
   78               if (attempts > endpoints.size()) {
   79                   log.warn("Couldn't find an endpoint from the Round Robin loadbalancing algorithm");
   80                   return null;
   81               }
   82   
   83           } while (!nextEndpoint.isActive(synapseMessageContext));
   84   
   85           return nextEndpoint;
   86       }
   87   
   88       public void reset(AlgorithmContext algorithmContext) {
   89           if (log.isDebugEnabled()) {
   90               log.debug("Resetting the Round Robin loadbalancing algorithm ...");
   91           }
   92           algorithmContext.setCurrentEPR(0);
   93       }
   94   }

Home » synapse-1.2-src » org.apache.synapse.endpoints.algorithms » [javadoc | source]