Browse Source

Fix synchronization issue in StompSubProtocolHandler

Two concurrent threads should not send a message on a single WebSocket
session at the same time, for example see:
http://docs.oracle.com/javaee/7/api/javax/websocket/RemoteEndpoint.Basic.html

In StompSubProtocolHandler it is quite possible that multiple messages
may be broadcast to a single WebSocket client concurrently.

This change adds synchronization around the sending of a message to a
specific cilent session.

Issue: SPR-11023
pull/422/head
Rossen Stoyanchev 11 years ago
parent
commit
e764c8d897
  1. 6
      spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java

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

@ -174,7 +174,11 @@ public class StompSubProtocolHandler implements SubProtocolHandler { @@ -174,7 +174,11 @@ public class StompSubProtocolHandler implements SubProtocolHandler {
try {
message = MessageBuilder.withPayload(message.getPayload()).setHeaders(headers).build();
byte[] bytes = this.stompEncoder.encode((Message<byte[]>) message);
session.sendMessage(new TextMessage(new String(bytes, Charset.forName("UTF-8"))));
synchronized(session) {
session.sendMessage(new TextMessage(new String(bytes, Charset.forName("UTF-8"))));
}
}
catch (Throwable t) {
sendErrorMessage(session, t);

Loading…
Cancel
Save