You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2023/06/16 06:20:07 UTC

[servicecomb-java-chassis] branch 2.8.x updated: [SCB-2796]fix running not set problem

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

liubao pushed a commit to branch 2.8.x
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/2.8.x by this push:
     new 4a7efbff1 [SCB-2796]fix running not set problem
4a7efbff1 is described below

commit 4a7efbff10a05415f72b670531aa7a5b5f4e71ca
Author: liubao <bi...@qq.com>
AuthorDate: Fri Jun 16 14:19:04 2023 +0800

    [SCB-2796]fix running not set problem
---
 .../servicecomb/serviceregistry/RegistryUtils.java   | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java
index fe16cd4b6..72022f6a1 100644
--- a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java
+++ b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java
@@ -22,6 +22,7 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.function.Consumer;
 import java.util.function.Function;
@@ -68,7 +69,7 @@ public final class RegistryUtils {
    */
   private static volatile ServiceRegistry serviceRegistry;
 
-  private static volatile boolean running = false;
+  private static AtomicBoolean running = new AtomicBoolean(false);
 
   private static final Map<String, ServiceRegistry> EXTRA_SERVICE_REGISTRIES = new LinkedHashMap<>();
 
@@ -121,19 +122,20 @@ public final class RegistryUtils {
   }
 
   public static void run() {
-    if (running) {
-      if (DynamicPropertyFactory.getInstance()
-          .getBooleanProperty(SERVICECOMB_SERVICE_REGISTRY_REPEATED_INITIALIZATION_ALLOWED, false).get()) {
-        return;
-      }
-      throw new IllegalStateException("Registry has already bean initialized and not allowed to initialize twice.");
+    if (running.compareAndSet(false, true)) {
+      executeOnEachServiceRegistry(ServiceRegistry::run);
+      return;
+    }
+    if (DynamicPropertyFactory.getInstance()
+        .getBooleanProperty(SERVICECOMB_SERVICE_REGISTRY_REPEATED_INITIALIZATION_ALLOWED, false).get()) {
+      return;
     }
-    executeOnEachServiceRegistry(ServiceRegistry::run);
+    throw new IllegalStateException("Registry has already bean initialized and not allowed to initialize twice.");
   }
 
   public static void destroy() {
+    running.set(false);
     executeOnEachServiceRegistry(ServiceRegistry::destroy);
-    running = false;
     if (serviceRegistry != null) {
       serviceRegistry = null;
     }