summaryrefslogtreecommitdiff
path: root/qpid/java
diff options
context:
space:
mode:
authorRobert Godfrey <rgodfrey@apache.org>2014-04-24 19:54:14 +0000
committerRobert Godfrey <rgodfrey@apache.org>2014-04-24 19:54:14 +0000
commitae2b32ed4f0c87e9c2a9b650533f3896ab654d65 (patch)
treeb4e63b62f861277d8610b1c2adb8b113f38d5eed /qpid/java
parente8f4b182c6a6b4df51a4853d21319e624b1203b7 (diff)
downloadqpid-python-ae2b32ed4f0c87e9c2a9b650533f3896ab654d65.tar.gz
QPID-5722 : Client connection read can hang forever since socket timeout is 0 (Patch from Michael Samson)
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1589855 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/java')
-rw-r--r--qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/TCPTransportProvier.java45
1 files changed, 37 insertions, 8 deletions
diff --git a/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/TCPTransportProvier.java b/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/TCPTransportProvier.java
index 6cc749d11d..f4a21ea359 100644
--- a/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/TCPTransportProvier.java
+++ b/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/TCPTransportProvier.java
@@ -33,11 +33,21 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
+import java.net.SocketTimeoutException;
import java.nio.ByteBuffer;
class TCPTransportProvier implements TransportProvider
{
private final String _transport;
+
+ // Defines read socket timeout in milliseconds. A value of 0 means that the socket
+ // read will block forever. Default value is set to 10000, which is 10 seconds.
+ private int _readTimeout = Integer.getInteger("qpid.connection_read_timeout", 10000);
+
+ // Defines the max idle read timeout in milliseconds before the connection is closed down in
+ // the event of a SocketTimeoutException. A value of -1L will disable idle read timeout checking.
+ // Default value is set to -1L, which means disable idle read checks.
+ private long _readIdleTimeout = Long.getLong("qpid.connection_read_idle_timeout", -1L);
public TCPTransportProvier(final String transport)
{
@@ -66,11 +76,11 @@ class TCPTransportProvier implements TransportProvider
{
s = new Socket(address, port);
}
+ // set socket read timeout
+ s.setSoTimeout(_readTimeout);
conn.setRemoteAddress(s.getRemoteSocketAddress());
-
-
ConnectionHandler.FrameOutput<FrameBody> out = new ConnectionHandler.FrameOutput<FrameBody>(conn);
ConnectionHandler.BytesSource src;
@@ -175,15 +185,34 @@ class TCPTransportProvier implements TransportProvider
{
int read;
boolean done = false;
- while(!handler.isDone() && (read = inputStream.read(buf)) != -1)
+ long lastReadTime = System.currentTimeMillis();
+ while(!handler.isDone())
{
- ByteBuffer bbuf = ByteBuffer.wrap(buf, 0, read);
- while(bbuf.hasRemaining() && !handler.isDone())
+ try
+ {
+ read = inputStream.read(buf);
+ if(read == -1)
+ {
+ break;
+ }
+ lastReadTime = System.currentTimeMillis();
+
+ ByteBuffer bbuf = ByteBuffer.wrap(buf, 0, read);
+ while(bbuf.hasRemaining() && !handler.isDone())
+ {
+ handler.parse(bbuf);
+ }
+
+ }
+ catch(SocketTimeoutException e)
{
- handler.parse(bbuf);
+ // Note that a SocketTimeoutException could only occur if _readTimeout > 0.
+ // Only perform idle read timeout checking if _readIdleTimeout is greater than -1
+ if(_readIdleTimeout > -1 && (System.currentTimeMillis() - lastReadTime >= _readIdleTimeout)){
+ // break out of while loop and close down connection
+ break;
+ }
}
-
-
}
if(!handler.isDone())
{