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

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


The following commit(s) were added to refs/heads/master by this push:
     new ce1fb4cc2b [type : bug] Not first offline from the gateway when 
stopping service (#5501) (#5507)
ce1fb4cc2b is described below

commit ce1fb4cc2b8bc6e243b72a8f457d201149869361
Author: jerbo99 <[email protected]>
AuthorDate: Wed Apr 3 17:31:24 2024 +0800

    [type : bug] Not first offline from the gateway when stopping service 
(#5501) (#5507)
    
    Co-authored-by: xiaoyu <[email protected]>
---
 .../core/shutdown/ShenyuClientShutdownHook.java     | 19 ++++---------------
 .../client/core/shutdown/ShutdownHookManager.java   | 21 ++++++++++++++++++++-
 2 files changed, 24 insertions(+), 16 deletions(-)

diff --git 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/shutdown/ShenyuClientShutdownHook.java
 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/shutdown/ShenyuClientShutdownHook.java
index e784f6a87a..04720181e8 100644
--- 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/shutdown/ShenyuClientShutdownHook.java
+++ 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/shutdown/ShenyuClientShutdownHook.java
@@ -24,7 +24,6 @@ import java.util.Objects;
 import java.util.Properties;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.shenyu.register.client.api.ShenyuClientRegisterRepository;
 import org.apache.shenyu.register.common.config.ShenyuRegisterCenterConfig;
@@ -40,10 +39,6 @@ public class ShenyuClientShutdownHook {
 
     private static final AtomicBoolean DELAY = new AtomicBoolean(false);
 
-    private static String hookNamePrefix = "ShenyuClientShutdownHook";
-
-    private static AtomicInteger hookId = new AtomicInteger(0);
-
     private static Properties props;
 
     private static IdentityHashMap<Thread, Thread> delayHooks = new 
IdentityHashMap<>();
@@ -54,10 +49,7 @@ public class ShenyuClientShutdownHook {
     }
 
     public ShenyuClientShutdownHook(final ShenyuClientRegisterRepository 
repository, final ShenyuRegisterCenterConfig config) {
-        String name = String.join("-", hookNamePrefix, 
String.valueOf(hookId.incrementAndGet()));
-        ShutdownHookManager.get().addShutdownHook(new 
Thread(repository::closeRepository, name), 1);
-        LOG.info("Add hook {}", name);
-        ShenyuClientShutdownHook.props = config.getProps();
+        set(repository, config.getProps());
     }
 
     /**
@@ -67,9 +59,7 @@ public class ShenyuClientShutdownHook {
      * @param props  Properties
      */
     public static void set(final ShenyuClientRegisterRepository repository, 
final Properties props) {
-        String name = String.join("-", hookNamePrefix, 
String.valueOf(hookId.incrementAndGet()));
-        ShutdownHookManager.get().addShutdownHook(new 
Thread(repository::closeRepository, name), 1);
-        LOG.info("Add hook {}", name);
+        ShutdownHookManager.get().addShutdownHook(new 
Thread(repository::closeRepository), 1);
         ShenyuClientShutdownHook.props = props;
     }
 
@@ -105,7 +95,7 @@ public class ShenyuClientShutdownHook {
             while (System.currentTimeMillis() - s < delayOtherHooksExecTime) {
                 for (Iterator<Thread> iterator = 
Objects.requireNonNull(hooks).keySet().iterator(); iterator.hasNext();) {
                     Thread hook = iterator.next();
-                    if (hook.getName().startsWith(hookNamePrefix)) {
+                    if 
(hook.getName().equals(ShutdownHookManager.getHookName())) {
                         continue;
                     }
                     if (delayHooks.containsKey(hook) || 
delayedHooks.containsKey(hook)) {
@@ -138,8 +128,7 @@ public class ShenyuClientShutdownHook {
                 }
             }
 
-            hookNamePrefix = null;
-            hookId = new AtomicInteger(0);
+            ShutdownHookManager.clearHookName();
             props = null;
             delayHooks = null;
             delayedHooks = null;
diff --git 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/shutdown/ShutdownHookManager.java
 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/shutdown/ShutdownHookManager.java
index bbe10a5e49..61e9b22513 100644
--- 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/shutdown/ShutdownHookManager.java
+++ 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/shutdown/ShutdownHookManager.java
@@ -37,6 +37,8 @@ public final class ShutdownHookManager {
 
     private static final Logger LOG = 
LoggerFactory.getLogger(ShutdownHookManager.class);
 
+    private static String hookName = "ShenyuClientShutdownHook";
+
     private final Set<HookEntry> hooks =
             Collections.synchronizedSet(new HashSet<HookEntry>());
 
@@ -57,8 +59,9 @@ public final class ShutdownHookManager {
                             LOG.error(ex.getMessage(), ex);
                         }
                     }
-                })
+                }, getHookName())
         );
+        LOG.info("Add hook {}", getHookName());
     }
 
     /**
@@ -162,6 +165,22 @@ public final class ShutdownHookManager {
         hooks.clear();
     }
 
+    /**
+     * Returns client shutdown hook name.
+     *
+     * @return client shutdown hook name
+     */
+    public static String getHookName() {
+        return hookName;
+    }
+
+    /**
+     * clear client shutdown hook name.
+     */
+    public static void clearHookName() {
+        hookName = null;
+    }
+
     /**
      * Private structure to store ShutdownHook and its priority.
      */

Reply via email to