Author: markt
Date: Tue Jun 17 20:31:05 2014
New Revision: 1603287

URL: http://svn.apache.org/r1603287
Log:
Ensure that the asynchronous WebSocket echo endpoint in the examples web 
application always waits for the previous message to complete before it sends 
the next.
Fixes some failures with this endpoint and the Autobahn tests

Modified:
    tomcat/trunk/webapps/docs/changelog.xml
    
tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/echo/EchoAsyncAnnotation.java

Modified: tomcat/trunk/webapps/docs/changelog.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1603287&r1=1603286&r2=1603287&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/trunk/webapps/docs/changelog.xml Tue Jun 17 20:31:05 2014
@@ -282,6 +282,11 @@
         Add options for all of the WebSocket echo endpoints to the WebSocket
         echo example in the examples web application. (markt)
       </add>
+      <fix>
+        Ensure that the asynchronous WebSocket echo endpoint in the examples
+        web application always waits for the previous message to complete 
before
+        it sends the next. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Other">

Modified: 
tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/echo/EchoAsyncAnnotation.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/echo/EchoAsyncAnnotation.java?rev=1603287&r1=1603286&r2=1603287&view=diff
==============================================================================
--- 
tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/echo/EchoAsyncAnnotation.java
 (original)
+++ 
tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/echo/EchoAsyncAnnotation.java
 Tue Jun 17 20:31:05 2014
@@ -19,6 +19,10 @@ package websocket.echo;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 
 import javax.websocket.OnMessage;
 import javax.websocket.PongMessage;
@@ -36,6 +40,9 @@ import javax.websocket.server.ServerEndp
 @ServerEndpoint("/websocket/echoAsyncAnnotation")
 public class EchoAsyncAnnotation {
 
+    private static final Future<Void> COMPLETED = new CompletedFuture();
+
+    Future<Void> f = COMPLETED;
     StringBuilder sb = null;
     ByteArrayOutputStream bytes = null;
 
@@ -46,8 +53,15 @@ public class EchoAsyncAnnotation {
         }
         sb.append(msg);
         if (last) {
-            //System.out.println("Write: " + sb.length());
-            session.getAsyncRemote().sendText(sb.toString());
+            // Before we send the next message, have to wait for the previous
+            // message to complete
+            try {
+                f.get();
+            } catch (InterruptedException | ExecutionException e) {
+                // Let the container deal with it
+                throw new RuntimeException(e);
+            }
+            f = session.getAsyncRemote().sendText(sb.toString());
             sb = null;
         }
     }
@@ -59,10 +73,16 @@ public class EchoAsyncAnnotation {
             bytes = new ByteArrayOutputStream();
         }
         bytes.write(msg);
-        //System.out.println("Got: " + msg.length + " " + last + " " + 
bytes.size());
         if (last) {
-            //System.out.println("Write bytes: " + bytes.size());
-            
session.getAsyncRemote().sendBinary(ByteBuffer.wrap(bytes.toByteArray()));
+            // Before we send the next message, have to wait for the previous
+            // message to complete
+            try {
+                f.get();
+            } catch (InterruptedException | ExecutionException e) {
+                // Let the container deal with it
+                throw new RuntimeException(e);
+            }
+            f = 
session.getAsyncRemote().sendBinary(ByteBuffer.wrap(bytes.toByteArray()));
             bytes = null;
         }
     }
@@ -76,4 +96,34 @@ public class EchoAsyncAnnotation {
     public void echoPongMessage(PongMessage pm) {
         // NO-OP
     }
+
+    private static class CompletedFuture implements Future<Void> {
+
+        @Override
+        public boolean cancel(boolean mayInterruptIfRunning) {
+            return false;
+        }
+
+        @Override
+        public boolean isCancelled() {
+            return false;
+        }
+
+        @Override
+        public boolean isDone() {
+            return true;
+        }
+
+        @Override
+        public Void get() throws InterruptedException, ExecutionException {
+            return null;
+        }
+
+        @Override
+        public Void get(long timeout, TimeUnit unit)
+                throws InterruptedException, ExecutionException,
+                TimeoutException {
+            return null;
+        }
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to