⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 roundrobin.java

📁 java开源的企业总线.xmlBlaster
💻 JAVA
字号:
/*------------------------------------------------------------------------------Name:      RoundRobin.javaProject:   xmlBlaster.orgCopyright: xmlBlaster.org, see xmlBlaster-LICENSE fileComment:   Simple demo implementation for clusteringAuthor:    xmlBlaster@marcelruff.info------------------------------------------------------------------------------*/package org.xmlBlaster.engine.cluster.simpledomain;import java.util.logging.Logger;import java.util.logging.Level;import org.xmlBlaster.util.plugin.I_Plugin;import org.xmlBlaster.util.XmlBlasterException;import org.xmlBlaster.engine.ServerScope;import org.xmlBlaster.engine.cluster.I_LoadBalancer;import org.xmlBlaster.engine.cluster.NodeDomainInfo;import org.xmlBlaster.engine.cluster.ClusterManager;import java.util.Set;import java.util.Iterator;/** * Implements dummy load balancing for xmlBlaster using round robin approach.  * @author xmlBlaster@marcelruff.info  * @since 0.79e */final public class RoundRobin implements I_LoadBalancer, I_Plugin {   private String ME = "RoundRobin";   private ServerScope glob = null;   private static Logger log = Logger.getLogger(RoundRobin.class.getName());   private int counter = 0;   /**    * This is called after instantiation of the plugin     * @param glob The Global handle of this xmlBlaster server instance.    */   public void initialize(ServerScope glob, ClusterManager clusterManager) {      this.glob = glob;      this.ME = this.ME + "-" + glob.getId();      log.info("Round robin load balancer is initialized");   }   /**    * This method is called by the PluginManager (enforced by I_Plugin).     * @see org.xmlBlaster.util.plugin.I_Plugin#init(org.xmlBlaster.util.Global,org.xmlBlaster.util.plugin.PluginInfo)    */   public void init(org.xmlBlaster.util.Global glob, org.xmlBlaster.util.plugin.PluginInfo pluginInfo) {   }   /**    * Return plugin type for Plugin loader    * @return "RoundRobin"    */   public String getType() {      return "RoundRobin";   }   /**    * Return plugin version for Plugin loader    * @return "1.0"    */   public String getVersion() {      return "1.0";   }   /**    * Get a human readable name of this filter implementation    * @return "RoundRobin"    */   public String getName() {      return "RoundRobin";   }   /**    * We determine which xmlBlaster node to choose with a simple counter.     * <p />    * @param nodeDomainInfoSet A set containing NodeDomainInfo objects, the possible xmlBlaster nodes.    *                       Is never null, but may have a size of 0.    * @return The chosen nodeDomainInfo to handle the message or null to handle it locally    * @see org.xmlBlaster.engine.cluster.I_LoadBalancer#getClusterNode(java.util.Set)    */   public synchronized NodeDomainInfo getClusterNode(Set nodeDomainInfoSet) throws XmlBlasterException {      // TODO: Change return to NodeDomainInfo[] if multiple fail over nodes exist !!!            if (nodeDomainInfoSet.size() == 0) {         log.warning("Empty nodeDomainInfoSet, using local node");         return null; // clusterManager.getMyClusterNode(); // handle locally      }      if (counter >= nodeDomainInfoSet.size()) // counter is our RoundRobin approach         counter = 0;      /*       The Set is sorted after       "<available:stratum:nodeId>"       available := 0 connected, 1 polling, 2 unavailable       stratum   := 0 master, 1 stratum, 2 stratum ...       So we may choose a slave routing to a master,       the chosen stratum must be smaller (closer to the master) than our current stratum              There must be the possibility to use a slave instead of the master       directly (choosing the stratum which is exactly one smaller?)      */      // Step 1: Find out my stratum for this message (if i have one)      // Check all rules to find my lowest stratum      int myStratum = Integer.MAX_VALUE;      Iterator it = nodeDomainInfoSet.iterator();      while (it.hasNext()) {         NodeDomainInfo nodeDomainInfo = (NodeDomainInfo)it.next();         if (nodeDomainInfo.getClusterNode().isLocalNode()) {            if (nodeDomainInfo.getStratum() < myStratum) {               myStratum = nodeDomainInfo.getStratum();               break;            }         }      }      // Step 2: Take the node with the lowest stratum or myself      // Aaheem, this is no round robin ... :-)      // We know that the Set is sorted after available:stratum:nodeId      it = nodeDomainInfoSet.iterator();      while (it.hasNext()) {         NodeDomainInfo nodeDomainInfo = (NodeDomainInfo)it.next();         if (myStratum <= nodeDomainInfo.getStratum()) {            // handle locally, no need to send to a worse or equal stratum            if (nodeDomainInfo.getStratum() > 0) {               log.warning("Selected myself as master node from a choice of " + nodeDomainInfoSet.size()                    + " nodes, but we are only stratum=" + nodeDomainInfo.getStratum() + ". The message is not routed further!");            }            else {               if (log.isLoggable(Level.FINE)) log.fine("Selected myself as master node from a choice of " + nodeDomainInfoSet.size() + " nodes");            }            return null; // handle locally: clusterManager.getMyClusterNode();         }         if (log.isLoggable(Level.FINE))            log.fine("Selected master node id='" +                      nodeDomainInfo.getClusterNode().getId() + "' from a choice of " +                      nodeDomainInfoSet.size() + " nodes.  alive = " +                      nodeDomainInfo.getClusterNode().isAlive() + ", polling = " +                     nodeDomainInfo.getClusterNode().isPolling());         return nodeDomainInfo;      }      /*      // Step 2: Filter the possible nodes (round robin)      it = nodeDomainInfoSet.iterator();      int ii=0;      while (it.hasNext()) {         Object obj = it.next();         if (ii == counter) {            NodeDomainInfo nodeDomainInfo = (NodeDomainInfo)obj;            ClusterNode clusterNode = nodeDomainInfo.getClusterNode();            if (log.isLoggable(Level.FINE)) log.trace(ME, "Selected master node id='" + clusterNode.getId() + "' from a choice of " + nodeDomainInfoSet.size() + " nodes");            counter++;            return clusterNode;         }         ii++;      }      */      log.warning("Can't find master, using local node");      return null; // handle locally: clusterManager.getMyClusterNode();   }   public void shutdown() throws XmlBlasterException {   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -