Skip to content

Commit fa09cd3

Browse files
committed
Avoid inefficient keySet iteration in messaging classes
Issue: SPR-15553
1 parent be93ee7 commit fa09cd3

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,9 @@ protected Map<String, Object> processHeadersToSend(Map<String, Object> headers)
258258

259259
SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
260260
initHeaders(headerAccessor);
261-
for (String key : headers.keySet()) {
262-
Object value = headers.get(key);
263-
headerAccessor.setNativeHeader(key, (value != null ? value.toString() : null));
261+
for (Map.Entry<String, Object> headerEntry : headers.entrySet()) {
262+
Object value = headerEntry.getValue();
263+
headerAccessor.setNativeHeader(headerEntry.getKey(), (value != null ? value.toString() : null));
264264
}
265265
return headerAccessor.getMessageHeaders();
266266
}

spring-messaging/src/main/java/org/springframework/messaging/simp/broker/DefaultSubscriptionRegistry.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,8 @@ public Set<Subscription> getSubscriptions(String destination) {
431431
}
432432

433433
public Subscription getSubscription(String subscriptionId) {
434-
for (String destination : this.destinationLookup.keySet()) {
435-
Set<Subscription> subs = this.destinationLookup.get(destination);
434+
for (Map.Entry<String, Set<DefaultSubscriptionRegistry.Subscription>> destinationEntry : this.destinationLookup.entrySet()) {
435+
Set<Subscription> subs = destinationEntry.getValue();
436436
if (subs != null) {
437437
for (Subscription sub : subs) {
438438
if (sub.getId().equalsIgnoreCase(subscriptionId)) {
@@ -459,17 +459,17 @@ public void addSubscription(String destination, String subscriptionId, Expressio
459459
}
460460

461461
public String removeSubscription(String subscriptionId) {
462-
for (String destination : this.destinationLookup.keySet()) {
463-
Set<Subscription> subs = this.destinationLookup.get(destination);
462+
for (Map.Entry<String, Set<DefaultSubscriptionRegistry.Subscription>> destinationEntry : this.destinationLookup.entrySet()) {
463+
Set<Subscription> subs = destinationEntry.getValue();
464464
if (subs != null) {
465465
for (Subscription sub : subs) {
466466
if (sub.getId().equals(subscriptionId) && subs.remove(sub)) {
467467
synchronized (this.destinationLookup) {
468468
if (subs.isEmpty()) {
469-
this.destinationLookup.remove(destination);
469+
this.destinationLookup.remove(destinationEntry.getKey());
470470
}
471471
}
472-
return destination;
472+
return destinationEntry.getKey();
473473
}
474474
}
475475
}

spring-messaging/src/main/java/org/springframework/messaging/simp/broker/SimpleBrokerMessageHandler.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.security.Principal;
2020
import java.util.Arrays;
2121
import java.util.Collection;
22+
import java.util.List;
2223
import java.util.Map;
2324
import java.util.concurrent.ConcurrentHashMap;
2425
import java.util.concurrent.ScheduledFuture;
@@ -333,11 +334,11 @@ protected void sendMessageToSubscribers(String destination, Message<?> message)
333334
logger.debug("Broadcasting to " + subscriptions.size() + " sessions.");
334335
}
335336
long now = System.currentTimeMillis();
336-
for (String sessionId : subscriptions.keySet()) {
337-
for (String subscriptionId : subscriptions.get(sessionId)) {
337+
for (Map.Entry<String, List<String>> subscriptionEntry : subscriptions.entrySet()) {
338+
for (String subscriptionId : subscriptionEntry.getValue()) {
338339
SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
339340
initHeaders(headerAccessor);
340-
headerAccessor.setSessionId(sessionId);
341+
headerAccessor.setSessionId(subscriptionEntry.getKey());
341342
headerAccessor.setSubscriptionId(subscriptionId);
342343
headerAccessor.copyHeadersIfAbsent(message.getHeaders());
343344
Object payload = message.getPayload();
@@ -349,7 +350,7 @@ protected void sendMessageToSubscribers(String destination, Message<?> message)
349350
logger.error("Failed to send " + message, ex);
350351
}
351352
finally {
352-
SessionInfo info = this.sessions.get(sessionId);
353+
SessionInfo info = this.sessions.get(subscriptionEntry.getKey());
353354
if (info != null) {
354355
info.setLastWriteTime(now);
355356
}

spring-messaging/src/main/java/org/springframework/messaging/support/NativeMessageHeaderAccessor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -191,9 +191,9 @@ public void addNativeHeaders(MultiValueMap<String, String> headers) {
191191
if (headers == null) {
192192
return;
193193
}
194-
for (String header : headers.keySet()) {
195-
for (String value : headers.get(header)) {
196-
addNativeHeader(header, value);
194+
for (Map.Entry<String, List<String>> headerEntry : headers.entrySet()) {
195+
for (String value : headerEntry.getValue()) {
196+
addNativeHeader(headerEntry.getKey(), value);
197197
}
198198
}
199199
}

0 commit comments

Comments
 (0)