diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketHandlerAdapter.java b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketHandlerAdapter.java index bd950d0aec5..893361aea01 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketHandlerAdapter.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketHandlerAdapter.java @@ -57,6 +57,7 @@ public class JettyWebSocketHandlerAdapter implements Session.Listener { this.wsSession = wsSession; } + @Override public void onWebSocketOpen(Session session) { try { @@ -65,7 +66,7 @@ public class JettyWebSocketHandlerAdapter implements Session.Listener { this.webSocketHandler.afterConnectionEstablished(this.wsSession); this.nativeSession.demand(); } - catch (Exception ex) { + catch (Throwable ex) { tryCloseWithError(ex); } } @@ -78,7 +79,7 @@ public class JettyWebSocketHandlerAdapter implements Session.Listener { this.webSocketHandler.handleMessage(this.wsSession, message); this.nativeSession.demand(); } - catch (Exception ex) { + catch (Throwable ex) { tryCloseWithError(ex); } } @@ -92,7 +93,7 @@ public class JettyWebSocketHandlerAdapter implements Session.Listener { this.webSocketHandler.handleMessage(this.wsSession, message); this.nativeSession.demand(); } - catch (Exception ex) { + catch (Throwable ex) { tryCloseWithError(ex); } } @@ -105,18 +106,19 @@ public class JettyWebSocketHandlerAdapter implements Session.Listener { this.webSocketHandler.handleMessage(this.wsSession, message); this.nativeSession.demand(); } - catch (Exception ex) { + catch (Throwable ex) { tryCloseWithError(ex); } } @Override - public void onWebSocketClose(int statusCode, String reason) { + public void onWebSocketClose(int statusCode, String reason, Callback callback) { CloseStatus closeStatus = new CloseStatus(statusCode, reason); + callback.succeed(); try { this.webSocketHandler.afterConnectionClosed(this.wsSession, closeStatus); } - catch (Exception ex) { + catch (Throwable ex) { if (logger.isWarnEnabled()) { logger.warn("Unhandled exception from afterConnectionClosed for " + this, ex); } @@ -128,7 +130,7 @@ public class JettyWebSocketHandlerAdapter implements Session.Listener { try { this.webSocketHandler.handleTransportError(this.wsSession, cause); } - catch (Exception ex) { + catch (Throwable ex) { if (logger.isWarnEnabled()) { logger.warn("Unhandled exception from handleTransportError for " + this, ex); } @@ -146,4 +148,5 @@ public class JettyWebSocketHandlerAdapter implements Session.Listener { } } } + } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/standard/StandardWebSocketHandlerAdapter.java b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/standard/StandardWebSocketHandlerAdapter.java index 44534f4ae81..106357428ac 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/standard/StandardWebSocketHandlerAdapter.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/standard/StandardWebSocketHandlerAdapter.java @@ -102,7 +102,7 @@ public class StandardWebSocketHandlerAdapter extends Endpoint { try { this.handler.afterConnectionEstablished(this.wsSession); } - catch (Exception ex) { + catch (Throwable ex) { ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, ex, logger); } } @@ -112,7 +112,7 @@ public class StandardWebSocketHandlerAdapter extends Endpoint { try { this.handler.handleMessage(this.wsSession, textMessage); } - catch (Exception ex) { + catch (Throwable ex) { ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, ex, logger); } } @@ -122,7 +122,7 @@ public class StandardWebSocketHandlerAdapter extends Endpoint { try { this.handler.handleMessage(this.wsSession, binaryMessage); } - catch (Exception ex) { + catch (Throwable ex) { ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, ex, logger); } } @@ -132,7 +132,7 @@ public class StandardWebSocketHandlerAdapter extends Endpoint { try { this.handler.handleMessage(this.wsSession, pongMessage); } - catch (Exception ex) { + catch (Throwable ex) { ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, ex, logger); } } @@ -143,7 +143,7 @@ public class StandardWebSocketHandlerAdapter extends Endpoint { try { this.handler.afterConnectionClosed(this.wsSession, closeStatus); } - catch (Exception ex) { + catch (Throwable ex) { if (logger.isWarnEnabled()) { logger.warn("Unhandled on-close exception for " + this.wsSession, ex); } @@ -155,7 +155,7 @@ public class StandardWebSocketHandlerAdapter extends Endpoint { try { this.handler.handleTransportError(this.wsSession, exception); } - catch (Exception ex) { + catch (Throwable ex) { ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, ex, logger); } } diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketHandlerAdapterTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketHandlerAdapterTests.java index c2f946dbfbb..7a0c5c82203 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketHandlerAdapterTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketHandlerAdapterTests.java @@ -16,6 +16,7 @@ package org.springframework.web.socket.adapter.jetty; +import org.eclipse.jetty.websocket.api.Callback; import org.eclipse.jetty.websocket.api.Session; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -57,7 +58,7 @@ class JettyWebSocketHandlerAdapterTests { @Test void onClose() throws Exception { - this.adapter.onWebSocketClose(1000, "reason"); + this.adapter.onWebSocketClose(1000, "reason", Callback.NOOP); verify(this.webSocketHandler).afterConnectionClosed(this.webSocketSession, CloseStatus.NORMAL.withReason("reason")); }