You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by ji...@apache.org on 2022/09/08 06:18:50 UTC

[pulsar] branch master updated: [refactor][broker] Refactor reflection method in delayed and service module (#17347)

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

jianghaiting pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new 2848fa0da09 [refactor][broker] Refactor reflection method in delayed and service module (#17347)
2848fa0da09 is described below

commit 2848fa0da09e035951220c3d04138041e1477e60
Author: Qiang Huang <HQ...@users.noreply.github.com>
AuthorDate: Thu Sep 8 14:18:43 2022 +0800

    [refactor][broker] Refactor reflection method in delayed and service module (#17347)
---
 .../pulsar/broker/delayed/DelayedDeliveryTrackerLoader.java  | 12 ++++--------
 .../pulsar/broker/service/schema/SchemaRegistryService.java  |  8 ++++----
 2 files changed, 8 insertions(+), 12 deletions(-)

diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/DelayedDeliveryTrackerLoader.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/DelayedDeliveryTrackerLoader.java
index adc4a0dc044..91a56b51a54 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/DelayedDeliveryTrackerLoader.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/DelayedDeliveryTrackerLoader.java
@@ -18,23 +18,19 @@
  */
 package org.apache.pulsar.broker.delayed;
 
-import static com.google.common.base.Preconditions.checkArgument;
 import java.io.IOException;
 import lombok.experimental.UtilityClass;
 import org.apache.pulsar.broker.ServiceConfiguration;
+import org.apache.pulsar.common.util.Reflections;
 
 @UtilityClass
 public class DelayedDeliveryTrackerLoader {
     public static DelayedDeliveryTrackerFactory loadDelayedDeliveryTrackerFactory(ServiceConfiguration conf)
             throws IOException {
-        Class<?> factoryClass;
         try {
-            factoryClass = Class.forName(conf.getDelayedDeliveryTrackerFactoryClassName());
-            Object obj = factoryClass.getDeclaredConstructor().newInstance();
-            checkArgument(obj instanceof DelayedDeliveryTrackerFactory,
-                    "The factory has to be an instance of " + DelayedDeliveryTrackerFactory.class.getName());
-
-            DelayedDeliveryTrackerFactory factory = (DelayedDeliveryTrackerFactory) obj;
+            DelayedDeliveryTrackerFactory factory =
+                    Reflections.createInstance(conf.getDelayedDeliveryTrackerFactoryClassName(),
+                            DelayedDeliveryTrackerFactory.class, Thread.currentThread().getContextClassLoader());
             factory.initialize(conf);
             return factory;
         } catch (Exception e) {
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryService.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryService.java
index 4668ba62850..e78477a9aa0 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryService.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryService.java
@@ -25,6 +25,7 @@ import java.util.concurrent.ScheduledExecutorService;
 import org.apache.pulsar.broker.service.schema.validator.SchemaRegistryServiceWithSchemaDataValidator;
 import org.apache.pulsar.common.protocol.schema.SchemaStorage;
 import org.apache.pulsar.common.schema.SchemaType;
+import org.apache.pulsar.common.util.Reflections;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -35,10 +36,9 @@ public interface SchemaRegistryService extends SchemaRegistry {
     static Map<SchemaType, SchemaCompatibilityCheck> getCheckers(Set<String> checkerClasses) throws Exception {
         Map<SchemaType, SchemaCompatibilityCheck> checkers = Maps.newHashMap();
         for (String className : checkerClasses) {
-            final Class<?> checkerClass = Class.forName(className);
-            SchemaCompatibilityCheck instance = (SchemaCompatibilityCheck) checkerClass
-                    .getDeclaredConstructor().newInstance();
-            checkers.put(instance.getSchemaType(), instance);
+            SchemaCompatibilityCheck schemaCompatibilityCheck = Reflections.createInstance(className,
+                    SchemaCompatibilityCheck.class, Thread.currentThread().getContextClassLoader());
+            checkers.put(schemaCompatibilityCheck.getSchemaType(), schemaCompatibilityCheck);
         }
         return checkers;
     }