Use Map.forEach instead of manual Map.Entry iteration wherever possible
Issue: SPR-16646
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -168,16 +168,16 @@ public class SimpAttributes {
|
||||
}
|
||||
|
||||
private void executeDestructionCallbacks() {
|
||||
for (Map.Entry<String, Object> entry : this.attributes.entrySet()) {
|
||||
if (entry.getKey().startsWith(DESTRUCTION_CALLBACK_NAME_PREFIX)) {
|
||||
this.attributes.forEach((key, value) -> {
|
||||
if (key.startsWith(DESTRUCTION_CALLBACK_NAME_PREFIX)) {
|
||||
try {
|
||||
((Runnable) entry.getValue()).run();
|
||||
((Runnable) value).run();
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
logger.error("Uncaught error in session attribute destruction callback", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -263,10 +263,7 @@ public class SimpMessagingTemplate extends AbstractMessageSendingTemplate<String
|
||||
|
||||
SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
|
||||
initHeaders(headerAccessor);
|
||||
for (Map.Entry<String, Object> headerEntry : headers.entrySet()) {
|
||||
Object value = headerEntry.getValue();
|
||||
headerAccessor.setNativeHeader(headerEntry.getKey(), (value != null ? value.toString() : null));
|
||||
}
|
||||
headers.forEach((key, value) -> headerAccessor.setNativeHeader(key, (value != null ? value.toString() : null)));
|
||||
return headerAccessor.getMessageHeaders();
|
||||
}
|
||||
|
||||
|
||||
@@ -270,8 +270,8 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
|
||||
for (SessionSubscriptionInfo info : subscriptionRegistry.getAllSubscriptions()) {
|
||||
for (String destinationPattern : info.getDestinations()) {
|
||||
if (getPathMatcher().match(destinationPattern, destination)) {
|
||||
for (Subscription subscription : info.getSubscriptions(destinationPattern)) {
|
||||
result.add(info.sessionId, subscription.getId());
|
||||
for (Subscription sub : info.getSubscriptions(destinationPattern)) {
|
||||
result.add(info.sessionId, sub.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -287,27 +287,23 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
|
||||
|
||||
public void updateAfterNewSubscription(String destination, String sessionId, String subsId) {
|
||||
synchronized (this.updateCache) {
|
||||
for (Map.Entry<String, LinkedMultiValueMap<String, String>> entry : this.updateCache.entrySet()) {
|
||||
String cachedDestination = entry.getKey();
|
||||
this.updateCache.forEach((cachedDestination, subscriptions) -> {
|
||||
if (getPathMatcher().match(destination, cachedDestination)) {
|
||||
LinkedMultiValueMap<String, String> subs = entry.getValue();
|
||||
// Subscription id's may also be populated via getSubscriptions()
|
||||
List<String> subsForSession = subs.get(sessionId);
|
||||
List<String> subsForSession = subscriptions.get(sessionId);
|
||||
if (subsForSession == null || !subsForSession.contains(subsId)) {
|
||||
subs.add(sessionId, subsId);
|
||||
this.accessCache.put(cachedDestination, subs.deepCopy());
|
||||
subscriptions.add(sessionId, subsId);
|
||||
this.accessCache.put(cachedDestination, subscriptions.deepCopy());
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void updateAfterRemovedSubscription(String sessionId, String subsId) {
|
||||
synchronized (this.updateCache) {
|
||||
Set<String> destinationsToRemove = new HashSet<>();
|
||||
for (Map.Entry<String, LinkedMultiValueMap<String, String>> entry : this.updateCache.entrySet()) {
|
||||
String destination = entry.getKey();
|
||||
LinkedMultiValueMap<String, String> sessionMap = entry.getValue();
|
||||
this.updateCache.forEach((destination, sessionMap) -> {
|
||||
List<String> subscriptions = sessionMap.get(sessionId);
|
||||
if (subscriptions != null) {
|
||||
subscriptions.remove(subsId);
|
||||
@@ -321,7 +317,7 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
|
||||
this.accessCache.put(destination, sessionMap.deepCopy());
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
for (String destination : destinationsToRemove) {
|
||||
this.updateCache.remove(destination);
|
||||
this.accessCache.remove(destination);
|
||||
@@ -332,9 +328,7 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
|
||||
public void updateAfterRemovedSession(SessionSubscriptionInfo info) {
|
||||
synchronized (this.updateCache) {
|
||||
Set<String> destinationsToRemove = new HashSet<>();
|
||||
for (Map.Entry<String, LinkedMultiValueMap<String, String>> entry : this.updateCache.entrySet()) {
|
||||
String destination = entry.getKey();
|
||||
LinkedMultiValueMap<String, String> sessionMap = entry.getValue();
|
||||
this.updateCache.forEach((destination, sessionMap) -> {
|
||||
if (sessionMap.remove(info.getSessionId()) != null) {
|
||||
if (sessionMap.isEmpty()) {
|
||||
destinationsToRemove.add(destination);
|
||||
@@ -343,7 +337,7 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
|
||||
this.accessCache.put(destination, sessionMap.deepCopy());
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
for (String destination : destinationsToRemove) {
|
||||
this.updateCache.remove(destination);
|
||||
this.accessCache.remove(destination);
|
||||
@@ -433,13 +427,9 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
|
||||
public Subscription getSubscription(String subscriptionId) {
|
||||
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)) {
|
||||
return sub;
|
||||
}
|
||||
for (Subscription sub : destinationEntry.getValue()) {
|
||||
if (sub.getId().equalsIgnoreCase(subscriptionId)) {
|
||||
return sub;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,7 +19,6 @@ 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;
|
||||
@@ -352,11 +351,11 @@ public class SimpleBrokerMessageHandler extends AbstractBrokerMessageHandler {
|
||||
logger.debug("Broadcasting to " + subscriptions.size() + " sessions.");
|
||||
}
|
||||
long now = System.currentTimeMillis();
|
||||
for (Map.Entry<String, List<String>> subscriptionEntry : subscriptions.entrySet()) {
|
||||
for (String subscriptionId : subscriptionEntry.getValue()) {
|
||||
subscriptions.forEach((sessionId, subscriptionIds) -> {
|
||||
for (String subscriptionId : subscriptionIds) {
|
||||
SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
|
||||
initHeaders(headerAccessor);
|
||||
headerAccessor.setSessionId(subscriptionEntry.getKey());
|
||||
headerAccessor.setSessionId(sessionId);
|
||||
headerAccessor.setSubscriptionId(subscriptionId);
|
||||
headerAccessor.copyHeadersIfAbsent(message.getHeaders());
|
||||
Object payload = message.getPayload();
|
||||
@@ -370,13 +369,13 @@ public class SimpleBrokerMessageHandler extends AbstractBrokerMessageHandler {
|
||||
}
|
||||
}
|
||||
finally {
|
||||
SessionInfo info = this.sessions.get(subscriptionEntry.getKey());
|
||||
SessionInfo info = this.sessions.get(sessionId);
|
||||
if (info != null) {
|
||||
info.setLastWriteTime(now);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -45,11 +45,11 @@ public interface SubscriptionRegistry {
|
||||
void unregisterAllSubscriptions(String sessionId);
|
||||
|
||||
/**
|
||||
* Find all subscriptions that should receive the given message. The map
|
||||
* returned is safe to iterate and will never be modified.
|
||||
* Find all subscriptions that should receive the given message.
|
||||
* The map returned is safe to iterate and will never be modified.
|
||||
* @param message the message
|
||||
* @return a {@code MultiValueMap} with sessionId-subscriptionId pairs,
|
||||
* possibly empty.
|
||||
* @return a {@code MultiValueMap} with sessionId-subscriptionId pairs
|
||||
* (possibly empty)
|
||||
*/
|
||||
MultiValueMap<String, String> findSubscriptions(Message<?> message);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user