Improve CMS poing
authorMathieu Baudier <mbaudier@argeo.org>
Mon, 31 Oct 2022 05:43:27 +0000 (06:43 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Mon, 31 Oct 2022 05:43:27 +0000 (06:43 +0100)
org.argeo.cms.ee/src/org/argeo/cms/websocket/server/PingEndpoint.java
org.argeo.cms.lib.jetty/src/org/argeo/cms/jetty/JettyHttpServer.java
org.argeo.cms/src/org/argeo/cms/client/WsPing.java

index b81cc591d0ac3f924b98d9c7e4c0e3463ff477dd..5ae491acfe15227b4337afe66f0a883c021582ea 100644 (file)
@@ -1,5 +1,7 @@
 package org.argeo.cms.websocket.server;
 
+import java.nio.channels.ClosedChannelException;
+
 import javax.websocket.OnError;
 import javax.websocket.server.ServerEndpoint;
 
@@ -11,6 +13,10 @@ public class PingEndpoint {
 
        @OnError
        public void onError(Throwable e) {
+               if (e instanceof ClosedChannelException) {
+                       // ignore, as it probably means ping was closed on the other side
+                       return;
+               }
                log.error("Cannot process ping", e);
        }
 }
index 027ef9f3dcba0b54dc1a1371e126f9f45e566642..eb7b957ddf35690db78ec6c7fb99787fff54656b 100644 (file)
@@ -124,7 +124,7 @@ public class JettyHttpServer extends HttpsServer {
                        started = true;
                } catch (Exception e) {
                        stop();
-                       throw new IllegalStateException("Cannot start Jetty HTTPS server", e);
+                       throw new IllegalStateException("Cannot start Jetty HTTP server", e);
                }
        }
 
index 55d6f047fbe0c47e5f4d9eb8deeefa79ffa4f2e9..caf6796713ddc49eb3c8b89e62298085775980c7 100644 (file)
@@ -14,17 +14,19 @@ import java.util.concurrent.ExecutionException;
 /** Tests connectivity to the web socket server. */
 public class WsPing implements Runnable {
        private final static int PING_FRAME_SIZE = 125;
+       private final static DecimalFormat decimalFormat = new DecimalFormat("0.0");
+       static {
+               decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
+       }
 
        private final URI uri;
        private final UUID uuid;
 
-       private final DecimalFormat decimalFormat;
+       private WebSocket webSocket;
 
        public WsPing(URI uri) {
                this.uri = uri;
                this.uuid = UUID.randomUUID();
-               decimalFormat = new DecimalFormat("0.0");
-               decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
        }
 
        @Override
@@ -52,7 +54,7 @@ public class WsPing implements Runnable {
 
                        HttpClient client = HttpClient.newHttpClient();
                        CompletableFuture<WebSocket> ws = client.newWebSocketBuilder().buildAsync(uri, listener);
-                       WebSocket webSocket = ws.get();
+                       webSocket = ws.get();
                        webSocket.request(Long.MAX_VALUE);
 
                        Runtime.getRuntime().addShutdownHook(new Thread(() -> webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "")));
@@ -67,19 +69,22 @@ public class WsPing implements Runnable {
                                webSocket.sendPing(buffer);
                                Thread.sleep(1000);
                        }
-               } catch (InterruptedException | ExecutionException e) {
-                       e.printStackTrace();
+               } catch (InterruptedException e) {
+                       if (webSocket != null)
+                               webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "");
+               } catch (ExecutionException e) {
+                       throw new RuntimeException("Cannot ping " + uri, e.getCause());
                }
        }
 
-       public static void main(String[] args) throws Exception {
-               if (args.length == 0) {
-                       System.err.println("usage: java " + WsPing.class.getName() + " <url>");
-                       System.exit(1);
-                       return;
-               }
-               URI uri = URI.create(args[0]);
-               new WsPing(uri).run();
-       }
+//     public static void main(String[] args) throws Exception {
+//             if (args.length == 0) {
+//                     System.err.println("usage: java " + WsPing.class.getName() + " <url>");
+//                     System.exit(1);
+//                     return;
+//             }
+//             URI uri = URI.create(args[0]);
+//             new WsPing(uri).run();
+//     }
 
 }