Skip to content

Commit

Permalink
add some small optimize (#3171)
Browse files Browse the repository at this point in the history
* modify some log describe

* use java8 lambda expression
  • Loading branch information
lixiaojiee authored and carryxyh committed Jan 9, 2019
1 parent 2cbc83f commit f4e96a4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -104,7 +105,10 @@ public class ReferenceConfig<T> extends AbstractReferenceConfig {
* The interface class of the reference service
*/
private Class<?> interfaceClass;
// client type

/**
* client type
*/
private String client;

/**
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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<String>(Arrays.asList(methods)), ","));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -450,7 +450,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List<URL> 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
Expand All @@ -459,7 +459,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List<URL> 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());
}
}
}
Expand All @@ -470,7 +470,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List<URL> 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: <dubbo:argument index='0' .../> or <dubbo:argument type=xxx .../>");
throw new IllegalArgumentException("Argument config must set index or type attribute.eg: <dubbo:argument index='0' .../> or <dubbo:argument type=xxx .../>");
}

}
Expand All @@ -489,7 +489,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List<URL> 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<String>(Arrays.asList(methods)), ","));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() + "!");
}
}
}
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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 {
Expand Down Expand Up @@ -292,11 +292,10 @@ public void subscribe(URL url, NotifyListener listener) {
if (logger.isInfoEnabled()) {
logger.info("Subscribe: " + url);
}
Set<NotifyListener> listeners = subscribed.get(url);
if (listeners == null) {
subscribed.putIfAbsent(url, new ConcurrentHashSet<NotifyListener>());
listeners = subscribed.get(url);
}

Set<NotifyListener> listeners = subscribed.computeIfAbsent(url, k -> {
return new ConcurrentHashSet<>();
});
listeners.add(listener);
}

Expand Down Expand Up @@ -387,22 +386,18 @@ protected void notify(URL url, NotifyListener listener, List<URL> urls) {
for (URL u : urls) {
if (UrlUtils.isMatch(url, u)) {
String category = u.getParameter(Constants.CATEGORY_KEY, Constants.DEFAULT_CATEGORY);
List<URL> categoryList = result.get(category);
if (categoryList == null) {
categoryList = new ArrayList<URL>();
result.put(category, categoryList);
}
List<URL> categoryList = result.computeIfAbsent(category, k -> {
return new ArrayList<>();
});
categoryList.add(u);
}
}
if (result.size() == 0) {
return;
}
Map<String, List<URL>> categoryNotified = notified.get(url);
if (categoryNotified == null) {
notified.putIfAbsent(url, new ConcurrentHashMap<String, List<URL>>());
categoryNotified = notified.get(url);
}
Map<String, List<URL>> categoryNotified = notified.computeIfAbsent(url, k -> {
return new ConcurrentHashMap<>();
});
for (Map.Entry<String, List<URL>> entry : result.entrySet()) {
String category = entry.getKey();
List<URL> categoryList = entry.getValue();
Expand Down

1 comment on commit f4e96a4

@zkqcommon
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

Please sign in to comment.