Skip to content

Commit e9ecaf6

Browse files
committed
Fix issue with StompSubProtocolHandler initialization
This change ensures that StompSubProtocolHandler is injected with an ApplicationEventPublisher for both the Java and XML config. Backport for: 0dddb6f 4372dac Issue: SPR-11825
1 parent 5092414 commit e9ecaf6

File tree

4 files changed

+11
-22
lines changed

4 files changed

+11
-22
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistry.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222
import java.util.Map;
2323

24+
import org.springframework.context.ApplicationContext;
2425
import org.springframework.messaging.simp.user.UserSessionRegistry;
2526
import org.springframework.scheduling.TaskScheduler;
2627
import org.springframework.util.Assert;
@@ -90,6 +91,9 @@ private static SubProtocolWebSocketHandler unwrapSubProtocolWebSocketHandler(Web
9091
return (SubProtocolWebSocketHandler) actual;
9192
}
9293

94+
protected void setApplicationContext(ApplicationContext applicationContext) {
95+
this.stompHandler.setApplicationEventPublisher(applicationContext);
96+
}
9397

9498
@Override
9599
public StompWebSocketEndpointRegistration addEndpoint(String... paths) {

spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurationSupport.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import org.springframework.context.annotation.Bean;
2020
import org.springframework.messaging.simp.config.AbstractMessageBrokerConfiguration;
21-
import org.springframework.messaging.simp.user.UserSessionRegistry;
2221
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
2322
import org.springframework.web.servlet.HandlerMapping;
2423
import org.springframework.web.socket.WebSocketHandler;
@@ -46,16 +45,11 @@ protected WebSocketMessageBrokerConfigurationSupport() {
4645
@Bean
4746
public HandlerMapping stompWebSocketHandlerMapping() {
4847

49-
WebSocketHandler webSocketHandler = subProtocolWebSocketHandler();
50-
UserSessionRegistry sessionRegistry = userSessionRegistry();
51-
WebSocketTransportRegistration transportRegistration = getTransportRegistration();
52-
ThreadPoolTaskScheduler taskScheduler = messageBrokerSockJsTaskScheduler();
53-
54-
WebMvcStompEndpointRegistry registry = new WebMvcStompEndpointRegistry(
55-
webSocketHandler, transportRegistration, sessionRegistry, taskScheduler);
48+
WebMvcStompEndpointRegistry registry = new WebMvcStompEndpointRegistry(subProtocolWebSocketHandler(),
49+
getTransportRegistration(), userSessionRegistry(), messageBrokerSockJsTaskScheduler());
5650

51+
registry.setApplicationContext(getApplicationContext());
5752
registerStompEndpoints(registry);
58-
5953
return registry.getHandlerMapping();
6054
}
6155

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
* @since 4.0
6666
*/
6767
public class SubProtocolWebSocketHandler implements WebSocketHandler,
68-
SubProtocolCapable, MessageHandler, SmartLifecycle, ApplicationEventPublisherAware {
68+
SubProtocolCapable, MessageHandler, SmartLifecycle {
6969

7070
/**
7171
* Sessions connected to this handler use a sub-protocol. Hence we expect to
@@ -97,12 +97,10 @@ public class SubProtocolWebSocketHandler implements WebSocketHandler,
9797

9898
private final ReentrantLock sessionCheckLock = new ReentrantLock();
9999

100-
private Object lifecycleMonitor = new Object();
100+
private final Object lifecycleMonitor = new Object();
101101

102102
private volatile boolean running = false;
103103

104-
private ApplicationEventPublisher eventPublisher;
105-
106104

107105
public SubProtocolWebSocketHandler(MessageChannel clientInboundChannel, SubscribableChannel clientOutboundChannel) {
108106
Assert.notNull(clientInboundChannel, "ClientInboundChannel must not be null");
@@ -147,10 +145,6 @@ public void addProtocolHandler(SubProtocolHandler handler) {
147145
+ " to protocol '" + protocol + "', it is already mapped to handler " + replaced);
148146
}
149147
}
150-
151-
if (handler instanceof ApplicationEventPublisherAware) {
152-
((ApplicationEventPublisherAware) handler).setApplicationEventPublisher(this.eventPublisher);
153-
}
154148
}
155149

156150
/**
@@ -203,11 +197,6 @@ public int getSendBufferSizeLimit() {
203197
return sendBufferSizeLimit;
204198
}
205199

206-
@Override
207-
public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) {
208-
this.eventPublisher = eventPublisher;
209-
}
210-
211200
@Override
212201
public boolean isAutoStartup() {
213202
return true;

spring-websocket/src/test/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParserTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ public void simpleBroker() {
118118
assertNotNull(stompHandler);
119119
assertEquals(128 * 1024, stompHandler.getMessageSizeLimit());
120120

121+
assertNotNull(new DirectFieldAccessor(stompHandler).getPropertyValue("eventPublisher"));
122+
121123
httpRequestHandler = (HttpRequestHandler) suhm.getUrlMap().get("/test/**");
122124
assertNotNull(httpRequestHandler);
123125
assertThat(httpRequestHandler, Matchers.instanceOf(SockJsHttpRequestHandler.class));

0 commit comments

Comments
 (0)