From f4e96a4bb20c6b9a8286323ba5ea796f36f84181 Mon Sep 17 00:00:00 2001 From: XiaoJie Li Date: Wed, 9 Jan 2019 10:19:54 +0800 Subject: [PATCH] add some small optimize (#3171) * modify some log describe * use java8 lambda expression --- .../dubbo/config/AbstractInterfaceConfig.java | 2 +- .../apache/dubbo/config/ReferenceConfig.java | 17 ++++++---- .../apache/dubbo/config/ServiceConfig.java | 12 +++---- .../registry/support/AbstractRegistry.java | 33 ++++++++----------- 4 files changed, 31 insertions(+), 33 deletions(-) diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java index 051e26e9350..bd6e0fbff3d 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java @@ -500,7 +500,7 @@ private void convertRegistryIdsToRegistries() { } }); if (registries.size() > arr.length) { - throw new IllegalStateException("Too much registries found, the registries assigned to this service " + + throw new IllegalStateException("Too many registries found, the registries assigned to this service " + "are :" + registryIds + ", but got " + registries.size() + " registries!"); } } diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java index 3110d762ecc..4b30bfe101d 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java @@ -21,6 +21,7 @@ import org.apache.dubbo.common.Version; import org.apache.dubbo.common.bytecode.Wrapper; import org.apache.dubbo.common.extension.ExtensionLoader; +import org.apache.dubbo.common.utils.ClassHelper; import org.apache.dubbo.common.utils.ConfigUtils; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.common.utils.StringUtils; @@ -104,7 +105,10 @@ public class ReferenceConfig extends AbstractReferenceConfig { * The interface class of the reference service */ private Class interfaceClass; - // client type + + /** + * client type + */ private String client; /** @@ -226,7 +230,7 @@ public synchronized T get() { checkAndUpdateSubConfigs(); if (destroyed) { - throw new IllegalStateException("Already destroyed!"); + throw new IllegalStateException("The invoker of ReferenceConfig(" + url + ") has already destroyed!"); } if (ref == null) { init(); @@ -245,7 +249,7 @@ public synchronized void destroy() { try { invoker.destroy(); } catch (Throwable t) { - logger.warn("Unexpected err when destroy invoker of ReferenceConfig(" + url + ").", t); + logger.warn("Unexpected error occured when destroy invoker of ReferenceConfig(" + url + ").", t); } invoker = null; ref = null; @@ -270,7 +274,7 @@ private void init() { String[] methods = Wrapper.getWrapper(interfaceClass).getMethodNames(); if (methods.length == 0) { - logger.warn("NO method found in service interface " + interfaceClass.getName()); + logger.warn("No method found in service interface " + interfaceClass.getName()); map.put("methods", Constants.ANY_VALUE); } else { map.put("methods", StringUtils.join(new HashSet(Arrays.asList(methods)), ",")); @@ -465,8 +469,7 @@ public Class getInterfaceClass() { } try { if (interfaceName != null && interfaceName.length() > 0) { - this.interfaceClass = Class.forName(interfaceName, true, Thread.currentThread() - .getContextClassLoader()); + this.interfaceClass = Class.forName(interfaceName, true, ClassHelper.getClassLoader()); } } catch (ClassNotFoundException t) { throw new IllegalStateException(t.getMessage(), t); @@ -588,7 +591,7 @@ private void resolveFile() { fis = new FileInputStream(new File(resolveFile)); properties.load(fis); } catch (IOException e) { - throw new IllegalStateException("Unload " + resolveFile + ", cause: " + e.getMessage(), e); + throw new IllegalStateException("Failed to load " + resolveFile + ", cause: " + e.getMessage(), e); } finally { try { if (null != fis) { diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java index e1e3d0c969f..fe870db1f2b 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java @@ -353,7 +353,7 @@ public synchronized void export() { protected synchronized void doExport() { if (unexported) { - throw new IllegalStateException("Already unexported!"); + throw new IllegalStateException("The service " + interfaceClass.getName() + " has already unexported!"); } if (exported) { return; @@ -392,7 +392,7 @@ public synchronized void unexport() { try { exporter.unexport(); } catch (Throwable t) { - logger.warn("unexpected err when unexport" + exporter, t); + logger.warn("Unexpected error occured when unexport " + exporter, t); } } exporters.clear(); @@ -450,7 +450,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List r if (argtypes[argument.getIndex()].getName().equals(argument.getType())) { appendParameters(map, argument, method.getName() + "." + argument.getIndex()); } else { - throw new IllegalArgumentException("argument config error : the index attribute and type attribute not match :index :" + argument.getIndex() + ", type:" + argument.getType()); + throw new IllegalArgumentException("Argument config error : the index attribute and type attribute not match :index :" + argument.getIndex() + ", type:" + argument.getType()); } } else { // multiple callbacks in the method @@ -459,7 +459,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List r if (argclazz.getName().equals(argument.getType())) { appendParameters(map, argument, method.getName() + "." + j); if (argument.getIndex() != -1 && argument.getIndex() != j) { - throw new IllegalArgumentException("argument config error : the index attribute and type attribute not match :index :" + argument.getIndex() + ", type:" + argument.getType()); + throw new IllegalArgumentException("Argument config error : the index attribute and type attribute not match :index :" + argument.getIndex() + ", type:" + argument.getType()); } } } @@ -470,7 +470,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List r } else if (argument.getIndex() != -1) { appendParameters(map, argument, method.getName() + "." + argument.getIndex()); } else { - throw new IllegalArgumentException("argument config must set index or type attribute.eg: or "); + throw new IllegalArgumentException("Argument config must set index or type attribute.eg: or "); } } @@ -489,7 +489,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List r String[] methods = Wrapper.getWrapper(interfaceClass).getMethodNames(); if (methods.length == 0) { - logger.warn("NO method found in service interface " + interfaceClass.getName()); + logger.warn("No method found in service interface " + interfaceClass.getName()); map.put(Constants.METHODS_KEY, Constants.ANY_VALUE); } else { map.put(Constants.METHODS_KEY, StringUtils.join(new HashSet(Arrays.asList(methods)), ",")); diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java index fc2f86d31ea..50c7bb10a2b 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java @@ -85,7 +85,7 @@ public AbstractRegistry(URL url) { file = new File(filename); if (!file.exists() && file.getParentFile() != null && !file.getParentFile().exists()) { if (!file.getParentFile().mkdirs()) { - throw new IllegalArgumentException("Invalid registry store file " + file + ", cause: Failed to create directory " + file.getParentFile() + "!"); + throw new IllegalArgumentException("Invalid registry cache file " + file + ", cause: Failed to create directory " + file.getParentFile() + "!"); } } } @@ -186,7 +186,7 @@ public void doSaveProperties(long version) { } else { registryCacheExecutor.execute(new SaveProperties(lastCacheChanged.incrementAndGet())); } - logger.warn("Failed to save registry store file, cause: " + e.getMessage(), e); + logger.warn("Failed to save registry cache file, cause: " + e.getMessage(), e); } } @@ -197,10 +197,10 @@ private void loadProperties() { in = new FileInputStream(file); properties.load(in); if (logger.isInfoEnabled()) { - logger.info("Load registry store file " + file + ", data: " + properties); + logger.info("Load registry cache file " + file + ", data: " + properties); } } catch (Throwable e) { - logger.warn("Failed to load registry store file " + file, e); + logger.warn("Failed to load registry cache file " + file, e); } finally { if (in != null) { try { @@ -292,11 +292,10 @@ public void subscribe(URL url, NotifyListener listener) { if (logger.isInfoEnabled()) { logger.info("Subscribe: " + url); } - Set listeners = subscribed.get(url); - if (listeners == null) { - subscribed.putIfAbsent(url, new ConcurrentHashSet()); - listeners = subscribed.get(url); - } + + Set listeners = subscribed.computeIfAbsent(url, k -> { + return new ConcurrentHashSet<>(); + }); listeners.add(listener); } @@ -387,22 +386,18 @@ protected void notify(URL url, NotifyListener listener, List urls) { for (URL u : urls) { if (UrlUtils.isMatch(url, u)) { String category = u.getParameter(Constants.CATEGORY_KEY, Constants.DEFAULT_CATEGORY); - List categoryList = result.get(category); - if (categoryList == null) { - categoryList = new ArrayList(); - result.put(category, categoryList); - } + List categoryList = result.computeIfAbsent(category, k -> { + return new ArrayList<>(); + }); categoryList.add(u); } } if (result.size() == 0) { return; } - Map> categoryNotified = notified.get(url); - if (categoryNotified == null) { - notified.putIfAbsent(url, new ConcurrentHashMap>()); - categoryNotified = notified.get(url); - } + Map> categoryNotified = notified.computeIfAbsent(url, k -> { + return new ConcurrentHashMap<>(); + }); for (Map.Entry> entry : result.entrySet()) { String category = entry.getKey(); List categoryList = entry.getValue();