diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java index d057de6ee7a..dcbc8b837c1 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java @@ -228,24 +228,26 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE headerAccessor.setUser(session.getPrincipal()); headerAccessor.setImmutable(); - if (this.eventPublisher != null) { - if (StompCommand.CONNECT.equals(headerAccessor.getCommand())) { - this.stats.incrementConnectCount(); - publishEvent(new SessionConnectEvent(this, message)); - } - else if (StompCommand.DISCONNECT.equals(headerAccessor.getCommand())) { - this.stats.incrementDisconnectCount(); - } - else if (StompCommand.SUBSCRIBE.equals(headerAccessor.getCommand())) { - publishEvent(new SessionSubscribeEvent(this, message)); - } - else if (StompCommand.UNSUBSCRIBE.equals(headerAccessor.getCommand())) { - publishEvent(new SessionUnsubscribeEvent(this, message)); - } + if (StompCommand.CONNECT.equals(headerAccessor.getCommand())) { + this.stats.incrementConnectCount(); + } + else if (StompCommand.DISCONNECT.equals(headerAccessor.getCommand())) { + this.stats.incrementDisconnectCount(); } try { SimpAttributesContextHolder.setAttributesFromMessage(message); + if (this.eventPublisher != null) { + if (StompCommand.CONNECT.equals(headerAccessor.getCommand())) { + publishEvent(new SessionConnectEvent(this, message)); + } + else if (StompCommand.SUBSCRIBE.equals(headerAccessor.getCommand())) { + publishEvent(new SessionSubscribeEvent(this, message)); + } + else if (StompCommand.UNSUBSCRIBE.equals(headerAccessor.getCommand())) { + publishEvent(new SessionUnsubscribeEvent(this, message)); + } + } outputChannel.send(message); } finally { @@ -309,7 +311,14 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE this.stats.incrementConnectedCount(); stompAccessor = afterStompSessionConnected(message, stompAccessor, session); if (this.eventPublisher != null && StompCommand.CONNECTED.equals(command)) { - publishEvent(new SessionConnectedEvent(this, (Message) message)); + try { + SimpAttributes simpAttributes = new SimpAttributes(session.getId(), session.getAttributes()); + SimpAttributesContextHolder.setAttributes(simpAttributes); + publishEvent(new SessionConnectedEvent(this, (Message) message)); + } + finally { + SimpAttributesContextHolder.resetAttributes(); + } } } try { @@ -448,12 +457,12 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE this.userSessionRegistry.unregisterSessionId(userName, session.getId()); } Message message = createDisconnectMessage(session); - if (this.eventPublisher != null) { - publishEvent(new SessionDisconnectEvent(this, message, session.getId(), closeStatus)); - } SimpAttributes simpAttributes = SimpAttributes.fromMessage(message); try { SimpAttributesContextHolder.setAttributes(simpAttributes); + if (this.eventPublisher != null) { + publishEvent(new SessionDisconnectEvent(this, message, session.getId(), closeStatus)); + } outputChannel.send(message); } finally {