01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * 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.ArrayList;
25: import org.jboss.invocation.Invocation;
26:
27: /**
28: * LoadBalancingPolicy implementation that always favor the first available target i.e.
29: * no load balancing occurs. Nevertheless, the first target is randomly selected.
30: * This does not mean that fail-over will not occur if the
31: * first member in the list dies. In this case, fail-over will occur, and a new target
32: * will become the first member and invocation will continously be invoked on the same
33: * new target until its death. Each proxy using this policy will elect its own
34: * prefered target: the target is not shared accross the proxy family (for this
35: * behaviour please take a look at FirstAvailableIdenticalAllProxies)
36: *
37: * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>.
38: * @author <a href="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
39: * @version $Revision: 57188 $
40: * @see org.jboss.ha.framework.interfaces.LoadBalancePolicy
41: *
42: * <p><b>Revisions:</b><br>
43: * <p><b>2002/08/24: Sacha Labourey</b>
44: * <ol>
45: * <li>Use the target repository</li>
46: * <li>First choice is randomly selected to distribute the initial load</li>
47: * <li>When the list of targets change, we try to keep using the same
48: previously elected target node if it still exists. Previously,
49: we were working with the position id of the target node, thus
50: if the list order changed, we were switching to another node
51: while our prefered node was still up</li>
52: * </ol>
53: */
54:
55: public class FirstAvailable implements LoadBalancePolicy {
56: // Constants -----------------------------------------------------
57: private static final long serialVersionUID = 2008524502721775114L;
58:
59: // Attributes ----------------------------------------------------
60:
61: protected transient Object electedTarget = null;
62:
63: // Static --------------------------------------------------------
64:
65: // Constructors --------------------------------------------------
66:
67: // Public --------------------------------------------------------
68:
69: public void init(HARMIClient father) {
70: // do not use the HARMIClient in this policy
71: }
72:
73: public Object chooseTarget(FamilyClusterInfo clusterFamily) {
74: return chooseTarget(clusterFamily, null);
75: }
76:
77: public Object chooseTarget(FamilyClusterInfo clusterFamily,
78: Invocation routingDecision) {
79: ArrayList targets = clusterFamily.getTargets();
80: if (targets.size() == 0)
81: return null;
82:
83: if ((this .electedTarget != null)
84: && targets.contains(this .electedTarget)) {
85: return this .electedTarget;
86: } else {
87: int cursor = RandomRobin.localRandomizer.nextInt(targets
88: .size());
89: this.electedTarget = targets.get(cursor);
90: return this.electedTarget;
91: }
92: }
93: }
|