KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > driver > connectpolicy > RoundRobinConnectPolicy


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2005 Emic Networks.
4  * Contact: c-jdbc@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or any later
9  * version.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19  *
20  * Initial developer(s): Emmanuel Cecchet.
21  * Contributor(s): ______________________.
22  */

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

41 public class RoundRobinConnectPolicy extends AbstractControllerConnectPolicy
42 {
43   private int index = -1;
44
45   /**
46    * Creates a new <code>RandomConnectPolicy</code> object
47    *
48    * @param controllerList list of controller from C-JDBC url
49    * @param retryIntervalInMs Interval in milliseconds before retrying to
50    * re-connect to a controller that has failed
51    * @param debugLevel the debug level to use
52    * @see org.objectweb.cjdbc.driver.CjdbcUrl#DEBUG_LEVEL_OFF
53    */

54   public RoundRobinConnectPolicy(ControllerInfo[] controllerList,
55       long retryIntervalInMs, int debugLevel)
56   {
57     super(controllerList, retryIntervalInMs, debugLevel);
58   }
59
60   /**
61    * @see org.objectweb.cjdbc.driver.connectpolicy.AbstractControllerConnectPolicy#getController()
62    */

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