Skip to content

Commit 20d5f62

Browse files
artembilanpziobron
andauthored
spring-projectsGH-8644: WebSocketHandlerReg: Remove ThreadLocal (spring-projects#8667)
Fixes spring-projects#8644 * Introduce an inner `DynamicHandlerRegistrationProxy` class to create an `IntegrationDynamicWebSocketHandlerRegistration` and map it into a provided `WebSocketHandler` when a `dynamicHandlerMapping` is in action. Co-authored-by: pziobron <[email protected]>
1 parent fc3c8d2 commit 20d5f62

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

spring-integration-websocket/src/main/java/org/springframework/integration/websocket/config/IntegrationServletWebSocketHandlerRegistry.java

+27-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 the original author or authors.
2+
* Copyright 2021-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.
@@ -34,6 +34,7 @@
3434
import org.springframework.web.socket.config.annotation.ServletWebSocketHandlerRegistration;
3535
import org.springframework.web.socket.config.annotation.ServletWebSocketHandlerRegistry;
3636
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistration;
37+
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
3738

3839
/**
3940
* The {@link ServletWebSocketHandlerRegistry} extension for Spring Integration purpose, especially
@@ -46,8 +47,6 @@
4647
class IntegrationServletWebSocketHandlerRegistry extends ServletWebSocketHandlerRegistry
4748
implements ApplicationContextAware, DestructionAwareBeanPostProcessor {
4849

49-
private final ThreadLocal<IntegrationDynamicWebSocketHandlerRegistration> currentRegistration = new ThreadLocal<>();
50-
5150
private final Map<WebSocketHandler, List<String>> dynamicRegistrations = new HashMap<>();
5251

5352
private ApplicationContext applicationContext;
@@ -78,30 +77,17 @@ public AbstractHandlerMapping getHandlerMapping() {
7877
return originHandlerMapping;
7978
}
8079

81-
@Override
82-
public WebSocketHandlerRegistration addHandler(WebSocketHandler handler, String... paths) {
83-
if (this.dynamicHandlerMapping != null) {
84-
IntegrationDynamicWebSocketHandlerRegistration registration =
85-
new IntegrationDynamicWebSocketHandlerRegistration();
86-
registration.addHandler(handler, paths);
87-
this.currentRegistration.set(registration);
88-
return registration;
89-
}
90-
else {
91-
return super.addHandler(handler, paths);
92-
}
93-
}
94-
9580
@Override
9681
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
97-
if (this.dynamicHandlerMapping != null && bean instanceof ServerWebSocketContainer) {
98-
ServerWebSocketContainer serverWebSocketContainer = (ServerWebSocketContainer) bean;
82+
if (this.dynamicHandlerMapping != null && bean instanceof ServerWebSocketContainer serverWebSocketContainer) {
9983
if (serverWebSocketContainer.getSockJsTaskScheduler() == null) {
10084
serverWebSocketContainer.setSockJsTaskScheduler(this.sockJsTaskScheduler);
10185
}
102-
serverWebSocketContainer.registerWebSocketHandlers(this);
103-
IntegrationDynamicWebSocketHandlerRegistration registration = this.currentRegistration.get();
104-
this.currentRegistration.remove();
86+
87+
DynamicHandlerRegistrationProxy dynamicHandlerRegistrationProxy = new DynamicHandlerRegistrationProxy();
88+
89+
serverWebSocketContainer.registerWebSocketHandlers(dynamicHandlerRegistrationProxy);
90+
IntegrationDynamicWebSocketHandlerRegistration registration = dynamicHandlerRegistrationProxy.registration;
10591
MultiValueMap<HttpRequestHandler, String> mappings = registration.getMapping();
10692
for (Map.Entry<HttpRequestHandler, List<String>> entry : mappings.entrySet()) {
10793
HttpRequestHandler httpHandler = entry.getKey();
@@ -136,11 +122,30 @@ void removeRegistration(ServerWebSocketContainer serverWebSocketContainer) {
136122
}
137123
}
138124

125+
private static final class DynamicHandlerRegistrationProxy implements WebSocketHandlerRegistry {
126+
127+
private IntegrationDynamicWebSocketHandlerRegistration registration;
128+
129+
DynamicHandlerRegistrationProxy() {
130+
}
131+
132+
@Override
133+
public WebSocketHandlerRegistration addHandler(WebSocketHandler webSocketHandler, String... paths) {
134+
this.registration = new IntegrationDynamicWebSocketHandlerRegistration();
135+
this.registration.addHandler(webSocketHandler, paths);
136+
return this.registration;
137+
}
138+
139+
}
140+
139141
private static final class IntegrationDynamicWebSocketHandlerRegistration
140142
extends ServletWebSocketHandlerRegistration {
141143

142144
private WebSocketHandler handler;
143145

146+
IntegrationDynamicWebSocketHandlerRegistration() {
147+
}
148+
144149
@Override
145150
public WebSocketHandlerRegistration addHandler(WebSocketHandler handler, String... paths) {
146151
// The IntegrationWebSocketContainer comes only with a single WebSocketHandler

0 commit comments

Comments
 (0)