From 8d9285b5158d9276916baf61d9210996da9cf829 Mon Sep 17 00:00:00 2001 From: chenyong Date: Fri, 30 Sep 2022 20:56:24 +0800 Subject: [PATCH 1/2] fix duplicate class loading --- .../org/apache/rocketmq/common/utils/ServiceProvider.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/common/src/main/java/org/apache/rocketmq/common/utils/ServiceProvider.java b/common/src/main/java/org/apache/rocketmq/common/utils/ServiceProvider.java index 30a6b808f39..1a495f706a8 100644 --- a/common/src/main/java/org/apache/rocketmq/common/utils/ServiceProvider.java +++ b/common/src/main/java/org/apache/rocketmq/common/utils/ServiceProvider.java @@ -105,24 +105,22 @@ protected static InputStream getResourceAsStream(ClassLoader loader, String name public static List load(String name, Class clazz) { LOG.info("Looking for a resource file of name [{}] ...", name); - List services = new ArrayList(); + List services = new ArrayList<>(); try { - ArrayList names = new ArrayList(); final InputStream is = getResourceAsStream(getContextClassLoader(), name); if (is != null) { BufferedReader reader; reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)); String serviceName = reader.readLine(); + List names = new ArrayList<>(); while (serviceName != null && !"".equals(serviceName)) { LOG.info( "Creating an instance as specified by file {} which was present in the path of the context classloader.", name); if (!names.contains(serviceName)) { names.add(serviceName); + services.add((T) initService(getContextClassLoader(), serviceName, clazz)); } - - services.add((T) initService(getContextClassLoader(), serviceName, clazz)); - serviceName = reader.readLine(); } reader.close(); From db11f6bd639e2e4777f1c7175f6bab8d98d28a5d Mon Sep 17 00:00:00 2001 From: chenyong Date: Fri, 30 Sep 2022 21:14:21 +0800 Subject: [PATCH 2/2] remove redundant --- .../java/org/apache/rocketmq/common/utils/ServiceProvider.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/main/java/org/apache/rocketmq/common/utils/ServiceProvider.java b/common/src/main/java/org/apache/rocketmq/common/utils/ServiceProvider.java index 1a495f706a8..49f29a89d94 100644 --- a/common/src/main/java/org/apache/rocketmq/common/utils/ServiceProvider.java +++ b/common/src/main/java/org/apache/rocketmq/common/utils/ServiceProvider.java @@ -119,7 +119,7 @@ public static List load(String name, Class clazz) { name); if (!names.contains(serviceName)) { names.add(serviceName); - services.add((T) initService(getContextClassLoader(), serviceName, clazz)); + services.add(initService(getContextClassLoader(), serviceName, clazz)); } serviceName = reader.readLine(); }