Skip to content

Commit 6a5953d

Browse files
committed
Polishing in WebSocketStompClient
See gh-32195
1 parent b415361 commit 6a5953d

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/DefaultStompSession.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -109,8 +109,7 @@ public class DefaultStompSession implements ConnectionHandlingStompSession {
109109

110110
private final Map<String, ReceiptHandler> receiptHandlers = new ConcurrentHashMap<>(4);
111111

112-
/* Whether the client is willfully closing the connection */
113-
private volatile boolean closing;
112+
private volatile boolean clientSideClose;
114113

115114

116115
/**
@@ -368,7 +367,7 @@ public void disconnect() {
368367

369368
@Override
370369
public void disconnect(@Nullable StompHeaders headers) {
371-
this.closing = true;
370+
this.clientSideClose = true;
372371
try {
373372
StompHeaderAccessor accessor = createHeaderAccessor(StompCommand.DISCONNECT);
374373
if (headers != null) {
@@ -519,7 +518,7 @@ public void afterConnectionClosed() {
519518
if (logger.isDebugEnabled()) {
520519
logger.debug("Connection closed in session id=" + this.sessionId);
521520
}
522-
if (!this.closing) {
521+
if (!this.clientSideClose) {
523522
resetConnection();
524523
handleFailure(new ConnectionLostException("Connection closed"));
525524
}
@@ -729,11 +728,11 @@ private class ReadInactivityTask implements Runnable {
729728

730729
@Override
731730
public void run() {
732-
closing = true;
733-
String error = "Server has gone quiet. Closing connection in session id=" + sessionId + ".";
731+
String error = "Read inactivity. Closing connection in session id=" + sessionId + ".";
734732
if (logger.isDebugEnabled()) {
735733
logger.debug(error);
736734
}
735+
clientSideClose = true;
737736
resetConnection();
738737
handleFailure(new IllegalStateException(error));
739738
}

spring-websocket/src/main/java/org/springframework/web/socket/messaging/WebSocketStompClient.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -372,7 +372,7 @@ protected StompHeaders processConnectHeaders(@Nullable StompHeaders connectHeade
372372
private class WebSocketTcpConnectionHandlerAdapter implements BiConsumer<WebSocketSession, Throwable>,
373373
WebSocketHandler, TcpConnection<byte[]> {
374374

375-
private final TcpConnectionHandler<byte[]> connectionHandler;
375+
private final TcpConnectionHandler<byte[]> stompSession;
376376

377377
private final StompWebSocketMessageCodec codec = new StompWebSocketMessageCodec(getInboundMessageSizeLimit());
378378

@@ -385,17 +385,17 @@ private class WebSocketTcpConnectionHandlerAdapter implements BiConsumer<WebSock
385385

386386
private final List<ScheduledFuture<?>> inactivityTasks = new ArrayList<>(2);
387387

388-
public WebSocketTcpConnectionHandlerAdapter(TcpConnectionHandler<byte[]> connectionHandler) {
389-
Assert.notNull(connectionHandler, "TcpConnectionHandler must not be null");
390-
this.connectionHandler = connectionHandler;
388+
public WebSocketTcpConnectionHandlerAdapter(TcpConnectionHandler<byte[]> stompSession) {
389+
Assert.notNull(stompSession, "TcpConnectionHandler must not be null");
390+
this.stompSession = stompSession;
391391
}
392392

393393
// CompletableFuture callback implementation: handshake outcome
394394

395395
@Override
396396
public void accept(@Nullable WebSocketSession webSocketSession, @Nullable Throwable throwable) {
397397
if (throwable != null) {
398-
this.connectionHandler.afterConnectFailure(throwable);
398+
this.stompSession.afterConnectFailure(throwable);
399399
}
400400
}
401401

@@ -404,7 +404,7 @@ public void accept(@Nullable WebSocketSession webSocketSession, @Nullable Throwa
404404
@Override
405405
public void afterConnectionEstablished(WebSocketSession session) {
406406
this.session = session;
407-
this.connectionHandler.afterConnected(this);
407+
this.stompSession.afterConnected(this);
408408
}
409409

410410
@Override
@@ -415,23 +415,23 @@ public void handleMessage(WebSocketSession session, WebSocketMessage<?> webSocke
415415
messages = this.codec.decode(webSocketMessage);
416416
}
417417
catch (Throwable ex) {
418-
this.connectionHandler.handleFailure(ex);
418+
this.stompSession.handleFailure(ex);
419419
return;
420420
}
421421
for (Message<byte[]> message : messages) {
422-
this.connectionHandler.handleMessage(message);
422+
this.stompSession.handleMessage(message);
423423
}
424424
}
425425

426426
@Override
427-
public void handleTransportError(WebSocketSession session, Throwable ex) throws Exception {
428-
this.connectionHandler.handleFailure(ex);
427+
public void handleTransportError(WebSocketSession session, Throwable ex) {
428+
this.stompSession.handleFailure(ex);
429429
}
430430

431431
@Override
432-
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
432+
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) {
433433
cancelInactivityTasks();
434-
this.connectionHandler.afterConnectionClosed();
434+
this.stompSession.afterConnectionClosed();
435435
}
436436

437437
private void cancelInactivityTasks() {

0 commit comments

Comments
 (0)