KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.net.Socket JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import org.objectweb.cjdbc.common.stream.CJDBCOutputStream;
31 import org.objectweb.cjdbc.driver.CjdbcUrl;
32 import org.objectweb.cjdbc.driver.ControllerInfo;
33 import org.objectweb.cjdbc.driver.protocol.Commands;
34
35 /**
36  * This class defines a ControllerPingThread that periodically tries to connect
37  * to a failed controller to check if it is back online.
38  *
39  * @author <a HREF="mailto:emmanuel.cecchet@emicnetworks.com">Emmanuel Cecchet
40  * </a>
41  * @version 1.0
42  */

43 public class ControllerPingThread extends Thread JavaDoc
44 {
45   private AbstractControllerConnectPolicy policy;
46   private long retryIntervalInMs;
47   private boolean threadTerminated = false;
48   private int debugLevel;
49
50   /**
51    * Creates a new <code>ControllerPingThread</code> object.
52    *
53    * @param policy controller connection policy.
54    * @param retryIntervalInMs interval in ms between 2 pings (checks).
55    * @param debugLevel the debug level to use
56    */

57   public ControllerPingThread(AbstractControllerConnectPolicy policy,
58       long retryIntervalInMs, int debugLevel)
59   {
60     super("ControllerPingThread");
61     this.policy = policy;
62     this.retryIntervalInMs = retryIntervalInMs;
63     this.debugLevel = debugLevel;
64   }
65
66   /**
67    * @see java.lang.Runnable#run()
68    */

69   public void run()
70   {
71     HashSet JavaDoc suspectedControllers = policy.getSuspectedControllers();
72     synchronized (suspectedControllers)
73     {
74       while (!suspectedControllers.isEmpty())
75       {
76         // Sleep
77
try
78         {
79           suspectedControllers.wait(retryIntervalInMs);
80         }
81         catch (InterruptedException JavaDoc ignore)
82         {
83         }
84
85         // Try to connect to each controller
86
for (Iterator JavaDoc iter = suspectedControllers.iterator(); iter.hasNext();)
87         {
88           ControllerInfo controller = (ControllerInfo) iter.next();
89           try
90           {
91             Socket JavaDoc socket = new Socket JavaDoc(controller.getHostname(), controller
92                 .getPort());
93
94             // Connection successful
95

96             // Disable Nagle algorithm else small messages are not sent
97
// (at least under Linux) even if we flush the output stream.
98
socket.setTcpNoDelay(true);
99             CJDBCOutputStream out = new CJDBCOutputStream(socket);
100             // Tell the controller not to worry, just a ping
101
out.writeInt(Commands.Ping);
102             out.flush();
103             // The socket will be closed by the controller
104

105             // Full success, remove controller from suspect list
106
policy.removeControllerFromSuspectList(controller);
107             // Regenerate iterator
108
iter = suspectedControllers.iterator();
109           }
110           catch (Exception JavaDoc failed)
111           {
112             if (debugLevel == CjdbcUrl.DEBUG_LEVEL_DEBUG)
113               System.out.println("Ping failed to controller " + controller
114                   + " (" + failed + ")");
115           }
116         }
117       } // while
118
this.threadTerminated = true;
119     } // synchronized
120
}
121
122   /**
123    * Returns true if the thread is terminated.
124    *
125    * @return true if the thread is terminated.
126    */

127   public boolean isTerminated()
128   {
129     return threadTerminated;
130   }
131 }
132
Popular Tags