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/TServerSocket.java b/lib/java/src/transport/TServerSocket.java
index 8a8421d..ab2b5e7 100644
--- a/lib/java/src/transport/TServerSocket.java
+++ b/lib/java/src/transport/TServerSocket.java
@@ -4,6 +4,7 @@
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
+import java.net.SocketException;
/**
* Wrapper around ServerSocket for Thrift.
@@ -12,19 +13,55 @@
*/
public class TServerSocket extends TServerTransport {
+ /**
+ * Underlying serversocket object
+ */
private ServerSocket serverSocket_ = null;
+
+ /**
+ * Port to listen on
+ */
private int port_ = 0;
+ /**
+ * Timeout for client sockets from accept
+ */
+ private int clientTimeout_ = 0;
+
+ /**
+ * Creates a server socket from underlying socket object
+ */
public TServerSocket(ServerSocket serverSocket) {
- serverSocket_ = serverSocket;
+ this(serverSocket, 0);
}
+ /**
+ * Creates a server socket from underlying socket object
+ */
+ public TServerSocket(ServerSocket serverSocket, int clientTimeout) {
+ serverSocket_ = serverSocket;
+ clientTimeout_ = clientTimeout;
+ }
+
+ /**
+ * Creates just a port listening server socket
+ */
public TServerSocket(int port) throws TTransportException {
+ this(port, 0);
+ }
+
+ /**
+ * Creates just a port listening server socket
+ */
+ public TServerSocket(int port, int clientTimeout) throws TTransportException {
port_ = port;
+ clientTimeout_ = clientTimeout;
try {
+ // Make server socket
serverSocket_ = new ServerSocket();
+ // Prevent 2MSL delay problem on server restarts
serverSocket_.setReuseAddress(true);
- serverSocket_.setSoTimeout(0);
+ // Bind to listening port
serverSocket_.bind(new InetSocketAddress(port_));
} catch (IOException ioe) {
serverSocket_ = null;
@@ -32,7 +69,16 @@
}
}
- public void listen() throws TTransportException {}
+ public void listen() throws TTransportException {
+ // Make sure not to block on accept
+ if (serverSocket_ != null) {
+ try {
+ serverSocket_.setSoTimeout(0);
+ } catch (SocketException sx) {
+ sx.printStackTrace();
+ }
+ }
+ }
protected TSocket acceptImpl() throws TTransportException {
if (serverSocket_ == null) {
@@ -40,7 +86,9 @@
}
try {
Socket result = serverSocket_.accept();
- return new TSocket(result);
+ TSocket result2 = new TSocket(result);
+ result2.setTimeout(clientTimeout_);
+ return result2;
} catch (IOException iox) {
throw new TTransportException(iox);
}
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();
+ }
}
/**