Merge pull request #1394 from alexander-riss/keySet
Fix inefficient use of keySet operators in messaging classes Issue: SPR-15553
This commit is contained in:
commit
9e3f4ccce9
|
@ -258,9 +258,9 @@ public class SimpMessagingTemplate extends AbstractMessageSendingTemplate<String
|
|||
|
||||
SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
|
||||
initHeaders(headerAccessor);
|
||||
for (String key : headers.keySet()) {
|
||||
Object value = headers.get(key);
|
||||
headerAccessor.setNativeHeader(key, (value != null ? value.toString() : null));
|
||||
for (Map.Entry<String, Object> headerEntry : headers.entrySet()) {
|
||||
Object value = headerEntry.getValue();
|
||||
headerAccessor.setNativeHeader(headerEntry.getKey(), (value != null ? value.toString() : null));
|
||||
}
|
||||
return headerAccessor.getMessageHeaders();
|
||||
}
|
||||
|
|
|
@ -425,8 +425,8 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
|
|||
}
|
||||
|
||||
public Subscription getSubscription(String subscriptionId) {
|
||||
for (String destination : this.destinationLookup.keySet()) {
|
||||
Set<Subscription> subs = this.destinationLookup.get(destination);
|
||||
for (Map.Entry<String, Set<DefaultSubscriptionRegistry.Subscription>> destinationEntry : this.destinationLookup.entrySet()) {
|
||||
Set<Subscription> subs = destinationEntry.getValue();
|
||||
if (subs != null) {
|
||||
for (Subscription sub : subs) {
|
||||
if (sub.getId().equalsIgnoreCase(subscriptionId)) {
|
||||
|
@ -453,17 +453,17 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
|
|||
}
|
||||
|
||||
public String removeSubscription(String subscriptionId) {
|
||||
for (String destination : this.destinationLookup.keySet()) {
|
||||
Set<Subscription> subs = this.destinationLookup.get(destination);
|
||||
for (Map.Entry<String, Set<DefaultSubscriptionRegistry.Subscription>> destinationEntry : this.destinationLookup.entrySet()) {
|
||||
Set<Subscription> subs = destinationEntry.getValue();
|
||||
if (subs != null) {
|
||||
for (Subscription sub : subs) {
|
||||
if (sub.getId().equals(subscriptionId) && subs.remove(sub)) {
|
||||
synchronized (this.destinationLookup) {
|
||||
if (subs.isEmpty()) {
|
||||
this.destinationLookup.remove(destination);
|
||||
this.destinationLookup.remove(destinationEntry.getKey());
|
||||
}
|
||||
}
|
||||
return destination;
|
||||
return destinationEntry.getKey();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ package org.springframework.messaging.simp.broker;
|
|||
import java.security.Principal;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
@ -333,11 +334,11 @@ public class SimpleBrokerMessageHandler extends AbstractBrokerMessageHandler {
|
|||
logger.debug("Broadcasting to " + subscriptions.size() + " sessions.");
|
||||
}
|
||||
long now = System.currentTimeMillis();
|
||||
for (String sessionId : subscriptions.keySet()) {
|
||||
for (String subscriptionId : subscriptions.get(sessionId)) {
|
||||
for (Map.Entry<String, List<String>> subscriptionEntry : subscriptions.entrySet()) {
|
||||
for (String subscriptionId : subscriptionEntry.getValue()) {
|
||||
SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
|
||||
initHeaders(headerAccessor);
|
||||
headerAccessor.setSessionId(sessionId);
|
||||
headerAccessor.setSessionId(subscriptionEntry.getKey());
|
||||
headerAccessor.setSubscriptionId(subscriptionId);
|
||||
headerAccessor.copyHeadersIfAbsent(message.getHeaders());
|
||||
Object payload = message.getPayload();
|
||||
|
@ -349,7 +350,7 @@ public class SimpleBrokerMessageHandler extends AbstractBrokerMessageHandler {
|
|||
logger.error("Failed to send " + message, ex);
|
||||
}
|
||||
finally {
|
||||
SessionInfo info = this.sessions.get(sessionId);
|
||||
SessionInfo info = this.sessions.get(subscriptionEntry.getKey());
|
||||
if (info != null) {
|
||||
info.setLastWriteTime(now);
|
||||
}
|
||||
|
|
|
@ -191,9 +191,9 @@ public class NativeMessageHeaderAccessor extends MessageHeaderAccessor {
|
|||
if (headers == null) {
|
||||
return;
|
||||
}
|
||||
for (String header : headers.keySet()) {
|
||||
for (String value : headers.get(header)) {
|
||||
addNativeHeader(header, value);
|
||||
for (Map.Entry<String, List<String>> headerEntry : headers.entrySet()) {
|
||||
for (String value : headerEntry.getValue()) {
|
||||
addNativeHeader(headerEntry.getKey(), value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue