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:
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 *not* elect its own
35: * prefered target: the target *is* shared with all proxies that belong to the same family
36: * (for a different behaviour please take a look at FirstAvailable)
37: *
38: * @see org.jboss.ha.framework.interfaces.LoadBalancePolicy
39: *
40: * @author <a href="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
41: * @version $Revision: 57188 $
42: */
43: public class FirstAvailableIdenticalAllProxies implements
44: LoadBalancePolicy {
45: // Constants -----------------------------------------------------
46: private static final long serialVersionUID = 2910756623413400467L;
47:
48: // Attributes ----------------------------------------------------
49:
50: // Static --------------------------------------------------------
51:
52: // Constructors --------------------------------------------------
53:
54: // Public --------------------------------------------------------
55:
56: public void init(HARMIClient father) {
57: // do not use the HARMIClient in this policy
58: }
59:
60: public Object chooseTarget(FamilyClusterInfo clusterFamily) {
61: return chooseTarget(clusterFamily, null);
62: }
63:
64: public Object chooseTarget(FamilyClusterInfo clusterFamily,
65: Invocation routingDecision) {
66: Object target = clusterFamily.getObject();
67: ArrayList targets = clusterFamily.getTargets();
68:
69: if (targets.size() == 0)
70: return null;
71:
72: if (target != null && targets.contains(target)) {
73: return target;
74: } else {
75: int cursor = RandomRobin.localRandomizer.nextInt(targets
76: .size());
77: target = targets.get(cursor);
78: clusterFamily.setObject(target);
79: return target;
80: }
81: }
82:
83: }
|