You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2018/11/29 05:05:58 UTC

[incubator-dubbo] 02/02: fix NPE when no listeners registered for key

This is an automated email from the ASF dual-hosted git repository.

liujun pushed a commit to branch dev-metadata
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git

commit 23f0e52486e21ff8af15820fb9274ce93570b86c
Author: ken.lj <ke...@gmail.com>
AuthorDate: Thu Nov 29 13:05:21 2018 +0800

    fix NPE when no listeners registered for key
---
 .../dubbo/configcenter/support/zookeeper/CacheListener.java    | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/CacheListener.java b/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/CacheListener.java
index b91e76e..45111fe 100644
--- a/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/CacheListener.java
+++ b/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/CacheListener.java
@@ -20,6 +20,7 @@ import org.apache.curator.framework.CuratorFramework;
 import org.apache.curator.framework.recipes.cache.ChildData;
 import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
 import org.apache.curator.framework.recipes.cache.TreeCacheListener;
+import org.apache.dubbo.common.utils.CollectionUtils;
 import org.apache.dubbo.common.utils.StringUtils;
 import org.apache.dubbo.configcenter.ConfigChangeEvent;
 import org.apache.dubbo.configcenter.ConfigChangeType;
@@ -36,7 +37,7 @@ import java.util.concurrent.CountDownLatch;
  *
  */
 public class CacheListener implements TreeCacheListener {
-    private Map<String, Set<ConfigurationListener>> listeners = new ConcurrentHashMap<>();
+    private Map<String, Set<ConfigurationListener>> keyListeners = new ConcurrentHashMap<>();
     private CountDownLatch initializedLatch;
     private String rootPath;
 
@@ -82,12 +83,15 @@ public class CacheListener implements TreeCacheListener {
             }
 
             ConfigChangeEvent configChangeEvent = new ConfigChangeEvent(key, new String(value, StandardCharsets.UTF_8), changeType);
-            listeners.get(key).forEach(listener -> listener.process(configChangeEvent));
+            Set<ConfigurationListener> listeners = keyListeners.get(key);
+            if (CollectionUtils.isNotEmpty(listeners)) {
+                listeners.forEach(listener -> listener.process(configChangeEvent));
+            }
         }
     }
 
     public void addListener(String key, ConfigurationListener configurationListener) {
-        Set<ConfigurationListener> set = this.listeners.computeIfAbsent(key, k -> new CopyOnWriteArraySet<>());
+        Set<ConfigurationListener> set = this.keyListeners.computeIfAbsent(key, k -> new CopyOnWriteArraySet<>());
         set.add(configurationListener);
     }