Ver Fonte

Update NotificationControllerV2.java (#3145)

1. if `filteredNotifications` is not empty, then  `namespaces` will not empty。 so check  `filteredNotifications` before  `namespaces ` Initialized , will avoid create useless object

2. constructor (for set and map ): choose more appropriate initialCapacity
Joe há 4 anos atrás
pai
commit
df6cb3966c

+ 9 - 8
apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/controller/NotificationControllerV2.java

@@ -109,12 +109,17 @@ public class NotificationControllerV2 implements ReleaseMessageListener {
     if (CollectionUtils.isEmpty(notifications)) {
       throw new BadRequestException("Invalid format of notifications: " + notificationsAsString);
     }
-
-    DeferredResultWrapper deferredResultWrapper = new DeferredResultWrapper(bizConfig.longPollingTimeoutInMilli());
-    Set<String> namespaces = Sets.newHashSet();
-    Map<String, Long> clientSideNotifications = Maps.newHashMap();
+    
     Map<String, ApolloConfigNotification> filteredNotifications = filterNotifications(appId, notifications);
 
+    if (CollectionUtils.isEmpty(filteredNotifications)) {
+      throw new BadRequestException("Invalid format of notifications: " + notificationsAsString);
+    }
+    
+    DeferredResultWrapper deferredResultWrapper = new DeferredResultWrapper(bizConfig.longPollingTimeoutInMilli());
+    Set<String> namespaces = Sets.newHashSetWithExpectedSize(filteredNotifications.size());
+    Map<String, Long> clientSideNotifications = Maps.newHashMapWithExpectedSize(filteredNotifications.size());
+    
     for (Map.Entry<String, ApolloConfigNotification> notificationEntry : filteredNotifications.entrySet()) {
       String normalizedNamespace = notificationEntry.getKey();
       ApolloConfigNotification notification = notificationEntry.getValue();
@@ -125,10 +130,6 @@ public class NotificationControllerV2 implements ReleaseMessageListener {
       }
     }
 
-    if (CollectionUtils.isEmpty(namespaces)) {
-      throw new BadRequestException("Invalid format of notifications: " + notificationsAsString);
-    }
-
     Multimap<String, String> watchedKeysMap =
         watchKeysUtil.assembleAllWatchKeys(appId, cluster, namespaces, dataCenter);