KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ha > framework > interfaces > FirstAvailable


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ha.framework.interfaces;
23
24 import java.util.List JavaDoc;
25
26 import org.jboss.invocation.Invocation;
27
28 /**
29  * LoadBalancingPolicy implementation that always favor the first available target i.e.
30  * no load balancing occurs. Nevertheless, the first target is randomly selected.
31  * This does not mean that fail-over will not occur if the
32  * first member in the list dies. In this case, fail-over will occur, and a new target
33  * will become the first member and invocation will continously be invoked on the same
34  * new target until its death. Each proxy using this policy will elect its own
35  * prefered target: the target is not shared accross the proxy family (for this
36  * behaviour please take a look at FirstAvailableIdenticalAllProxies)
37  *
38  * @author <a HREF="mailto:bill@burkecentral.com">Bill Burke</a>.
39  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
40  * @version $Revision: 56192 $
41  * @see org.jboss.ha.framework.interfaces.LoadBalancePolicy
42  *
43  * <p><b>Revisions:</b><br>
44  * <p><b>2002/08/24: Sacha Labourey</b>
45  * <ol>
46  * <li>Use the target repository</li>
47  * <li>First choice is randomly selected to distribute the initial load</li>
48  * <li>When the list of targets change, we try to keep using the same
49          previously elected target node if it still exists. Previously,
50          we were working with the position id of the target node, thus
51          if the list order changed, we were switching to another node
52          while our prefered node was still up</li>
53  * </ol>
54  */

55
56 public class FirstAvailable implements LoadBalancePolicy
57 {
58    // Constants -----------------------------------------------------
59
private static final long serialVersionUID = 2008524502721775114L;
60
61    // Attributes ----------------------------------------------------
62

63    protected transient Object JavaDoc electedTarget = null;
64    
65    // Static --------------------------------------------------------
66

67    // Constructors --------------------------------------------------
68

69     // Public --------------------------------------------------------
70

71    public void init (HARMIClient father)
72    {
73       // do not use the HARMIClient in this policy
74
}
75
76    public Object JavaDoc chooseTarget (FamilyClusterInfo clusterFamily)
77    {
78       return chooseTarget(clusterFamily, null);
79    }
80
81    public Object JavaDoc chooseTarget (FamilyClusterInfo clusterFamily, Invocation routingDecision)
82    {
83       List JavaDoc targets = clusterFamily.getTargets ();
84       if (targets.size () == 0)
85          return null;
86
87       if ( (this.electedTarget != null) && targets.contains (this.electedTarget) )
88       {
89          return this.electedTarget;
90       }
91       else
92       {
93          int cursor = RandomRobin.localRandomizer.nextInt(targets.size());
94          this.electedTarget = targets.get(cursor);
95          return this.electedTarget;
96       }
97    }
98 }
99
Popular Tags