Skip to content

Commit 9eb39e1

Browse files
committed
Polishing
See gh-21798
1 parent a205eab commit 9eb39e1

File tree

6 files changed

+68
-49
lines changed

6 files changed

+68
-49
lines changed

spring-messaging/src/main/java/org/springframework/messaging/simp/SimpMessageHeaderAccessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -273,7 +273,7 @@ public static SimpMessageHeaderAccessor create(SimpMessageType messageType) {
273273
}
274274

275275
/**
276-
* Create an instance from the payload and headers of the given Message.
276+
* Create an instance by copying the headers of a Message.
277277
*/
278278
public static SimpMessageHeaderAccessor wrap(Message<?> message) {
279279
return new SimpMessageHeaderAccessor(message);

spring-messaging/src/main/java/org/springframework/messaging/simp/user/DefaultUserDestinationResolver.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -130,18 +130,17 @@ public UserDestinationResult resolveDestination(Message<?> message) {
130130
return null;
131131
}
132132
String user = parseResult.getUser();
133-
String sourceDestination = parseResult.getSourceDestination();
133+
String sourceDest = parseResult.getSourceDestination();
134134
Set<String> targetSet = new HashSet<>();
135135
for (String sessionId : parseResult.getSessionIds()) {
136-
String actualDestination = parseResult.getActualDestination();
137-
String targetDestination = getTargetDestination(
138-
sourceDestination, actualDestination, sessionId, user);
139-
if (targetDestination != null) {
140-
targetSet.add(targetDestination);
136+
String actualDest = parseResult.getActualDestination();
137+
String targetDest = getTargetDestination(sourceDest, actualDest, sessionId, user);
138+
if (targetDest != null) {
139+
targetSet.add(targetDest);
141140
}
142141
}
143-
String subscribeDestination = parseResult.getSubscribeDestination();
144-
return new UserDestinationResult(sourceDestination, targetSet, subscribeDestination, user);
142+
String subscribeDest = parseResult.getSubscribeDestination();
143+
return new UserDestinationResult(sourceDest, targetSet, subscribeDest, user);
145144
}
146145

147146
@Nullable
@@ -283,22 +282,37 @@ public ParseResult(String sourceDest, String actualDest, String subscribeDest,
283282
this.user = user;
284283
}
285284

285+
/**
286+
* The destination from the source message, e.g. "/user/{user}/queue/position-updates".
287+
*/
286288
public String getSourceDestination() {
287289
return this.sourceDestination;
288290
}
289291

292+
/**
293+
* The actual destination, without any user prefix, e.g. "/queue/position-updates".
294+
*/
290295
public String getActualDestination() {
291296
return this.actualDestination;
292297
}
293298

299+
/**
300+
* The user destination as it would be on a subscription, "/user/queue/position-updates".
301+
*/
294302
public String getSubscribeDestination() {
295303
return this.subscribeDestination;
296304
}
297305

306+
/**
307+
* The session id or id's for the user.
308+
*/
298309
public Set<String> getSessionIds() {
299310
return this.sessionIds;
300311
}
301312

313+
/**
314+
* The name of the user associated with the session.
315+
*/
302316
@Nullable
303317
public String getUser() {
304318
return this.user;

spring-messaging/src/main/java/org/springframework/messaging/simp/user/UserDestinationMessageHandler.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@
4343
/**
4444
* {@code MessageHandler} with support for "user" destinations.
4545
*
46-
* <p>Listens for messages with "user" destinations, translates their destination
47-
* to actual target destinations unique to the active session(s) of a user, and
48-
* then sends the resolved messages to the broker channel to be delivered.
46+
* <p>Listen for messages with "user" destinations, translate the destination to
47+
* a target destination that's unique to the active user session(s), and send
48+
* to the broker channel for delivery.
4949
*
5050
* @author Rossen Stoyanchev
5151
* @since 4.0
@@ -75,24 +75,24 @@ public class UserDestinationMessageHandler implements MessageHandler, SmartLifec
7575

7676

7777
/**
78-
* Create an instance with the given client and broker channels subscribing
79-
* to handle messages from each and then sending any resolved messages to the
80-
* broker channel.
78+
* Create an instance with the given client and broker channels to subscribe to,
79+
* and then send resolved messages to the broker channel.
8180
* @param clientInboundChannel messages received from clients.
8281
* @param brokerChannel messages sent to the broker.
83-
* @param resolver the resolver for "user" destinations.
82+
* @param destinationResolver the resolver for "user" destinations.
8483
*/
85-
public UserDestinationMessageHandler(SubscribableChannel clientInboundChannel,
86-
SubscribableChannel brokerChannel, UserDestinationResolver resolver) {
84+
public UserDestinationMessageHandler(
85+
SubscribableChannel clientInboundChannel, SubscribableChannel brokerChannel,
86+
UserDestinationResolver destinationResolver) {
8787

8888
Assert.notNull(clientInboundChannel, "'clientInChannel' must not be null");
8989
Assert.notNull(brokerChannel, "'brokerChannel' must not be null");
90-
Assert.notNull(resolver, "resolver must not be null");
90+
Assert.notNull(destinationResolver, "resolver must not be null");
9191

9292
this.clientInboundChannel = clientInboundChannel;
9393
this.brokerChannel = brokerChannel;
9494
this.messagingTemplate = new SimpMessagingTemplate(brokerChannel);
95-
this.destinationResolver = resolver;
95+
this.destinationResolver = destinationResolver;
9696
}
9797

9898

@@ -182,16 +182,16 @@ public final boolean isRunning() {
182182

183183

184184
@Override
185-
public void handleMessage(Message<?> message) throws MessagingException {
186-
Message<?> messageToUse = message;
185+
public void handleMessage(Message<?> sourceMessage) throws MessagingException {
186+
Message<?> message = sourceMessage;
187187
if (this.broadcastHandler != null) {
188-
messageToUse = this.broadcastHandler.preHandle(message);
189-
if (messageToUse == null) {
188+
message = this.broadcastHandler.preHandle(sourceMessage);
189+
if (message == null) {
190190
return;
191191
}
192192
}
193193

194-
UserDestinationResult result = this.destinationResolver.resolveDestination(messageToUse);
194+
UserDestinationResult result = this.destinationResolver.resolveDestination(message);
195195
if (result == null) {
196196
return;
197197
}
@@ -201,22 +201,22 @@ public void handleMessage(Message<?> message) throws MessagingException {
201201
logger.trace("No active sessions for user destination: " + result.getSourceDestination());
202202
}
203203
if (this.broadcastHandler != null) {
204-
this.broadcastHandler.handleUnresolved(messageToUse);
204+
this.broadcastHandler.handleUnresolved(message);
205205
}
206206
return;
207207
}
208208

209-
SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(messageToUse);
209+
SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(message);
210210
initHeaders(accessor);
211211
accessor.setNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION, result.getSubscribeDestination());
212212
accessor.setLeaveMutable(true);
213213

214-
messageToUse = MessageBuilder.createMessage(messageToUse.getPayload(), accessor.getMessageHeaders());
214+
message = MessageBuilder.createMessage(message.getPayload(), accessor.getMessageHeaders());
215215
if (logger.isTraceEnabled()) {
216216
logger.trace("Translated " + result.getSourceDestination() + " -> " + result.getTargetDestinations());
217217
}
218218
for (String target : result.getTargetDestinations()) {
219-
this.messagingTemplate.send(target, messageToUse);
219+
this.messagingTemplate.send(target, message);
220220
}
221221
}
222222

spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandlerTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ class StompBrokerRelayMessageHandlerTests {
5454

5555
private StompBrokerRelayMessageHandler brokerRelay;
5656

57-
private StubMessageChannel outboundChannel = new StubMessageChannel();
57+
private final StubMessageChannel outboundChannel = new StubMessageChannel();
5858

59-
private StubTcpOperations tcpClient = new StubTcpOperations();
59+
private final StubTcpOperations tcpClient = new StubTcpOperations();
6060

61-
private ArgumentCaptor<Runnable> messageCountTaskCaptor = ArgumentCaptor.forClass(Runnable.class);
61+
private final ArgumentCaptor<Runnable> messageCountTaskCaptor = ArgumentCaptor.forClass(Runnable.class);
6262

6363

6464
@BeforeEach

spring-messaging/src/test/java/org/springframework/messaging/simp/user/UserDestinationMessageHandlerTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.mockito.Mockito;
2424

2525
import org.springframework.core.testfixture.security.TestPrincipal;
26+
import org.springframework.lang.Nullable;
2627
import org.springframework.messaging.Message;
2728
import org.springframework.messaging.StubMessageChannel;
2829
import org.springframework.messaging.SubscribableChannel;
@@ -50,7 +51,8 @@ class UserDestinationMessageHandlerTests {
5051

5152
private final SubscribableChannel brokerChannel = mock();
5253

53-
private final UserDestinationMessageHandler handler = new UserDestinationMessageHandler(new StubMessageChannel(), this.brokerChannel, new DefaultUserDestinationResolver(this.registry));
54+
private final UserDestinationMessageHandler handler = new UserDestinationMessageHandler(
55+
new StubMessageChannel(), this.brokerChannel, new DefaultUserDestinationResolver(this.registry));
5456

5557

5658
@Test
@@ -184,7 +186,9 @@ void ignoreMessage() {
184186
}
185187

186188

187-
private Message<?> createWith(SimpMessageType type, String user, String sessionId, String destination) {
189+
private Message<?> createWith(
190+
SimpMessageType type, @Nullable String user, @Nullable String sessionId, @Nullable String destination) {
191+
188192
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(type);
189193
if (destination != null) {
190194
headers.setDestination(destination);

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,8 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE
108108
@Nullable
109109
private MessageHeaderInitializer headerInitializer;
110110

111-
private boolean preserveReceiveOrder;
112-
113-
private final Map<String, MessageChannel> messageChannels = new ConcurrentHashMap<>();
111+
@Nullable
112+
private Map<String, MessageChannel> orderedHandlingMessageChannels;
114113

115114
private final Map<String, Principal> stompAuthentications = new ConcurrentHashMap<>();
116115

@@ -209,7 +208,7 @@ public MessageHeaderInitializer getHeaderInitializer() {
209208
* @since 6.1
210209
*/
211210
public void setPreserveReceiveOrder(boolean preserveReceiveOrder) {
212-
this.preserveReceiveOrder = preserveReceiveOrder;
211+
this.orderedHandlingMessageChannels = (preserveReceiveOrder ? new ConcurrentHashMap<>() : null);
213212
}
214213

215214
/**
@@ -218,7 +217,7 @@ public void setPreserveReceiveOrder(boolean preserveReceiveOrder) {
218217
* @since 6.1
219218
*/
220219
public boolean isPreserveReceiveOrder() {
221-
return this.preserveReceiveOrder;
220+
return (this.orderedHandlingMessageChannels != null);
222221
}
223222

224223
@Override
@@ -253,7 +252,7 @@ public Stats getStats() {
253252
*/
254253
@Override
255254
public void handleMessageFromClient(WebSocketSession session,
256-
WebSocketMessage<?> webSocketMessage, MessageChannel outputChannel) {
255+
WebSocketMessage<?> webSocketMessage, MessageChannel targetChannel) {
257256

258257
List<Message<byte[]>> messages;
259258
try {
@@ -296,11 +295,11 @@ else if (webSocketMessage instanceof BinaryMessage binaryMessage) {
296295
return;
297296
}
298297

299-
MessageChannel channelToUse =
300-
(this.messageChannels.computeIfAbsent(session.getId(),
301-
id -> this.preserveReceiveOrder ?
302-
new OrderedMessageChannelDecorator(outputChannel, logger) :
303-
outputChannel));
298+
MessageChannel channelToUse = targetChannel;
299+
if (this.orderedHandlingMessageChannels != null) {
300+
channelToUse = this.orderedHandlingMessageChannels.computeIfAbsent(
301+
session.getId(), id -> new OrderedMessageChannelDecorator(targetChannel, logger));
302+
}
304303

305304
for (Message<byte[]> message : messages) {
306305
StompHeaderAccessor headerAccessor =
@@ -324,7 +323,7 @@ else if (webSocketMessage instanceof BinaryMessage binaryMessage) {
324323
});
325324
}
326325
headerAccessor.setHeader(SimpMessageHeaderAccessor.HEART_BEAT_HEADER, headerAccessor.getHeartbeat());
327-
if (!detectImmutableMessageInterceptor(outputChannel)) {
326+
if (!detectImmutableMessageInterceptor(targetChannel)) {
328327
headerAccessor.setImmutable();
329328
}
330329

@@ -686,7 +685,9 @@ public void afterSessionEnded(WebSocketSession session, CloseStatus closeStatus,
686685
outputChannel.send(message);
687686
}
688687
finally {
689-
this.messageChannels.remove(session.getId());
688+
if (this.orderedHandlingMessageChannels != null) {
689+
this.orderedHandlingMessageChannels.remove(session.getId());
690+
}
690691
this.stompAuthentications.remove(session.getId());
691692
SimpAttributesContextHolder.resetAttributes();
692693
simpAttributes.sessionCompleted();

0 commit comments

Comments
 (0)