|
22 | 22 | import java.util.List; |
23 | 23 | import java.util.Map; |
24 | 24 | import java.util.Objects; |
| 25 | +import java.util.concurrent.atomic.AtomicBoolean; |
25 | 26 | import java.util.concurrent.atomic.AtomicInteger; |
26 | 27 | import java.util.function.Consumer; |
27 | 28 | import java.util.function.Function; |
@@ -68,7 +69,7 @@ public final class RegistryUtils { |
68 | 69 | */ |
69 | 70 | private static volatile ServiceRegistry serviceRegistry; |
70 | 71 |
|
71 | | - private static volatile boolean running = false; |
| 72 | + private static AtomicBoolean running = new AtomicBoolean(false); |
72 | 73 |
|
73 | 74 | private static final Map<String, ServiceRegistry> EXTRA_SERVICE_REGISTRIES = new LinkedHashMap<>(); |
74 | 75 |
|
@@ -121,19 +122,20 @@ private static void initializeServiceRegistries(Configuration configuration) { |
121 | 122 | } |
122 | 123 |
|
123 | 124 | 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; |
130 | 132 | } |
131 | | - executeOnEachServiceRegistry(ServiceRegistry::run); |
| 133 | + throw new IllegalStateException("Registry has already bean initialized and not allowed to initialize twice."); |
132 | 134 | } |
133 | 135 |
|
134 | 136 | public static void destroy() { |
| 137 | + running.set(false); |
135 | 138 | executeOnEachServiceRegistry(ServiceRegistry::destroy); |
136 | | - running = false; |
137 | 139 | if (serviceRegistry != null) { |
138 | 140 | serviceRegistry = null; |
139 | 141 | } |
|
0 commit comments