Skip to content

Commit 4a7efbf

Browse files
committed
[SCB-2796]fix running not set problem
1 parent 39b2eb9 commit 4a7efbf

File tree

1 file changed

+11
-9
lines changed
  • service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry

1 file changed

+11
-9
lines changed

service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323
import java.util.Map;
2424
import java.util.Objects;
25+
import java.util.concurrent.atomic.AtomicBoolean;
2526
import java.util.concurrent.atomic.AtomicInteger;
2627
import java.util.function.Consumer;
2728
import java.util.function.Function;
@@ -68,7 +69,7 @@ public final class RegistryUtils {
6869
*/
6970
private static volatile ServiceRegistry serviceRegistry;
7071

71-
private static volatile boolean running = false;
72+
private static AtomicBoolean running = new AtomicBoolean(false);
7273

7374
private static final Map<String, ServiceRegistry> EXTRA_SERVICE_REGISTRIES = new LinkedHashMap<>();
7475

@@ -121,19 +122,20 @@ private static void initializeServiceRegistries(Configuration configuration) {
121122
}
122123

123124
public static void run() {
124-
if (running) {
125-
if (DynamicPropertyFactory.getInstance()
126-
.getBooleanProperty(SERVICECOMB_SERVICE_REGISTRY_REPEATED_INITIALIZATION_ALLOWED, false).get()) {
127-
return;
128-
}
129-
throw new IllegalStateException("Registry has already bean initialized and not allowed to initialize twice.");
125+
if (running.compareAndSet(false, true)) {
126+
executeOnEachServiceRegistry(ServiceRegistry::run);
127+
return;
128+
}
129+
if (DynamicPropertyFactory.getInstance()
130+
.getBooleanProperty(SERVICECOMB_SERVICE_REGISTRY_REPEATED_INITIALIZATION_ALLOWED, false).get()) {
131+
return;
130132
}
131-
executeOnEachServiceRegistry(ServiceRegistry::run);
133+
throw new IllegalStateException("Registry has already bean initialized and not allowed to initialize twice.");
132134
}
133135

134136
public static void destroy() {
137+
running.set(false);
135138
executeOnEachServiceRegistry(ServiceRegistry::destroy);
136-
running = false;
137139
if (serviceRegistry != null) {
138140
serviceRegistry = null;
139141
}

0 commit comments

Comments
 (0)