Add socket and server socket timeout support to thrift java
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664804 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/java/src/transport/TSocket.java b/lib/java/src/transport/TSocket.java
index 6b4fa3b..544a1d5 100644
--- a/lib/java/src/transport/TSocket.java
+++ b/lib/java/src/transport/TSocket.java
@@ -5,6 +5,7 @@
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
+import java.net.SocketException;
/**
* Socket implementation of the TTransport interface. To be commented soon!
@@ -13,16 +14,27 @@
*/
public class TSocket extends TIOStreamTransport {
- /** Wrapped Socket object */
+ /**
+ * Wrapped Socket object
+ */
private Socket socket_ = null;
- /** Remote host */
+ /**
+ * Remote host
+ */
private String host_ = null;
- /** Remote port */
+ /**
+ * Remote port
+ */
private int port_ = 0;
/**
+ * Socket timeout
+ */
+ private int timeout_ = 0;
+
+ /**
* Constructor that takes an already created socket.
*
* @param socket Already created socket object
@@ -30,6 +42,7 @@
*/
public TSocket(Socket socket) throws TTransportException {
socket_ = socket;
+
if (isOpen()) {
try {
inputStream_ = new BufferedInputStream(socket_.getInputStream(), 1024);
@@ -49,9 +62,36 @@
* @param port Remote port
*/
public TSocket(String host, int port) {
+ this(host, port, 500);
+ }
+
+ /**
+ * Creates a new unconnected socket that will connect to the given host
+ * on the given port.
+ *
+ * @param host Remote host
+ * @param port Remote port
+ * @param timeout Socket timeout
+ */
+ public TSocket(String host, int port, int timeout) {
socket_ = new Socket();
host_ = host;
port_ = port;
+ timeout_ = timeout;
+ }
+
+ /**
+ * Sets the socket timeout
+ *
+ * @param timeout Milliseconds timeout
+ */
+ public void setTimeout(int timeout) {
+ timeout_ = timeout;
+ try {
+ socket_.setSoTimeout(timeout);
+ } catch (SocketException sx) {
+ sx.printStackTrace();
+ }
}
/**