From a99ef6d9b2ca77f60e2838ff20a7957254a80fc4 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 18 Sep 2014 16:30:52 -0400 Subject: [PATCH] Extend websocket scope to event publication This change extends the "websocket" scope to ApplicationContext events published from StompSubProtocolHandler. This however will only work with ApplicationEventMulticaster that multicasts events in the same thread. Issue: SPR-12172 --- .../messaging/StompSubProtocolHandler.java | 45 +++++++++++-------- 1 file changed, 27 insertions(+), 18 deletions(-) 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 {