Skip to content

Commit f4e96a4

Browse files
lixiaojieecarryxyh
authored andcommitted
add some small optimize (#3171)
* modify some log describe * use java8 lambda expression
1 parent 2cbc83f commit f4e96a4

File tree

4 files changed

+31
-33
lines changed

4 files changed

+31
-33
lines changed

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ private void convertRegistryIdsToRegistries() {
500500
}
501501
});
502502
if (registries.size() > arr.length) {
503-
throw new IllegalStateException("Too much registries found, the registries assigned to this service " +
503+
throw new IllegalStateException("Too many registries found, the registries assigned to this service " +
504504
"are :" + registryIds + ", but got " + registries.size() + " registries!");
505505
}
506506
}

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.dubbo.common.Version;
2222
import org.apache.dubbo.common.bytecode.Wrapper;
2323
import org.apache.dubbo.common.extension.ExtensionLoader;
24+
import org.apache.dubbo.common.utils.ClassHelper;
2425
import org.apache.dubbo.common.utils.ConfigUtils;
2526
import org.apache.dubbo.common.utils.NetUtils;
2627
import org.apache.dubbo.common.utils.StringUtils;
@@ -104,7 +105,10 @@ public class ReferenceConfig<T> extends AbstractReferenceConfig {
104105
* The interface class of the reference service
105106
*/
106107
private Class<?> interfaceClass;
107-
// client type
108+
109+
/**
110+
* client type
111+
*/
108112
private String client;
109113

110114
/**
@@ -226,7 +230,7 @@ public synchronized T get() {
226230
checkAndUpdateSubConfigs();
227231

228232
if (destroyed) {
229-
throw new IllegalStateException("Already destroyed!");
233+
throw new IllegalStateException("The invoker of ReferenceConfig(" + url + ") has already destroyed!");
230234
}
231235
if (ref == null) {
232236
init();
@@ -245,7 +249,7 @@ public synchronized void destroy() {
245249
try {
246250
invoker.destroy();
247251
} catch (Throwable t) {
248-
logger.warn("Unexpected err when destroy invoker of ReferenceConfig(" + url + ").", t);
252+
logger.warn("Unexpected error occured when destroy invoker of ReferenceConfig(" + url + ").", t);
249253
}
250254
invoker = null;
251255
ref = null;
@@ -270,7 +274,7 @@ private void init() {
270274

271275
String[] methods = Wrapper.getWrapper(interfaceClass).getMethodNames();
272276
if (methods.length == 0) {
273-
logger.warn("NO method found in service interface " + interfaceClass.getName());
277+
logger.warn("No method found in service interface " + interfaceClass.getName());
274278
map.put("methods", Constants.ANY_VALUE);
275279
} else {
276280
map.put("methods", StringUtils.join(new HashSet<String>(Arrays.asList(methods)), ","));
@@ -465,8 +469,7 @@ public Class<?> getInterfaceClass() {
465469
}
466470
try {
467471
if (interfaceName != null && interfaceName.length() > 0) {
468-
this.interfaceClass = Class.forName(interfaceName, true, Thread.currentThread()
469-
.getContextClassLoader());
472+
this.interfaceClass = Class.forName(interfaceName, true, ClassHelper.getClassLoader());
470473
}
471474
} catch (ClassNotFoundException t) {
472475
throw new IllegalStateException(t.getMessage(), t);
@@ -588,7 +591,7 @@ private void resolveFile() {
588591
fis = new FileInputStream(new File(resolveFile));
589592
properties.load(fis);
590593
} catch (IOException e) {
591-
throw new IllegalStateException("Unload " + resolveFile + ", cause: " + e.getMessage(), e);
594+
throw new IllegalStateException("Failed to load " + resolveFile + ", cause: " + e.getMessage(), e);
592595
} finally {
593596
try {
594597
if (null != fis) {

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ public synchronized void export() {
353353

354354
protected synchronized void doExport() {
355355
if (unexported) {
356-
throw new IllegalStateException("Already unexported!");
356+
throw new IllegalStateException("The service " + interfaceClass.getName() + " has already unexported!");
357357
}
358358
if (exported) {
359359
return;
@@ -392,7 +392,7 @@ public synchronized void unexport() {
392392
try {
393393
exporter.unexport();
394394
} catch (Throwable t) {
395-
logger.warn("unexpected err when unexport" + exporter, t);
395+
logger.warn("Unexpected error occured when unexport " + exporter, t);
396396
}
397397
}
398398
exporters.clear();
@@ -450,7 +450,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List<URL> r
450450
if (argtypes[argument.getIndex()].getName().equals(argument.getType())) {
451451
appendParameters(map, argument, method.getName() + "." + argument.getIndex());
452452
} else {
453-
throw new IllegalArgumentException("argument config error : the index attribute and type attribute not match :index :" + argument.getIndex() + ", type:" + argument.getType());
453+
throw new IllegalArgumentException("Argument config error : the index attribute and type attribute not match :index :" + argument.getIndex() + ", type:" + argument.getType());
454454
}
455455
} else {
456456
// multiple callbacks in the method
@@ -459,7 +459,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List<URL> r
459459
if (argclazz.getName().equals(argument.getType())) {
460460
appendParameters(map, argument, method.getName() + "." + j);
461461
if (argument.getIndex() != -1 && argument.getIndex() != j) {
462-
throw new IllegalArgumentException("argument config error : the index attribute and type attribute not match :index :" + argument.getIndex() + ", type:" + argument.getType());
462+
throw new IllegalArgumentException("Argument config error : the index attribute and type attribute not match :index :" + argument.getIndex() + ", type:" + argument.getType());
463463
}
464464
}
465465
}
@@ -470,7 +470,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List<URL> r
470470
} else if (argument.getIndex() != -1) {
471471
appendParameters(map, argument, method.getName() + "." + argument.getIndex());
472472
} else {
473-
throw new IllegalArgumentException("argument config must set index or type attribute.eg: <dubbo:argument index='0' .../> or <dubbo:argument type=xxx .../>");
473+
throw new IllegalArgumentException("Argument config must set index or type attribute.eg: <dubbo:argument index='0' .../> or <dubbo:argument type=xxx .../>");
474474
}
475475

476476
}
@@ -489,7 +489,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List<URL> r
489489

490490
String[] methods = Wrapper.getWrapper(interfaceClass).getMethodNames();
491491
if (methods.length == 0) {
492-
logger.warn("NO method found in service interface " + interfaceClass.getName());
492+
logger.warn("No method found in service interface " + interfaceClass.getName());
493493
map.put(Constants.METHODS_KEY, Constants.ANY_VALUE);
494494
} else {
495495
map.put(Constants.METHODS_KEY, StringUtils.join(new HashSet<String>(Arrays.asList(methods)), ","));

dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public AbstractRegistry(URL url) {
8585
file = new File(filename);
8686
if (!file.exists() && file.getParentFile() != null && !file.getParentFile().exists()) {
8787
if (!file.getParentFile().mkdirs()) {
88-
throw new IllegalArgumentException("Invalid registry store file " + file + ", cause: Failed to create directory " + file.getParentFile() + "!");
88+
throw new IllegalArgumentException("Invalid registry cache file " + file + ", cause: Failed to create directory " + file.getParentFile() + "!");
8989
}
9090
}
9191
}
@@ -186,7 +186,7 @@ public void doSaveProperties(long version) {
186186
} else {
187187
registryCacheExecutor.execute(new SaveProperties(lastCacheChanged.incrementAndGet()));
188188
}
189-
logger.warn("Failed to save registry store file, cause: " + e.getMessage(), e);
189+
logger.warn("Failed to save registry cache file, cause: " + e.getMessage(), e);
190190
}
191191
}
192192

@@ -197,10 +197,10 @@ private void loadProperties() {
197197
in = new FileInputStream(file);
198198
properties.load(in);
199199
if (logger.isInfoEnabled()) {
200-
logger.info("Load registry store file " + file + ", data: " + properties);
200+
logger.info("Load registry cache file " + file + ", data: " + properties);
201201
}
202202
} catch (Throwable e) {
203-
logger.warn("Failed to load registry store file " + file, e);
203+
logger.warn("Failed to load registry cache file " + file, e);
204204
} finally {
205205
if (in != null) {
206206
try {
@@ -292,11 +292,10 @@ public void subscribe(URL url, NotifyListener listener) {
292292
if (logger.isInfoEnabled()) {
293293
logger.info("Subscribe: " + url);
294294
}
295-
Set<NotifyListener> listeners = subscribed.get(url);
296-
if (listeners == null) {
297-
subscribed.putIfAbsent(url, new ConcurrentHashSet<NotifyListener>());
298-
listeners = subscribed.get(url);
299-
}
295+
296+
Set<NotifyListener> listeners = subscribed.computeIfAbsent(url, k -> {
297+
return new ConcurrentHashSet<>();
298+
});
300299
listeners.add(listener);
301300
}
302301

@@ -387,22 +386,18 @@ protected void notify(URL url, NotifyListener listener, List<URL> urls) {
387386
for (URL u : urls) {
388387
if (UrlUtils.isMatch(url, u)) {
389388
String category = u.getParameter(Constants.CATEGORY_KEY, Constants.DEFAULT_CATEGORY);
390-
List<URL> categoryList = result.get(category);
391-
if (categoryList == null) {
392-
categoryList = new ArrayList<URL>();
393-
result.put(category, categoryList);
394-
}
389+
List<URL> categoryList = result.computeIfAbsent(category, k -> {
390+
return new ArrayList<>();
391+
});
395392
categoryList.add(u);
396393
}
397394
}
398395
if (result.size() == 0) {
399396
return;
400397
}
401-
Map<String, List<URL>> categoryNotified = notified.get(url);
402-
if (categoryNotified == null) {
403-
notified.putIfAbsent(url, new ConcurrentHashMap<String, List<URL>>());
404-
categoryNotified = notified.get(url);
405-
}
398+
Map<String, List<URL>> categoryNotified = notified.computeIfAbsent(url, k -> {
399+
return new ConcurrentHashMap<>();
400+
});
406401
for (Map.Entry<String, List<URL>> entry : result.entrySet()) {
407402
String category = entry.getKey();
408403
List<URL> categoryList = entry.getValue();

0 commit comments

Comments
 (0)