[jbossweb-commits] JBossWeb SVN: r2289 - branches/7.4.x/src/main/java/org/apache/tomcat/websocket.

jbossweb-commits at lists.jboss.org jbossweb-commits at lists.jboss.org
Wed Oct 23 04:36:46 EDT 2013


Author: remy.maucherat at jboss.com
Date: 2013-10-23 04:36:46 -0400 (Wed, 23 Oct 2013)
New Revision: 2289

Modified:
   branches/7.4.x/src/main/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
Log:
Port patch adding connection timeout to websockets.

Modified: branches/7.4.x/src/main/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
===================================================================
--- branches/7.4.x/src/main/java/org/apache/tomcat/websocket/WsWebSocketContainer.java	2013-10-23 08:31:30 UTC (rev 2288)
+++ branches/7.4.x/src/main/java/org/apache/tomcat/websocket/WsWebSocketContainer.java	2013-10-23 08:36:46 UTC (rev 2289)
@@ -49,6 +49,7 @@
 import java.util.concurrent.SynchronousQueue;
 import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import javax.net.ssl.SSLContext;
@@ -87,6 +88,16 @@
             "org.apache.tomcat.websocket.SSL_TRUSTSTORE_PWD";
     public static final String SSL_TRUSTSTORE_PWD_DEFAULT = "changeit";
 
+    /**
+     * Property name to set to configure the timeout (in milliseconds) when
+     * establishing a WebSocket connection to server. The default is
+     * {@link #IO_TIMEOUT_MS_DEFAULT}.
+     */
+    public static final String IO_TIMEOUT_MS_PROPERTY =
+            "org.apache.tomcat.websocket.IO_TIMEOUT_MS";
+
+    public static final long IO_TIMEOUT_MS_DEFAULT = 5000;
+
     private static final Random random = new Random();
     private static final byte[] crlf = new byte[] {13, 10};
     private static final AsynchronousChannelGroup asynchronousChannelGroup;
@@ -273,30 +284,38 @@
             channel = new AsyncChannelWrapperNonSecure(socketChannel);
         }
 
+        // Get the connection timeout
+        long timeout = IO_TIMEOUT_MS_DEFAULT;
+        String timeoutValue = (String) clientEndpointConfiguration.getUserProperties().get(
+                IO_TIMEOUT_MS_PROPERTY);
+        if (timeoutValue != null) {
+            timeout = Long.valueOf(timeoutValue).intValue();
+        }
+
         ByteBuffer response;
         String subProtocol;
         try {
-            fConnect.get();
+            fConnect.get(timeout, TimeUnit.MILLISECONDS);
 
             Future<Void> fHandshake = channel.handshake();
-            fHandshake.get();
+            fHandshake.get(timeout, TimeUnit.MILLISECONDS);
 
             int toWrite = request.limit();
 
             Future<Integer> fWrite = channel.write(request);
-            Integer thisWrite = fWrite.get();
+            Integer thisWrite = fWrite.get(timeout, TimeUnit.MILLISECONDS);
             toWrite -= thisWrite.intValue();
 
             while (toWrite > 0) {
                 fWrite = channel.write(request);
-                thisWrite = fWrite.get();
+                thisWrite = fWrite.get(timeout, TimeUnit.MILLISECONDS);
                 toWrite -= thisWrite.intValue();
             }
             // Same size as the WsFrame input buffer
             response = ByteBuffer.allocate(maxBinaryMessageBufferSize);
 
             HandshakeResponse handshakeResponse =
-                    processResponse(response, channel);
+                    processResponse(response, channel, timeout);
             clientEndpointConfiguration.getConfigurator().
                     afterResponse(handshakeResponse);
 
@@ -319,6 +338,8 @@
             throw new DeploymentException(MESSAGES.httpRequestFailed(), e);
         } catch (EOFException e) {
             throw new DeploymentException(MESSAGES.httpRequestFailed(), e);
+        } catch (TimeoutException e) {
+            throw new DeploymentException(MESSAGES.httpRequestFailed(), e);
         }
 
         // Switch to WebSocket
@@ -528,10 +549,12 @@
      * @throws ExecutionException
      * @throws InterruptedException
      * @throws DeploymentException
+     * @throws TimeoutException
      */
     private HandshakeResponse processResponse(ByteBuffer response,
-            AsyncChannelWrapper channel) throws InterruptedException,
-            ExecutionException, DeploymentException, EOFException {
+            AsyncChannelWrapper channel, long timeout) throws InterruptedException,
+            ExecutionException, DeploymentException, EOFException,
+            TimeoutException {
 
         Map<String,List<String>> headers = new HashMap<String, List<String>>();
 
@@ -541,7 +564,7 @@
         while (!readHeaders) {
             // Blocking read
             Future<Integer> read = channel.read(response);
-            Integer bytesRead = read.get();
+            Integer bytesRead = read.get(timeout, TimeUnit.MILLISECONDS);
             if (bytesRead.intValue() == -1) {
                 throw new EOFException();
             }



More information about the jbossweb-commits mailing list