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

albumenj pushed a commit to branch 3.1
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.1 by this push:
     new 1630c6d00c Optimization for reusing existing logic (#10508)
1630c6d00c is described below

commit 1630c6d00c6d3dae63c357c72d7d2ff790da6625
Author: 灼华 <[email protected]>
AuthorDate: Sun Aug 28 10:16:56 2022 +0800

    Optimization for reusing existing logic (#10508)
    
    * Optimization for reusing existing logic
    
    * Delele MultiInstanceActivateComparator.java
---
 .../cluster/filter/DefaultFilterChainBuilder.java  |   4 +-
 .../extension/support/ActivateComparator.java      |  32 +++-
 .../support/MultiInstanceActivateComparator.java   | 175 ---------------------
 .../dubbo/metadata/AbstractCacheManager.java       |   2 +-
 .../dubbo/registry/ListenerRegistryWrapper.java    |  90 ++++-------
 .../registry/client/AbstractServiceDiscovery.java  |  19 ++-
 .../registry/client/ServiceDiscoveryFactory.java   |   1 -
 .../registry/client/metadata/MetadataUtils.java    |   1 +
 .../registry/integration/RegistryProtocol.java     |   6 -
 .../zookeeper/util/CuratorFrameworkUtils.java      |   3 +-
 .../rpc/listener/ListenerExporterWrapper.java      |  49 +++---
 .../dubbo/rpc/listener/ListenerInvokerWrapper.java |  45 +++---
 12 files changed, 112 insertions(+), 315 deletions(-)

diff --git 
a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/filter/DefaultFilterChainBuilder.java
 
b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/filter/DefaultFilterChainBuilder.java
index 15be3004c2..22a22cd982 100644
--- 
a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/filter/DefaultFilterChainBuilder.java
+++ 
b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/filter/DefaultFilterChainBuilder.java
@@ -19,7 +19,7 @@ package org.apache.dubbo.rpc.cluster.filter;
 import org.apache.dubbo.common.URL;
 import org.apache.dubbo.common.extension.Activate;
 import org.apache.dubbo.common.extension.ExtensionDirector;
-import 
org.apache.dubbo.common.extension.support.MultiInstanceActivateComparator;
+import org.apache.dubbo.common.extension.support.ActivateComparator;
 import org.apache.dubbo.common.utils.CollectionUtils;
 import org.apache.dubbo.rpc.Filter;
 import org.apache.dubbo.rpc.Invoker;
@@ -113,7 +113,7 @@ public class DefaultFilterChainBuilder implements 
FilterChainBuilder {
     }
 
     private <T> List<T> sortingAndDeduplication(List<T> filters, 
List<ExtensionDirector> directors) {
-        Map<Class<?>, T> filtersSet = new TreeMap<>(new 
MultiInstanceActivateComparator(directors));
+        Map<Class<?>, T> filtersSet = new TreeMap<>(new 
ActivateComparator(directors));
         for (T filter : filters) {
             filtersSet.putIfAbsent(filter.getClass(), filter);
         }
diff --git 
a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/support/ActivateComparator.java
 
b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/support/ActivateComparator.java
index 8adc4412fc..d8386f116c 100644
--- 
a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/support/ActivateComparator.java
+++ 
b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/support/ActivateComparator.java
@@ -22,8 +22,10 @@ import org.apache.dubbo.common.extension.ExtensionLoader;
 import org.apache.dubbo.common.extension.SPI;
 import org.apache.dubbo.common.utils.ArrayUtils;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Comparator;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -32,11 +34,16 @@ import java.util.concurrent.ConcurrentHashMap;
  */
 public class ActivateComparator implements Comparator<Class<?>> {
 
-    private final ExtensionDirector extensionDirector;
+    private final List<ExtensionDirector> extensionDirectors;
     private final Map<Class<?>, ActivateInfo> activateInfoMap = new 
ConcurrentHashMap<>();
 
     public ActivateComparator(ExtensionDirector extensionDirector) {
-        this.extensionDirector = extensionDirector;
+        extensionDirectors = new ArrayList<>();
+        extensionDirectors.add(extensionDirector);
+    }
+
+    public ActivateComparator(List<ExtensionDirector> extensionDirectors) {
+        this.extensionDirectors = extensionDirectors;
     }
 
     @Override
@@ -60,9 +67,16 @@ public class ActivateComparator implements 
Comparator<Class<?>> {
         ActivateInfo a2 = parseActivate(o2);
 
         if ((a1.applicableToCompare() || a2.applicableToCompare()) && inf != 
null) {
-            ExtensionLoader<?> extensionLoader = 
extensionDirector.getExtensionLoader(inf);
             if (a1.applicableToCompare()) {
-                String n2 = extensionLoader.getExtensionName(o2);
+                String n2 = null;
+                for (ExtensionDirector director : extensionDirectors) {
+                    ExtensionLoader<?> extensionLoader = 
director.getExtensionLoader(inf);
+                    n2 = extensionLoader.getExtensionName(o2);
+                    if (n2 != null) {
+                        break;
+                    }
+                }
+
                 if (a1.isLess(n2)) {
                     return -1;
                 }
@@ -73,7 +87,15 @@ public class ActivateComparator implements 
Comparator<Class<?>> {
             }
 
             if (a2.applicableToCompare()) {
-                String n1 = extensionLoader.getExtensionName(o1);
+                String n1 = null;
+                for (ExtensionDirector director : extensionDirectors) {
+                    ExtensionLoader<?> extensionLoader = 
director.getExtensionLoader(inf);
+                    n1 = extensionLoader.getExtensionName(o1);
+                    if (n1 != null) {
+                        break;
+                    }
+                }
+
                 if (a2.isLess(n1)) {
                     return 1;
                 }
diff --git 
a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/support/MultiInstanceActivateComparator.java
 
b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/support/MultiInstanceActivateComparator.java
deleted file mode 100644
index 9d78914514..0000000000
--- 
a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/support/MultiInstanceActivateComparator.java
+++ /dev/null
@@ -1,175 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.dubbo.common.extension.support;
-
-import org.apache.dubbo.common.extension.Activate;
-import org.apache.dubbo.common.extension.ExtensionDirector;
-import org.apache.dubbo.common.extension.ExtensionLoader;
-import org.apache.dubbo.common.extension.SPI;
-import org.apache.dubbo.common.utils.ArrayUtils;
-
-import java.util.List;
-import java.util.Comparator;
-import java.util.Map;
-import java.util.Arrays;
-import java.util.concurrent.ConcurrentHashMap;
-
-public class MultiInstanceActivateComparator implements Comparator<Class<?>> {
-
-    private final List<ExtensionDirector> extensionDirectors;
-    private final Map<Class<?>, ActivateInfo> activateInfoMap = new 
ConcurrentHashMap<>();
-
-    public MultiInstanceActivateComparator(List<ExtensionDirector> 
extensionDirectors) {
-        this.extensionDirectors = extensionDirectors;
-    }
-
-    @Override
-    public int compare(Class o1, Class o2) {
-        if (o1 == null && o2 == null) {
-            return 0;
-        }
-        if (o1 == null) {
-            return -1;
-        }
-        if (o2 == null) {
-            return 1;
-        }
-        if (o1.equals(o2)) {
-            return 0;
-        }
-
-        Class<?> inf = findSpi(o1);
-
-        ActivateInfo a1 = parseActivate(o1);
-        ActivateInfo a2 = parseActivate(o2);
-
-        if ((a1.applicableToCompare() || a2.applicableToCompare()) && inf != 
null) {
-
-
-            if (a1.applicableToCompare()) {
-                String n2 = null;
-                for (ExtensionDirector director : extensionDirectors) {
-                    ExtensionLoader<?> extensionLoader = 
director.getExtensionLoader(inf);
-                    n2 = extensionLoader.getExtensionName(o2);
-                    if (n2 != null) {
-                        break;
-                    }
-                }
-                if (a1.isLess(n2)) {
-                    return -1;
-                }
-
-                if (a1.isMore(n2)) {
-                    return 1;
-                }
-            }
-
-            if (a2.applicableToCompare()) {
-                String n1 = null;
-                for (ExtensionDirector director : extensionDirectors) {
-                    ExtensionLoader<?> extensionLoader = 
director.getExtensionLoader(inf);
-                    n1 = extensionLoader.getExtensionName(o1);
-                    if (n1 != null) {
-                        break;
-                    }
-                }
-
-                if (a2.isLess(n1)) {
-                    return 1;
-                }
-
-                if (a2.isMore(n1)) {
-                    return -1;
-                }
-            }
-
-            return a1.order > a2.order ? 1 : -1;
-        }
-
-        // In order to avoid the problem of inconsistency between the loading 
order of two filters
-        // in different loading scenarios without specifying the order 
attribute of the filter,
-        // when the order is the same, compare its filterName
-        if (a1.order > a2.order) {
-            return 1;
-        } else if (a1.order == a2.order) {
-            return o1.getSimpleName().compareTo(o2.getSimpleName()) > 0 ? 1 : 
-1;
-        } else {
-            return -1;
-        }
-    }
-
-    private Class<?> findSpi(Class<?> clazz) {
-        if (clazz.getInterfaces().length == 0) {
-            return null;
-        }
-
-        for (Class<?> intf : clazz.getInterfaces()) {
-            if (intf.isAnnotationPresent(SPI.class)) {
-                return intf;
-            }
-            Class<?> result = findSpi(intf);
-            if (result != null) {
-                return result;
-            }
-        }
-
-        return null;
-    }
-
-    private ActivateInfo parseActivate(Class<?> clazz) {
-        ActivateInfo info = activateInfoMap.get(clazz);
-        if (info != null) {
-            return info;
-        }
-        info = new ActivateInfo();
-        if (clazz.isAnnotationPresent(Activate.class)) {
-            Activate activate = clazz.getAnnotation(Activate.class);
-            info.before = activate.before();
-            info.after = activate.after();
-            info.order = activate.order();
-        } else if 
(clazz.isAnnotationPresent(com.alibaba.dubbo.common.extension.Activate.class)) {
-            com.alibaba.dubbo.common.extension.Activate activate = 
clazz.getAnnotation(
-                com.alibaba.dubbo.common.extension.Activate.class);
-            info.before = activate.before();
-            info.after = activate.after();
-            info.order = activate.order();
-        } else {
-            info.order = 0;
-        }
-        activateInfoMap.put(clazz, info);
-        return info;
-    }
-
-    private static class ActivateInfo {
-        private String[] before;
-        private String[] after;
-        private int order;
-
-        private boolean applicableToCompare() {
-            return ArrayUtils.isNotEmpty(before) || 
ArrayUtils.isNotEmpty(after);
-        }
-
-        private boolean isLess(String name) {
-            return Arrays.asList(before).contains(name);
-        }
-
-        private boolean isMore(String name) {
-            return Arrays.asList(after).contains(name);
-        }
-    }
-}
diff --git 
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/AbstractCacheManager.java
 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/AbstractCacheManager.java
index 4863f3dd6e..f299353474 100644
--- 
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/AbstractCacheManager.java
+++ 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/AbstractCacheManager.java
@@ -46,7 +46,7 @@ public abstract class AbstractCacheManager<V> implements 
Disposable {
         try {
             cacheStore = FileCacheStoreFactory.getInstance(filePath, fileName, 
enableFileCache);
             Map<String, String> properties = cacheStore.loadCache(entrySize);
-            logger.info("Successfully loaded mapping cache from file " + 
fileName + ", entries " + properties.size());
+            logger.info("Successfully loaded " + getName() + " cache from file 
" + fileName + ", entries " + properties.size());
             for (Map.Entry<String, String> entry : properties.entrySet()) {
                 String key = entry.getKey();
                 String value = entry.getValue();
diff --git 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/ListenerRegistryWrapper.java
 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/ListenerRegistryWrapper.java
index c2300755df..c9cdc67773 100644
--- 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/ListenerRegistryWrapper.java
+++ 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/ListenerRegistryWrapper.java
@@ -24,6 +24,7 @@ import org.apache.dubbo.common.utils.CollectionUtils;
 import org.apache.dubbo.common.utils.UrlUtils;
 
 import java.util.List;
+import java.util.function.Consumer;
 
 public class ListenerRegistryWrapper implements Registry {
     private static final ErrorTypeAwareLogger logger = 
LoggerFactory.getErrorTypeAwareLogger(ListenerRegistryWrapper.class);
@@ -59,21 +60,8 @@ public class ListenerRegistryWrapper implements Registry {
                 registry.register(url);
             }
         } finally {
-            if (CollectionUtils.isNotEmpty(listeners) && 
!UrlUtils.isConsumer(url)) {
-                RuntimeException exception = null;
-                for (RegistryServiceListener listener : listeners) {
-                    if (listener != null) {
-                        try {
-                            listener.onRegister(url, registry);
-                        } catch (RuntimeException t) {
-                            logger.error(t.getMessage(), t);
-                            exception = t;
-                        }
-                    }
-                }
-                if (exception != null) {
-                    throw exception;
-                }
+            if (!UrlUtils.isConsumer(url)) {
+                listenerEvent(serviceListener -> 
serviceListener.onRegister(url, registry));
             }
         }
     }
@@ -85,21 +73,8 @@ public class ListenerRegistryWrapper implements Registry {
                 registry.unregister(url);
             }
         } finally {
-            if (CollectionUtils.isNotEmpty(listeners) && 
!UrlUtils.isConsumer(url)) {
-                RuntimeException exception = null;
-                for (RegistryServiceListener listener : listeners) {
-                    if (listener != null) {
-                        try {
-                            listener.onUnregister(url, registry);
-                        } catch (RuntimeException t) {
-                            logger.error(t.getMessage(), t);
-                            exception = t;
-                        }
-                    }
-                }
-                if (exception != null) {
-                    throw exception;
-                }
+            if (!UrlUtils.isConsumer(url)) {
+                listenerEvent(serviceListener -> 
serviceListener.onUnregister(url, registry));
             }
         }
     }
@@ -111,46 +86,18 @@ public class ListenerRegistryWrapper implements Registry {
                 registry.subscribe(url, listener);
             }
         } finally {
-            if (CollectionUtils.isNotEmpty(listeners)) {
-                RuntimeException exception = null;
-                for (RegistryServiceListener registryListener : listeners) {
-                    if (registryListener != null) {
-                        try {
-                            registryListener.onSubscribe(url, registry);
-                        } catch (RuntimeException t) {
-                            logger.error(t.getMessage(), t);
-                            exception = t;
-                        }
-                    }
-                }
-                if (exception != null) {
-                    throw exception;
-                }
-            }
+            listenerEvent(serviceListener -> serviceListener.onSubscribe(url, 
registry));
         }
     }
 
+
     @Override
     public void unsubscribe(URL url, NotifyListener listener) {
         try {
             registry.unsubscribe(url, listener);
         } finally {
-            if (CollectionUtils.isNotEmpty(listeners)) {
-                RuntimeException exception = null;
-                for (RegistryServiceListener registryListener : listeners) {
-                    if (registryListener != null) {
-                        try {
-                            registryListener.onUnsubscribe(url, registry);
-                        } catch (RuntimeException t) {
-                            logger.error(t.getMessage(), t);
-                            exception = t;
-                        }
-                    }
-                }
-                if (exception != null) {
-                    throw exception;
-                }
-            }
+            listenerEvent(serviceListener -> 
serviceListener.onUnsubscribe(url, registry));
+
         }
     }
 
@@ -167,4 +114,23 @@ public class ListenerRegistryWrapper implements Registry {
     public Registry getRegistry() {
         return registry;
     }
+
+    private void listenerEvent(Consumer<RegistryServiceListener> consumer) {
+        if (CollectionUtils.isNotEmpty(listeners)) {
+            RuntimeException exception = null;
+            for (RegistryServiceListener listener : listeners) {
+                if (listener != null) {
+                    try {
+                        consumer.accept(listener);
+                    } catch (RuntimeException t) {
+                        logger.error(t.getMessage(), t);
+                        exception = t;
+                    }
+                }
+            }
+            if (exception != null) {
+                throw exception;
+            }
+        }
+    }
 }
diff --git 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java
 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java
index a3a73e28d5..4ede56efb4 100644
--- 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java
+++ 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java
@@ -67,29 +67,28 @@ public abstract class AbstractServiceDiscovery implements 
ServiceDiscovery {
     protected ApplicationModel applicationModel;
 
     public AbstractServiceDiscovery(ApplicationModel applicationModel, URL 
registryURL) {
-        this(applicationModel.getApplicationName(), registryURL);
-        this.applicationModel = applicationModel;
+        this(applicationModel, applicationModel.getApplicationName(), 
registryURL);
         MetadataReportInstance metadataReportInstance = 
applicationModel.getBeanFactory().getBean(MetadataReportInstance.class);
         metadataType = metadataReportInstance.getMetadataType();
         this.metadataReport = 
metadataReportInstance.getMetadataReport(registryURL.getParameter(REGISTRY_CLUSTER_KEY));
-//        if 
(REMOTE_METADATA_STORAGE_TYPE.equals(metadataReportInstance.getMetadataType())) 
{
-//            this.metadataReport = 
metadataReportInstance.getMetadataReport(registryURL.getParameter(REGISTRY_CLUSTER_KEY));
-//        } else {
-//            this.metadataReport = 
metadataReportInstance.getNopMetadataReport();
-//        }
     }
 
     public AbstractServiceDiscovery(String serviceName, URL registryURL) {
-        this.applicationModel = ApplicationModel.defaultModel();
-        this.registryURL = registryURL;
+        this(ApplicationModel.defaultModel(), serviceName, registryURL);
+    }
+
+    private AbstractServiceDiscovery(ApplicationModel applicationModel, String 
serviceName, URL registryURL) {
+        this.applicationModel = applicationModel;
         this.serviceName = serviceName;
+        this.registryURL = registryURL;
         this.metadataInfo = new MetadataInfo(serviceName);
         boolean localCacheEnabled = 
registryURL.getParameter(REGISTRY_LOCAL_FILE_CACHE_ENABLED, true);
         this.metaCacheManager = new MetaCacheManager(localCacheEnabled, 
getCacheNameSuffix(),
             applicationModel.getFrameworkModel().getBeanFactory()
-            
.getBean(FrameworkExecutorRepository.class).getCacheRefreshingScheduledExecutor());
+                
.getBean(FrameworkExecutorRepository.class).getCacheRefreshingScheduledExecutor());
     }
 
+
     @Override
     public synchronized void register() throws RuntimeException {
         this.serviceInstance = createServiceInstance(this.metadataInfo);
diff --git 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryFactory.java
 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryFactory.java
index 2332f04e88..21680951e8 100644
--- 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryFactory.java
+++ 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryFactory.java
@@ -35,7 +35,6 @@ public interface ServiceDiscoveryFactory {
      * Get the instance of {@link ServiceDiscovery}
      *
      * @param registryURL the {@link URL} to connect the registry
-     * @param model, the application model context
      * @return non-null
      */
     ServiceDiscovery getServiceDiscovery(URL registryURL);
diff --git 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataUtils.java
 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataUtils.java
index 48fe0deea3..1007cec8c6 100644
--- 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataUtils.java
+++ 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataUtils.java
@@ -58,6 +58,7 @@ public class MetadataUtils {
         if (getMetadataReports(applicationModel).size() == 0) {
             String msg = "Remote Metadata Report Server is not provided or 
unavailable, will stop registering service definition to remote center!";
             logger.warn(msg);
+            return;
         }
 
         try {
diff --git 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java
 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java
index 19cc45f103..913c0cc539 100644
--- 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java
+++ 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java
@@ -151,7 +151,6 @@ public class RegistryProtocol implements Protocol, 
ScopeModelAware {
     private final ConcurrentMap<String, ExporterChangeableWrapper<?>> bounds = 
new ConcurrentHashMap<>();
     protected Protocol protocol;
     protected ProxyFactory proxyFactory;
-    //protected RegistryFactory registryFactory;
 
     private ConcurrentMap<URL, ReExportTask> reExportFailedTasks = new 
ConcurrentHashMap<>();
     private HashedWheelTimer retryTimer = new HashedWheelTimer(new 
NamedThreadFactory("DubboReexportTimer", true), DEFAULT_REGISTRY_RETRY_PERIOD, 
TimeUnit.MILLISECONDS, 128);
@@ -181,11 +180,6 @@ public class RegistryProtocol implements Protocol, 
ScopeModelAware {
         this.protocol = protocol;
     }
 
-    // Cannot inject registryFactory (application scope) into protocol 
(framework scope)
-//    public void setRegistryFactory(RegistryFactory registryFactory) {
-//        this.registryFactory = registryFactory;
-//    }
-
     public void setProxyFactory(ProxyFactory proxyFactory) {
         this.proxyFactory = proxyFactory;
     }
diff --git 
a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/util/CuratorFrameworkUtils.java
 
b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/util/CuratorFrameworkUtils.java
index 9f10fa2e8a..12e6fa03b7 100644
--- 
a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/util/CuratorFrameworkUtils.java
+++ 
b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/util/CuratorFrameworkUtils.java
@@ -19,6 +19,7 @@ package org.apache.dubbo.registry.zookeeper.util;
 import org.apache.dubbo.common.URL;
 import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
 import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.common.utils.StringUtils;
 import org.apache.dubbo.registry.client.DefaultServiceInstance;
 import org.apache.dubbo.registry.client.ServiceInstance;
 import org.apache.dubbo.registry.zookeeper.ZookeeperInstance;
@@ -77,7 +78,7 @@ public abstract class CuratorFrameworkUtils {
             .connectString(connectionURL.getBackupAddress())
             .retryPolicy(buildRetryPolicy(connectionURL));
         String userInformation = connectionURL.getUserInformation();
-        if (userInformation != null && userInformation.length() > 0) {
+        if (StringUtils.isNotEmpty(userInformation)) {
             builder = builder.authorization("digest", 
userInformation.getBytes());
             builder.aclProvider(new ACLProvider() {
                 @Override
diff --git 
a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/listener/ListenerExporterWrapper.java
 
b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/listener/ListenerExporterWrapper.java
index df3497f690..021af672a4 100644
--- 
a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/listener/ListenerExporterWrapper.java
+++ 
b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/listener/ListenerExporterWrapper.java
@@ -24,6 +24,7 @@ import org.apache.dubbo.rpc.ExporterListener;
 import org.apache.dubbo.rpc.Invoker;
 
 import java.util.List;
+import java.util.function.Consumer;
 
 /**
  * ListenerExporter
@@ -42,22 +43,7 @@ public class ListenerExporterWrapper<T> implements 
Exporter<T> {
         }
         this.exporter = exporter;
         this.listeners = listeners;
-        if (CollectionUtils.isNotEmpty(listeners)) {
-            RuntimeException exception = null;
-            for (ExporterListener listener : listeners) {
-                if (listener != null) {
-                    try {
-                        listener.exported(this);
-                    } catch (RuntimeException t) {
-                        logger.error(t.getMessage(), t);
-                        exception = t;
-                    }
-                }
-            }
-            if (exception != null) {
-                throw exception;
-            }
-        }
+        listenerEvent(listener -> listener.exported(this));
     }
 
     @Override
@@ -70,23 +56,26 @@ public class ListenerExporterWrapper<T> implements 
Exporter<T> {
         try {
             exporter.unexport();
         } finally {
-            if (CollectionUtils.isNotEmpty(listeners)) {
-                RuntimeException exception = null;
-                for (ExporterListener listener : listeners) {
-                    if (listener != null) {
-                        try {
-                            listener.unexported(this);
-                        } catch (RuntimeException t) {
-                            logger.error(t.getMessage(), t);
-                            exception = t;
-                        }
+            listenerEvent(listener -> listener.unexported(this));
+        }
+    }
+
+    private void listenerEvent(Consumer<ExporterListener> consumer) {
+        if (CollectionUtils.isNotEmpty(listeners)) {
+            RuntimeException exception = null;
+            for (ExporterListener listener : listeners) {
+                if (listener != null) {
+                    try {
+                        consumer.accept(listener);
+                    } catch (RuntimeException t) {
+                        logger.error(t.getMessage(), t);
+                        exception = t;
                     }
                 }
-                if (exception != null) {
-                    throw exception;
-                }
+            }
+            if (exception != null) {
+                throw exception;
             }
         }
     }
-
 }
diff --git 
a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/listener/ListenerInvokerWrapper.java
 
b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/listener/ListenerInvokerWrapper.java
index 14254c7721..2addb81d29 100644
--- 
a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/listener/ListenerInvokerWrapper.java
+++ 
b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/listener/ListenerInvokerWrapper.java
@@ -27,6 +27,7 @@ import org.apache.dubbo.rpc.Result;
 import org.apache.dubbo.rpc.RpcException;
 
 import java.util.List;
+import java.util.function.Consumer;
 
 /**
  * ListenerInvoker
@@ -45,19 +46,10 @@ public class ListenerInvokerWrapper<T> implements 
Invoker<T> {
         }
         this.invoker = invoker;
         this.listeners = listeners;
-        if (CollectionUtils.isNotEmpty(listeners)) {
-            for (InvokerListener listener : listeners) {
-                if (listener != null) {
-                    try {
-                        listener.referred(invoker);
-                    } catch (Throwable t) {
-                        logger.error(t.getMessage(), t);
-                    }
-                }
-            }
-        }
+        listenerEvent(listener -> listener.referred(invoker));
     }
 
+
     @Override
     public Class<T> getInterface() {
         return invoker.getInterface();
@@ -88,17 +80,7 @@ public class ListenerInvokerWrapper<T> implements Invoker<T> 
{
         try {
             invoker.destroy();
         } finally {
-            if (CollectionUtils.isNotEmpty(listeners)) {
-                for (InvokerListener listener : listeners) {
-                    if (listener != null) {
-                        try {
-                            listener.destroyed(invoker);
-                        } catch (Throwable t) {
-                            logger.error(t.getMessage(), t);
-                        }
-                    }
-                }
-            }
+            listenerEvent(listener -> listener.destroyed(invoker));
         }
     }
 
@@ -109,4 +91,23 @@ public class ListenerInvokerWrapper<T> implements 
Invoker<T> {
     public List<InvokerListener> getListeners() {
         return listeners;
     }
+
+    private void listenerEvent(Consumer<InvokerListener> consumer) {
+        if (CollectionUtils.isNotEmpty(listeners)) {
+            RuntimeException exception = null;
+            for (InvokerListener listener : listeners) {
+                if (listener != null) {
+                    try {
+                        consumer.accept(listener);
+                    } catch (RuntimeException t) {
+                        logger.error(t.getMessage(), t);
+                        exception = t;
+                    }
+                }
+            }
+            if (exception != null) {
+                throw exception;
+            }
+        }
+    }
 }

Reply via email to