01: /**
02: * Sequoia: Database clustering technology.
03: * Copyright (C) 2005 Emic Networks.
04: * Contact: sequoia@continuent.org
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: *
18: * Initial developer(s): Emmanuel Cecchet.
19: * Contributor(s): ______________________.
20: */package org.continuent.sequoia.driver.connectpolicy;
21:
22: import org.continuent.sequoia.common.exceptions.NoMoreControllerException;
23: import org.continuent.sequoia.driver.ControllerInfo;
24: import org.continuent.sequoia.driver.SequoiaUrl;
25:
26: /**
27: * This class defines a RoundRobinConnectPolicy used when the Sequoia URL has
28: * the following form:
29: * jdbc:sequoia://node1,node2,node3/myDB?preferredController=roundRobin
30: * <p>
31: * Round robin starts with the first node in URL.
32: *
33: * @author <a href="mailto:emmanuel.cecchet@emicnetworks.com">Emmanuel Cecchet
34: * </a>
35: * @version 1.0
36: */
37: public class RoundRobinConnectPolicy extends
38: AbstractControllerConnectPolicy {
39: private int index = -1;
40:
41: /**
42: * Creates a new <code>RandomConnectPolicy</code> object
43: *
44: * @param controllerList the controller list on which the policy applies
45: * @param pingDelayInMs Interval in milliseconds between two pings of a
46: * controller
47: * @param controllerTimeoutInMs timeout in milliseconds after which a
48: * controller is considered as dead if it did not respond to pings
49: * @param debugLevel the debug level to use
50: * @see org.continuent.sequoia.driver.SequoiaUrl#DEBUG_LEVEL_OFF
51: */
52: public RoundRobinConnectPolicy(ControllerInfo[] controllerList,
53: int pingDelayInMs, int controllerTimeoutInMs, int debugLevel) {
54: super (controllerList, pingDelayInMs, controllerTimeoutInMs,
55: debugLevel);
56: }
57:
58: /**
59: * @see org.continuent.sequoia.driver.connectpolicy.AbstractControllerConnectPolicy#getController()
60: */
61: public synchronized ControllerInfo getController()
62: throws NoMoreControllerException {
63: if (aliveControllers.isEmpty())
64: throw new NoMoreControllerException();
65: index++;
66: if (index >= aliveControllers.size())
67: index = 0;
68: ControllerInfo selectedController = super
69: .getControllerByNum(index);
70: if (debugLevel == SequoiaUrl.DEBUG_LEVEL_DEBUG)
71: System.out.println("Selected controller: "
72: + selectedController);
73: return selectedController;
74: }
75: }
|