Skip to content

Commit d89badf

Browse files
GH-8600: Fix WebSocket removeRegistration (#8601)
* GH-8600: Fix WebSocket `removeRegistration` Fixes #8600 When we register a dynamic WebSocket endpoint and use a `WebSocketHandlerDecoratorFactory` such an endpoint is not removed on an `IntegrationFlow` destruction. The actual `WebSocketHandler` is decorated, however we still use an initial one for condition. * Refactor `IntegrationWebSocketContainer` to expose a `protected` setter for the `WebSocketHandler` which is called from the `ServerWebSocketContainer` after decoration **Cherry-pick to `6.0.x` & `5.5.x`** * Fix language in Javadocs Co-authored-by: Gary Russell <[email protected]> --------- Co-authored-by: Gary Russell <[email protected]>
1 parent c44c2e6 commit d89badf

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

spring-integration-websocket/src/main/java/org/springframework/integration/websocket/ClientWebSocketContainer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public void stop(Runnable callback) {
197197
* <p>
198198
* Opened {@link WebSocketSession} is populated to the wrapping {@link ClientWebSocketContainer}.
199199
* <p>
200-
* The {@link #webSocketHandler} is used to handle {@link WebSocketSession} events.
200+
* The {@link #getWebSocketHandler()} is used to handle {@link WebSocketSession} events.
201201
*/
202202
private final class IntegrationWebSocketConnectionManager extends ConnectionManagerSupport {
203203

@@ -242,8 +242,7 @@ protected void openConnection() {
242242
}
243243
ClientWebSocketContainer.this.headers.setSecWebSocketProtocol(getSubProtocols());
244244
ListenableFuture<WebSocketSession> future =
245-
this.client.doHandshake(ClientWebSocketContainer.this.webSocketHandler,
246-
ClientWebSocketContainer.this.headers, getUri());
245+
this.client.doHandshake(getWebSocketHandler(), ClientWebSocketContainer.this.headers, getUri());
247246

248247
future.addCallback(new ListenableFutureCallback<WebSocketSession>() {
249248

spring-integration-websocket/src/main/java/org/springframework/integration/websocket/IntegrationWebSocketContainer.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2021 the original author or authors.
2+
* Copyright 2014-2023 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.
@@ -67,7 +67,7 @@ public abstract class IntegrationWebSocketContainer implements DisposableBean {
6767

6868
protected final Log logger = LogFactory.getLog(getClass()); // NOSONAR
6969

70-
protected final WebSocketHandler webSocketHandler = new IntegrationWebSocketHandler(); // NOSONAR
70+
private WebSocketHandler webSocketHandler = new IntegrationWebSocketHandler();
7171

7272
protected final Map<String, WebSocketSession> sessions = new ConcurrentHashMap<>(); // NOSONAR
7373

@@ -104,6 +104,15 @@ public void addSupportedProtocols(String... protocols) {
104104
}
105105
}
106106

107+
/**
108+
* Replace the default {@link WebSocketHandler} with the one provided here, e.g. via decoration factories.
109+
* @param handler the actual {@link WebSocketHandler} to replace.
110+
* @since 5.5.18
111+
*/
112+
protected void setWebSocketHandler(WebSocketHandler handler) {
113+
this.webSocketHandler = handler;
114+
}
115+
107116
public WebSocketHandler getWebSocketHandler() {
108117
return this.webSocketHandler;
109118
}

spring-integration-websocket/src/main/java/org/springframework/integration/websocket/ServerWebSocketContainer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2022 the original author or authors.
2+
* Copyright 2014-2023 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.
@@ -150,11 +150,12 @@ public TaskScheduler getSockJsTaskScheduler() {
150150

151151
@Override
152152
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
153-
WebSocketHandler webSocketHandler = this.webSocketHandler;
153+
WebSocketHandler webSocketHandler = getWebSocketHandler();
154154

155155
if (this.decoratorFactories != null) {
156156
for (WebSocketHandlerDecoratorFactory factory : this.decoratorFactories) {
157157
webSocketHandler = factory.decorate(webSocketHandler);
158+
setWebSocketHandler(webSocketHandler);
158159
}
159160
}
160161

spring-integration-websocket/src/test/java/org/springframework/integration/websocket/dsl/WebSocketDslTests.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.integration.websocket.dsl;
1818

19+
import java.util.concurrent.atomic.AtomicReference;
20+
1921
import javax.websocket.DeploymentException;
2022

2123
import org.junit.jupiter.api.Test;
@@ -39,6 +41,7 @@
3941
import org.springframework.test.annotation.DirtiesContext;
4042
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
4143
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
44+
import org.springframework.web.socket.WebSocketHandler;
4245
import org.springframework.web.socket.client.WebSocketClient;
4346
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
4447
import org.springframework.web.socket.server.HandshakeHandler;
@@ -48,6 +51,9 @@
4851
import static org.assertj.core.api.Assertions.assertThat;
4952
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5053
import static org.awaitility.Awaitility.await;
54+
import static org.mockito.ArgumentMatchers.any;
55+
import static org.mockito.Mockito.spy;
56+
import static org.mockito.Mockito.verify;
5157

5258
@SpringJUnitConfig(classes = WebSocketDslTests.ClientConfig.class)
5359
@DirtiesContext
@@ -63,13 +69,19 @@ public class WebSocketDslTests {
6369
IntegrationFlowContext integrationFlowContext;
6470

6571
@Test
66-
void testDynamicServerEndpointRegistration() {
72+
void testDynamicServerEndpointRegistration() throws Exception {
6773
// Dynamic server flow
6874
AnnotationConfigWebApplicationContext serverContext = this.server.getServerContext();
6975
IntegrationFlowContext serverIntegrationFlowContext = serverContext.getBean(IntegrationFlowContext.class);
76+
AtomicReference<WebSocketHandler> decoratedHandler = new AtomicReference<>();
7077
ServerWebSocketContainer serverWebSocketContainer =
7178
new ServerWebSocketContainer("/dynamic")
7279
.setHandshakeHandler(serverContext.getBean(HandshakeHandler.class))
80+
.setDecoratorFactories(handler -> {
81+
WebSocketHandler spy = spy(handler);
82+
decoratedHandler.set(spy);
83+
return spy;
84+
})
7385
.withSockJs();
7486

7587
WebSocketInboundChannelAdapter webSocketInboundChannelAdapter =
@@ -108,6 +120,8 @@ void testDynamicServerEndpointRegistration() {
108120
.extracting(Message::getPayload)
109121
.isEqualTo("dynamic test");
110122

123+
verify(decoratedHandler.get()).handleMessage(any(), any());
124+
111125
dynamicServerFlow.destroy();
112126

113127
await() // Looks like endpoint is removed on the server side somewhat async

0 commit comments

Comments
 (0)