Use static exception instance when a connection is closed

This commit is contained in:
Chris Eager authored and Chris Eager committed 2023-09-07 16:06:16 -05:00
1 parent 8348263fab
commit 92bb783cbb
2 files changed
+9 -1

No files matched your search

@@ -47,6 +47,11 @@ import java.util.concurrent.ConcurrentHashMap;
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public class WebSocketResourceProvider<T extends Principal> implements WebSocketListener {
/**
* A static exception instance passed to outstanding requests (via {@code completeExceptionally} in
* {@link #onWebSocketClose(int, String)}
*/
public static final IOException CONNECTION_CLOSED_EXCEPTION = new IOException("Connection closed!");
private static final Logger logger = LoggerFactory.getLogger(WebSocketResourceProvider.class);
private final Map<Long, CompletableFuture<WebSocketResponseMessage>> requestMap = new ConcurrentHashMap<>();
@@ -141,7 +146,7 @@ public class WebSocketResourceProvider<T extends Principal> implements WebSocket
CompletableFuture<WebSocketResponseMessage> outstandingRequest = requestMap.remove(requestId);
if (outstandingRequest != null) {
outstandingRequest.completeExceptionally(new IOException("Connection closed!"));
outstandingRequest.completeExceptionally(CONNECTION_CLOSED_EXCEPTION);
}
}
}