Browse Source

fix inefficient use of keySet operators in messaging classes

issue: SPR-15553
pull/1394/head
Alexander Riss 8 years ago
parent
commit
e49a5d8424
  1. 6
      spring-messaging/src/main/java/org/springframework/messaging/simp/SimpMessagingTemplate.java
  2. 12
      spring-messaging/src/main/java/org/springframework/messaging/simp/broker/DefaultSubscriptionRegistry.java
  3. 9
      spring-messaging/src/main/java/org/springframework/messaging/simp/broker/SimpleBrokerMessageHandler.java
  4. 6
      spring-messaging/src/main/java/org/springframework/messaging/support/NativeMessageHeaderAccessor.java

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

@ -258,9 +258,9 @@ public class SimpMessagingTemplate extends AbstractMessageSendingTemplate<String @@ -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();
}

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

@ -425,8 +425,8 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry { @@ -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 { @@ -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();
}
}
}

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

@ -19,6 +19,7 @@ package org.springframework.messaging.simp.broker; @@ -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 { @@ -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 { @@ -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);
}

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

@ -191,9 +191,9 @@ public class NativeMessageHeaderAccessor extends MessageHeaderAccessor { @@ -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…
Cancel
Save