Skip to content

Commit 0dddb6f

Browse files
Artem Bilanrstoyanchev
authored andcommitted
Fix issue with StompSubProtocolHandler initialization
This change ensures that StompSubProtocolHandler is injected with an ApplicationEventPublisher for both the Java and XML config. Issue: SPR-11825
1 parent 5ed9bed commit 0dddb6f

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import java.util.List;
2222
import java.util.Map;
2323

24+
import org.springframework.beans.BeansException;
25+
import org.springframework.context.ApplicationContext;
26+
import org.springframework.context.ApplicationContextAware;
2427
import org.springframework.messaging.simp.user.UserSessionRegistry;
2528
import org.springframework.scheduling.TaskScheduler;
2629
import org.springframework.util.Assert;
@@ -38,9 +41,10 @@
3841
* {@link SimpleUrlHandlerMapping} for use in Spring MVC.
3942
*
4043
* @author Rossen Stoyanchev
44+
* @author Artem Bilan
4145
* @since 4.0
4246
*/
43-
public class WebMvcStompEndpointRegistry implements StompEndpointRegistry {
47+
public class WebMvcStompEndpointRegistry implements StompEndpointRegistry, ApplicationContextAware {
4448

4549
private final WebSocketHandler webSocketHandler;
4650

@@ -84,6 +88,11 @@ public WebMvcStompEndpointRegistry(WebSocketHandler webSocketHandler,
8488
this.sockJsScheduler = defaultSockJsTaskScheduler;
8589
}
8690

91+
@Override
92+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
93+
this.stompHandler.setApplicationEventPublisher(applicationContext);
94+
}
95+
8796
private static SubProtocolWebSocketHandler unwrapSubProtocolWebSocketHandler(WebSocketHandler wsHandler) {
8897
WebSocketHandler actual = WebSocketHandlerDecorator.unwrap(wsHandler);
8998
Assert.isInstanceOf(SubProtocolWebSocketHandler.class, actual, "No SubProtocolWebSocketHandler in " + wsHandler);

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.web.socket.config.annotation;
1818

19+
import java.util.Collections;
20+
1921
import org.springframework.beans.factory.config.CustomScopeConfigurer;
2022
import org.springframework.context.annotation.Bean;
2123
import org.springframework.messaging.simp.SimpSessionScope;
@@ -26,8 +28,6 @@
2628
import org.springframework.web.socket.WebSocketHandler;
2729
import org.springframework.web.socket.messaging.SubProtocolWebSocketHandler;
2830

29-
import java.util.Collections;
30-
3131
/**
3232
* Extends {@link AbstractMessageBrokerConfiguration} and adds configuration for
3333
* receiving and responding to STOMP messages from WebSocket clients.
@@ -37,6 +37,7 @@
3737
* also be extended directly.
3838
*
3939
* @author Rossen Stoyanchev
40+
* @author Artem Bilan
4041
* @since 4.0
4142
*/
4243
public abstract class WebSocketMessageBrokerConfigurationSupport extends AbstractMessageBrokerConfiguration {
@@ -58,6 +59,8 @@ public HandlerMapping stompWebSocketHandlerMapping() {
5859
WebMvcStompEndpointRegistry registry = new WebMvcStompEndpointRegistry(
5960
webSocketHandler, transportRegistration, sessionRegistry, taskScheduler);
6061

62+
registry.setApplicationContext(getApplicationContext());
63+
6164
registerStompEndpoints(registry);
6265

6366
return registry.getHandlerMapping();

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import org.apache.commons.logging.Log;
2929
import org.apache.commons.logging.LogFactory;
3030

31-
import org.springframework.context.ApplicationEventPublisher;
32-
import org.springframework.context.ApplicationEventPublisherAware;
3331
import org.springframework.context.SmartLifecycle;
3432
import org.springframework.messaging.Message;
3533
import org.springframework.messaging.MessageChannel;
@@ -60,10 +58,11 @@
6058
*
6159
* @author Rossen Stoyanchev
6260
* @author Andy Wilkinson
61+
* @author Artem Bilan
6362
* @since 4.0
6463
*/
6564
public class SubProtocolWebSocketHandler implements WebSocketHandler,
66-
SubProtocolCapable, MessageHandler, SmartLifecycle, ApplicationEventPublisherAware {
65+
SubProtocolCapable, MessageHandler, SmartLifecycle {
6766

6867
private final Log logger = LogFactory.getLog(SubProtocolWebSocketHandler.class);
6968

@@ -82,12 +81,10 @@ public class SubProtocolWebSocketHandler implements WebSocketHandler,
8281

8382
private int sendBufferSizeLimit = 512 * 1024;
8483

85-
private Object lifecycleMonitor = new Object();
84+
private final Object lifecycleMonitor = new Object();
8685

8786
private volatile boolean running = false;
8887

89-
private ApplicationEventPublisher eventPublisher;
90-
9188

9289
public SubProtocolWebSocketHandler(MessageChannel clientInboundChannel, SubscribableChannel clientOutboundChannel) {
9390
Assert.notNull(clientInboundChannel, "ClientInboundChannel must not be null");
@@ -132,10 +129,6 @@ public void addProtocolHandler(SubProtocolHandler handler) {
132129
+ " to protocol '" + protocol + "', it is already mapped to handler " + replaced);
133130
}
134131
}
135-
136-
if (handler instanceof ApplicationEventPublisherAware) {
137-
((ApplicationEventPublisherAware) handler).setApplicationEventPublisher(this.eventPublisher);
138-
}
139132
}
140133

141134
/**
@@ -188,11 +181,6 @@ public int getSendBufferSizeLimit() {
188181
return sendBufferSizeLimit;
189182
}
190183

191-
@Override
192-
public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) {
193-
this.eventPublisher = eventPublisher;
194-
}
195-
196184
@Override
197185
public boolean isAutoStartup() {
198186
return true;

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.web.socket.config;
1818

19+
import static org.junit.Assert.*;
20+
1921
import java.util.ArrayList;
2022
import java.util.Arrays;
2123
import java.util.List;
@@ -39,12 +41,12 @@
3941
import org.springframework.messaging.converter.StringMessageConverter;
4042
import org.springframework.messaging.simp.SimpMessagingTemplate;
4143
import org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler;
42-
import org.springframework.messaging.simp.user.DefaultUserDestinationResolver;
4344
import org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler;
45+
import org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler;
46+
import org.springframework.messaging.simp.user.DefaultUserDestinationResolver;
4447
import org.springframework.messaging.simp.user.UserDestinationMessageHandler;
4548
import org.springframework.messaging.simp.user.UserDestinationResolver;
4649
import org.springframework.messaging.simp.user.UserSessionRegistry;
47-
import org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler;
4850
import org.springframework.messaging.support.AbstractSubscribableChannel;
4951
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
5052
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
@@ -64,13 +66,12 @@
6466
import org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService;
6567
import org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler;
6668

67-
import static org.junit.Assert.*;
68-
6969
/**
7070
* Test fixture for MessageBrokerBeanDefinitionParser.
7171
* See test configuration files websocket-config-broker-*.xml.
7272
*
7373
* @author Brian Clozel
74+
* @author Artem Bilan
7475
*/
7576
public class MessageBrokerBeanDefinitionParserTests {
7677

@@ -118,6 +119,8 @@ public void simpleBroker() {
118119
assertNotNull(stompHandler);
119120
assertEquals(128 * 1024, stompHandler.getMessageSizeLimit());
120121

122+
assertNotNull(new DirectFieldAccessor(stompHandler).getPropertyValue("eventPublisher"));
123+
121124
httpRequestHandler = (HttpRequestHandler) suhm.getUrlMap().get("/test/**");
122125
assertNotNull(httpRequestHandler);
123126
assertThat(httpRequestHandler, Matchers.instanceOf(SockJsHttpRequestHandler.class));
@@ -365,4 +368,4 @@ private WebSocketHandler unwrapWebSocketHandler(WebSocketHandler handler) {
365368
((WebSocketHandlerDecorator) handler).getLastHandler() : handler;
366369
}
367370

368-
}
371+
}

0 commit comments

Comments
 (0)