KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > driver > connectpolicy > RoundRobinConnectPolicy


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2005 Emic Networks.
4  * Contact: sequoia@continuent.org
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
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  */

21
22 package org.continuent.sequoia.driver.connectpolicy;
23
24 import org.continuent.sequoia.common.exceptions.NoMoreControllerException;
25 import org.continuent.sequoia.driver.ControllerInfo;
26 import org.continuent.sequoia.driver.SequoiaUrl;
27
28 /**
29  * This class defines a RoundRobinConnectPolicy used when the Sequoia URL has
30  * the following form:
31  * jdbc:sequoia://node1,node2,node3/myDB?preferredController=roundRobin
32  * <p>
33  * Round robin starts with the first node in URL.
34  *
35  * @author <a HREF="mailto:emmanuel.cecchet@emicnetworks.com">Emmanuel Cecchet
36  * </a>
37  * @version 1.0
38  */

39 public class RoundRobinConnectPolicy extends AbstractControllerConnectPolicy
40 {
41   private int index = -1;
42
43   /**
44    * Creates a new <code>RandomConnectPolicy</code> object
45    *
46    * @param controllerList list of controller from Sequoia url
47    * @param retryIntervalInMs Interval in milliseconds before retrying to
48    * re-connect to a controller that has failed
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       long retryIntervalInMs, int debugLevel)
54   {
55     super(controllerList, retryIntervalInMs, debugLevel);
56   }
57
58   /**
59    * @see org.continuent.sequoia.driver.connectpolicy.AbstractControllerConnectPolicy#getController()
60    */

61   public ControllerInfo getController() throws NoMoreControllerException
62   {
63     synchronized (suspectedControllers)
64     {
65       int looked = 0;
66       do
67       {
68         index = (index + 1) % controllerList.length;
69         looked++;
70       }
71       while (suspectedControllers.contains(controllerList[index])
72           && (looked <= controllerList.length));
73       if (looked > controllerList.length)
74         throw new NoMoreControllerException();
75       if (debugLevel == SequoiaUrl.DEBUG_LEVEL_DEBUG)
76         System.out.println("Selected controller[" + index + "]:"
77             + controllerList[index]);
78       return controllerList[index];
79     }
80   }
81
82 }
83
Popular Tags