You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@seata.apache.org by ji...@apache.org on 2024/08/26 03:49:43 UTC

(incubator-seata) branch 2.x updated: optimize: optimize the reflection operation in class `SerializerServiceLoader` (#6780)

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

jianbin pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/incubator-seata.git


The following commit(s) were added to refs/heads/2.x by this push:
     new 8af2d8448b optimize: optimize the reflection operation in class `SerializerServiceLoader` (#6780)
8af2d8448b is described below

commit 8af2d8448b7958eef2c69f6c3ffd99ac7ca23dc5
Author: 王良 <84...@qq.com>
AuthorDate: Mon Aug 26 11:49:38 2024 +0800

    optimize: optimize the reflection operation in class `SerializerServiceLoader` (#6780)
---
 changes/en-us/2.x.md                                      |  1 +
 changes/zh-cn/2.x.md                                      |  1 +
 .../java/org/apache/seata/common/util/ReflectionUtil.java | 15 +++++++++++++++
 .../org/apache/seata/common/util/ReflectionUtilTest.java  |  6 ++++++
 .../seata/core/serializer/SerializerServiceLoader.java    | 13 ++++++-------
 5 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md
index c189f9cc0c..e9a57c4bf9 100644
--- a/changes/en-us/2.x.md
+++ b/changes/en-us/2.x.md
@@ -64,6 +64,7 @@ Add changes here for all PR submitted to the 2.x branch.
 - [[#6761](https://github.com/apache/incubator-seata/pull/6761)] optimize the namingserver code to improve readability
 - [[#6768](https://github.com/apache/incubator-seata/pull/6768)] report the tcc fence transaction isolation level
 - [[#6770](https://github.com/apache/incubator-seata/pull/6770)] Automatic deletion of namingserver vgroup through Caffeine map
+- [[#6780](https://github.com/apache/incubator-seata/pull/6780)] optimize the reflection operation in class `SerializerServiceLoader`
 
 ### refactor:
 
diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md
index 1ccc432ae6..d38a48d2d8 100644
--- a/changes/zh-cn/2.x.md
+++ b/changes/zh-cn/2.x.md
@@ -65,6 +65,7 @@
 - [[#6761](https://github.com/apache/incubator-seata/pull/6761)] 提升namingserver manager代码可读性
 - [[#6768](https://github.com/apache/incubator-seata/pull/6768)] 上报tcc fence事务隔离级别
 - [[#6770](https://github.com/apache/incubator-seata/pull/6770)] 通过caffeine map支持namingserver事务分组的过期删除
+- [[#6780](https://github.com/apache/incubator-seata/pull/6780)] 优化类 `SerializerServiceLoader` 中的反射操作
 
 
 ### refactor:
diff --git a/common/src/main/java/org/apache/seata/common/util/ReflectionUtil.java b/common/src/main/java/org/apache/seata/common/util/ReflectionUtil.java
index b9ba762bef..5da00a4b61 100644
--- a/common/src/main/java/org/apache/seata/common/util/ReflectionUtil.java
+++ b/common/src/main/java/org/apache/seata/common/util/ReflectionUtil.java
@@ -89,6 +89,21 @@ public final class ReflectionUtil {
         return Class.forName(className, true, Thread.currentThread().getContextClassLoader());
     }
 
+    /**
+     * Is class present boolean.
+     *
+     * @param className the class name
+     * @return boolean true if the class is present
+     */
+    public static boolean isClassPresent(String className) {
+        try {
+            Class.forName(className, false, Thread.currentThread().getContextClassLoader());
+            return true;
+        } catch (ClassNotFoundException e) {
+            return false;
+        }
+    }
+
     /**
      * Get the wrapped class
      *
diff --git a/common/src/test/java/org/apache/seata/common/util/ReflectionUtilTest.java b/common/src/test/java/org/apache/seata/common/util/ReflectionUtilTest.java
index 20f22c8be7..7a1c390c14 100644
--- a/common/src/test/java/org/apache/seata/common/util/ReflectionUtilTest.java
+++ b/common/src/test/java/org/apache/seata/common/util/ReflectionUtilTest.java
@@ -45,6 +45,12 @@ public class ReflectionUtilTest {
                 ReflectionUtil.getClassByName("java.lang.String"));
     }
 
+    @Test
+    public void testIsClassPresent() {
+        Assertions.assertTrue(ReflectionUtil.isClassPresent("java.lang.String"));
+        Assertions.assertFalse(ReflectionUtil.isClassPresent("java.lang.String2"));
+    }
+
     @Test
     public void testGetWrappedClass() {
         Assertions.assertEquals(Byte.class, ReflectionUtil.getWrappedClass(byte.class));
diff --git a/core/src/main/java/org/apache/seata/core/serializer/SerializerServiceLoader.java b/core/src/main/java/org/apache/seata/core/serializer/SerializerServiceLoader.java
index 0503fa8f73..63ce440edd 100644
--- a/core/src/main/java/org/apache/seata/core/serializer/SerializerServiceLoader.java
+++ b/core/src/main/java/org/apache/seata/core/serializer/SerializerServiceLoader.java
@@ -56,6 +56,7 @@ public final class SerializerServiceLoader {
     }
 
     private static final String PROTOBUF_SERIALIZER_CLASS_NAME = "org.apache.seata.serializer.protobuf.ProtobufSerializer";
+    private static final boolean CONTAINS_PROTOBUF_DEPENDENCY = ReflectionUtil.isClassPresent(PROTOBUF_SERIALIZER_CLASS_NAME);
 
     /**
      * Load the service of {@link Serializer}
@@ -65,15 +66,13 @@ public final class SerializerServiceLoader {
      * @throws EnhancedServiceNotFoundException the enhanced service not found exception
      */
     public static Serializer load(SerializerType type, byte version) throws EnhancedServiceNotFoundException {
-        if (type == SerializerType.PROTOBUF) {
-            try {
-                ReflectionUtil.getClassByName(PROTOBUF_SERIALIZER_CLASS_NAME);
-            } catch (ClassNotFoundException e) {
-                throw new EnhancedServiceNotFoundException("'ProtobufSerializer' not found. " +
-                        "Please manually reference 'org.apache.seata:seata-serializer-protobuf' dependency ", e);
-            }
+        // The following code is only used to kindly prompt users to add missing dependencies.
+        if (type == SerializerType.PROTOBUF && !CONTAINS_PROTOBUF_DEPENDENCY) {
+            throw new EnhancedServiceNotFoundException("The class '" + PROTOBUF_SERIALIZER_CLASS_NAME + "' not found. " +
+                    "Please manually reference 'org.apache.seata:seata-serializer-protobuf' dependency.");
         }
 
+
         String key = serialzerKey(type, version);
         Serializer serializer = SERIALIZER_MAP.get(key);
         if (serializer == null) {


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org