Skip to content

Commit bbdb72d

Browse files
sdeleuzerstoyanchev
authored andcommitted
Add configuration for message buffer size limit
BufferingStompDecoder message buffer size limit can now be configured with JavaConfig MessageBrokerRegistry.setMessageBufferSizeLimit() or with XML <websocket:message-brocker message-buffer-size="">. Issue: SPR-11527
1 parent ebffd67 commit bbdb72d

File tree

14 files changed

+103
-20
lines changed

14 files changed

+103
-20
lines changed

spring-messaging/src/main/java/org/springframework/messaging/simp/config/MessageBrokerRegistry.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -47,6 +47,8 @@ public class MessageBrokerRegistry {
4747

4848
private ChannelRegistration brokerChannelRegistration = new ChannelRegistration();
4949

50+
private Integer messageBufferSizeLimit;
51+
5052

5153
public MessageBrokerRegistry(SubscribableChannel clientInboundChannel, MessageChannel clientOutboundChannel) {
5254
Assert.notNull(clientInboundChannel);
@@ -119,6 +121,22 @@ public ChannelRegistration configureBrokerChannel() {
119121
return this.brokerChannelRegistration;
120122
}
121123

124+
/**
125+
* Configure the message buffer size limit in bytes.
126+
* @since 4.0.3
127+
*/
128+
public MessageBrokerRegistry setMessageBufferSizeLimit(Integer messageBufferSizeLimit) {
129+
this.messageBufferSizeLimit = messageBufferSizeLimit;
130+
return this;
131+
}
132+
133+
/**
134+
* Get the message buffer size limit in bytes.
135+
* @since 4.0.3
136+
*/
137+
public Integer getMessageBufferSizeLimit() {
138+
return this.messageBufferSizeLimit;
139+
}
122140

123141
protected SimpleBrokerMessageHandler getSimpleBroker(SubscribableChannel brokerChannel) {
124142
if ((this.simpleBrokerRegistration == null) && (this.brokerRelayRegistration == null)) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232

3333
/**
34-
* A an extension of {@link org.springframework.messaging.simp.stomp.StompDecoder}
34+
* An extension of {@link org.springframework.messaging.simp.stomp.StompDecoder}
3535
* that chunks any bytes remaining after a single full STOMP frame has been read.
3636
* The remaining bytes may contain more STOMP frames or an incomplete STOMP frame.
3737
*

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,11 @@ public BeanDefinition parse(Element element, ParserContext parserCxt) {
124124
beanName = registerBeanDef(beanDef, parserCxt, source);
125125
RuntimeBeanReference userSessionRegistry = new RuntimeBeanReference(beanName);
126126

127+
String frameBufferSizeAttribute = element.getAttribute("message-buffer-size");
128+
Integer messageBufferSizeLimit = frameBufferSizeAttribute.isEmpty() ? null : Integer.parseInt(frameBufferSizeAttribute);
129+
127130
RuntimeBeanReference subProtocolWsHandler = registerSubProtocolWebSocketHandler(
128-
clientInChannel, clientOutChannel, userSessionRegistry, parserCxt, source);
131+
clientInChannel, clientOutChannel, userSessionRegistry, messageBufferSizeLimit, parserCxt, source);
129132

130133
for(Element stompEndpointElem : DomUtils.getChildElementsByTagName(element, "stomp-endpoint")) {
131134

@@ -228,10 +231,14 @@ else if (!channelName.equals("brokerChannel")) {
228231

229232
private RuntimeBeanReference registerSubProtocolWebSocketHandler(
230233
RuntimeBeanReference clientInChannel, RuntimeBeanReference clientOutChannel,
231-
RuntimeBeanReference userSessionRegistry, ParserContext parserCxt, Object source) {
234+
RuntimeBeanReference userSessionRegistry, Integer messageBufferSizeLimit,
235+
ParserContext parserCxt, Object source) {
232236

233237
RootBeanDefinition stompHandlerDef = new RootBeanDefinition(StompSubProtocolHandler.class);
234238
stompHandlerDef.getPropertyValues().add("userSessionRegistry", userSessionRegistry);
239+
if(messageBufferSizeLimit != null) {
240+
stompHandlerDef.getPropertyValues().add("messageBufferSizeLimit", messageBufferSizeLimit);
241+
}
235242
registerBeanDef(stompHandlerDef, parserCxt, source);
236243

237244
ConstructorArgumentValues cavs = new ConstructorArgumentValues();

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

Lines changed: 6 additions & 1 deletion
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.messaging.simp.config.MessageBrokerRegistry;
2425
import org.springframework.messaging.simp.user.UserSessionRegistry;
2526
import org.springframework.scheduling.TaskScheduler;
2627
import org.springframework.util.Assert;
@@ -57,7 +58,8 @@ public class WebMvcStompEndpointRegistry implements StompEndpointRegistry {
5758

5859

5960
public WebMvcStompEndpointRegistry(WebSocketHandler webSocketHandler,
60-
UserSessionRegistry userSessionRegistry, TaskScheduler defaultSockJsTaskScheduler) {
61+
UserSessionRegistry userSessionRegistry, TaskScheduler defaultSockJsTaskScheduler,
62+
MessageBrokerRegistry brokerRegistry) {
6163

6264
Assert.notNull(webSocketHandler);
6365
Assert.notNull(userSessionRegistry);
@@ -67,6 +69,9 @@ public WebMvcStompEndpointRegistry(WebSocketHandler webSocketHandler,
6769
this.stompHandler = new StompSubProtocolHandler();
6870
this.stompHandler.setUserSessionRegistry(userSessionRegistry);
6971
this.sockJsScheduler = defaultSockJsTaskScheduler;
72+
if(brokerRegistry.getMessageBufferSizeLimit() != null) {
73+
this.stompHandler.setMessageBufferSizeLimit(brokerRegistry.getMessageBufferSizeLimit());
74+
}
7075
}
7176

7277
private static SubProtocolWebSocketHandler unwrapSubProtocolWebSocketHandler(WebSocketHandler webSocketHandler) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -42,7 +42,8 @@ protected WebSocketMessageBrokerConfigurationSupport() {
4242
@Bean
4343
public HandlerMapping stompWebSocketHandlerMapping() {
4444
WebMvcStompEndpointRegistry registry = new WebMvcStompEndpointRegistry(
45-
subProtocolWebSocketHandler(), userSessionRegistry(), messageBrokerSockJsTaskScheduler());
45+
subProtocolWebSocketHandler(), userSessionRegistry(),
46+
messageBrokerSockJsTaskScheduler(), getBrokerRegistry());
4647
registerStompEndpoints(registry);
4748
return registry.getHandlerMapping();
4849
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,16 @@ public class StompSubProtocolHandler implements SubProtocolHandler {
7979

8080

8181
/**
82-
* TODO
83-
* @param messageBufferSizeLimit
82+
* Set the message buffer size limit in bytes.
83+
* @since 4.0.3
8484
*/
8585
public void setMessageBufferSizeLimit(int messageBufferSizeLimit) {
8686
this.messageBufferSizeLimit = messageBufferSizeLimit;
8787
}
8888

8989
/**
90-
* TODO
91-
* @return
90+
* Get the message buffer size limit in bytes.
91+
* @since 4.0.3
9292
*/
9393
public int getMessageBufferSizeLimit() {
9494
return this.messageBufferSizeLimit;

spring-websocket/src/main/resources/org/springframework/web/socket/config/spring-websocket-4.0.xsd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,13 @@
575575
]]></xsd:documentation>
576576
</xsd:annotation>
577577
</xsd:attribute>
578+
<xsd:attribute name="message-buffer-size" type="xsd:int">
579+
<xsd:annotation>
580+
<xsd:documentation><![CDATA[
581+
The message buffer size limit in bytes for simple messaging protocols like STOMP.
582+
]]></xsd:documentation>
583+
</xsd:annotation>
584+
</xsd:attribute>
578585
<xsd:attribute name="order" type="xsd:token">
579586
<xsd:annotation>
580587
<xsd:documentation><![CDATA[

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ public void simpleBroker() {
114114
(StompSubProtocolHandler) subProtocolWsHandler.getProtocolHandlerMap().get("v12.stomp");
115115
assertNotNull(stompHandler);
116116

117+
int messageBufferSizeLimit = (int)new DirectFieldAccessor(stompHandler).getPropertyValue("messageBufferSizeLimit");
118+
assertEquals(123, messageBufferSizeLimit);
119+
117120
httpRequestHandler = (HttpRequestHandler) suhm.getUrlMap().get("/test/**");
118121
assertNotNull(httpRequestHandler);
119122
assertThat(httpRequestHandler, Matchers.instanceOf(SockJsHttpRequestHandler.class));

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -22,8 +22,8 @@
2222
import org.junit.Test;
2323
import org.mockito.Mockito;
2424

25-
import org.springframework.messaging.MessageChannel;
2625
import org.springframework.messaging.SubscribableChannel;
26+
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
2727
import org.springframework.messaging.simp.user.DefaultUserSessionRegistry;
2828
import org.springframework.messaging.simp.user.UserSessionRegistry;
2929
import org.springframework.scheduling.TaskScheduler;
@@ -47,15 +47,19 @@ public class WebMvcStompEndpointRegistryTests {
4747

4848
private UserSessionRegistry userSessionRegistry;
4949

50+
private MessageBrokerRegistry messageBrokerRegistry;
51+
5052

5153
@Before
5254
public void setup() {
53-
MessageChannel inChannel = Mockito.mock(MessageChannel.class);
55+
SubscribableChannel inChannel = Mockito.mock(SubscribableChannel.class);
5456
SubscribableChannel outChannel = Mockito.mock(SubscribableChannel.class);
5557
this.webSocketHandler = new SubProtocolWebSocketHandler(inChannel, outChannel);
5658
this.userSessionRegistry = new DefaultUserSessionRegistry();
59+
this.messageBrokerRegistry = new MessageBrokerRegistry(inChannel, outChannel);
5760
TaskScheduler taskScheduler = Mockito.mock(TaskScheduler.class);
58-
this.registry = new WebMvcStompEndpointRegistry(webSocketHandler, userSessionRegistry, taskScheduler);
61+
this.registry = new WebMvcStompEndpointRegistry(webSocketHandler, userSessionRegistry,
62+
taskScheduler, messageBrokerRegistry);
5963
}
6064

6165

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
import org.junit.Before;
2525
import org.junit.Test;
2626

27+
import org.springframework.beans.DirectFieldAccessor;
2728
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2829
import org.springframework.context.annotation.Bean;
2930
import org.springframework.context.annotation.Configuration;
3031
import org.springframework.messaging.Message;
3132
import org.springframework.messaging.MessageHandler;
33+
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
3234
import org.springframework.messaging.support.AbstractSubscribableChannel;
3335
import org.springframework.messaging.support.ExecutorSubscribableChannel;
3436
import org.springframework.messaging.handler.annotation.MessageMapping;
@@ -43,7 +45,9 @@
4345
import org.springframework.web.socket.TextMessage;
4446
import org.springframework.web.socket.WebSocketSession;
4547
import org.springframework.web.socket.handler.TestWebSocketSession;
48+
import org.springframework.web.socket.messaging.StompSubProtocolHandler;
4649
import org.springframework.web.socket.messaging.StompTextMessageBuilder;
50+
import org.springframework.web.socket.messaging.SubProtocolHandler;
4751
import org.springframework.web.socket.messaging.SubProtocolWebSocketHandler;
4852

4953
import static org.junit.Assert.*;
@@ -104,6 +108,18 @@ public void clientOutboundChannelChannel() {
104108
assertTrue(handlers.iterator().next() instanceof SubProtocolWebSocketHandler);
105109
}
106110

111+
@Test
112+
public void maxFrameBufferSize() {
113+
SubProtocolWebSocketHandler subProtocolWebSocketHandler = this.config.getBean("subProtocolWebSocketHandler", SubProtocolWebSocketHandler.class);
114+
115+
List<SubProtocolHandler> protocolHandlers = subProtocolWebSocketHandler.getProtocolHandlers();
116+
for(SubProtocolHandler protocolHandler : protocolHandlers) {
117+
assertTrue(protocolHandler instanceof StompSubProtocolHandler);
118+
DirectFieldAccessor protocolHandlerFieldAccessor = new DirectFieldAccessor(protocolHandler);
119+
assertEquals(123, protocolHandlerFieldAccessor.getPropertyValue("messageBufferSizeLimit"));
120+
}
121+
}
122+
107123

108124
@Controller
109125
static class TestController {
@@ -133,6 +149,11 @@ public void registerStompEndpoints(StompEndpointRegistry registry) {
133149
registry.addEndpoint("/simpleBroker");
134150
}
135151

152+
@Override
153+
public void configureMessageBroker(MessageBrokerRegistry registry) {
154+
registry.setMessageBufferSizeLimit(123);
155+
}
156+
136157
}
137158

138159
@Configuration

0 commit comments

Comments
 (0)