Add singleSession attribute to @SendToUser

Added the ability to send a message only to one user session. Given a
user has two tabs open and the client sends a message to the server
from tab 1, it is now possible to send a reply message to only 1 tab
instead of the default mode of targetting all known user sessions.

Issue: SPR-11506
This commit is contained in:
Mark Galea
2014-03-14 21:55:03 +01:00
committed by Rossen Stoyanchev
parent a653c06711
commit 088b80f4c5
5 changed files with 82 additions and 3 deletions

View File

@@ -48,4 +48,11 @@ public @interface SendToUser {
*/
String[] value() default {};
/**
* A flag indicating whether the message is to be sent to a particular user session.
*
*/
boolean singleSession() default false;
}

View File

@@ -148,7 +148,12 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
String user = getUserName(message, headers);
String[] destinations = getTargetDestinations(sendToUser, message, this.defaultUserDestinationPrefix);
for (String destination : destinations) {
this.messagingTemplate.convertAndSendToUser(user, destination, returnValue, createHeaders(sessionId));
if (sendToUser.singleSession()) {
this.messagingTemplate.convertAndSendToUser(userName, destination, returnValue, createHeaders(sessionId));
}
else }
this.messagingTemplate.convertAndSendToUser(userName, destination, returnValue);
}
}
return;
}

View File

@@ -158,7 +158,12 @@ public class DefaultUserDestinationResolver implements UserDestinationResolver {
subscribeDestination = this.destinationPrefix.substring(0, startIndex-1) + destinationWithoutPrefix;
user = destination.substring(startIndex, endIndex);
user = StringUtils.replace(user, "%2F", "/");
sessionIds = this.userSessionRegistry.getSessionIds(user);
if (headers.getSessionId() == null){
sessionIds = this.userSessionRegistry.getSessionIds(user);
} else {
sessionIds = Collections.singleton(headers.getSessionId());
}
}
else {
if (logger.isTraceEnabled()) {