You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2022/03/24 10:25:04 UTC

[camel] 04/04: [CAMEL-17845][CAMEL-17725] Make singleton services thread safe

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

orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit c7a7f4f346036cf5356c751099c186b8bb846414
Author: John Poth <po...@gmail.com>
AuthorDate: Wed Mar 23 15:12:06 2022 +0100

    [CAMEL-17845][CAMEL-17725] Make singleton services thread safe
---
 .../fhir/services/FhirLocalSingletonContainerService.java   | 11 +++--------
 .../kafka/services/ContainerLocalSingletonKafkaService.java | 13 +++++--------
 2 files changed, 8 insertions(+), 16 deletions(-)

diff --git a/test-infra/camel-test-infra-fhir/src/test/java/org/apache/camel/test/infra/fhir/services/FhirLocalSingletonContainerService.java b/test-infra/camel-test-infra-fhir/src/test/java/org/apache/camel/test/infra/fhir/services/FhirLocalSingletonContainerService.java
index 6860473..41f9e2e 100644
--- a/test-infra/camel-test-infra-fhir/src/test/java/org/apache/camel/test/infra/fhir/services/FhirLocalSingletonContainerService.java
+++ b/test-infra/camel-test-infra-fhir/src/test/java/org/apache/camel/test/infra/fhir/services/FhirLocalSingletonContainerService.java
@@ -21,17 +21,12 @@ import org.junit.jupiter.api.extension.ExtensionContext;
 public class FhirLocalSingletonContainerService extends FhirLocalContainerService
         implements ExtensionContext.Store.CloseableResource {
 
-    private static boolean started;
-
     @Override
     public void beforeAll(ExtensionContext extensionContext) {
-        if (!started) {
-            started = true;
-            // Your "before all tests" startup logic goes here
-            // The following line registers a callback hook when the root test context is shut down
-            extensionContext.getRoot().getStore(ExtensionContext.Namespace.GLOBAL).put("fhir", this);
+        extensionContext.getRoot().getStore(ExtensionContext.Namespace.GLOBAL).getOrComputeIfAbsent("fhir", s -> {
             super.initialize();
-        }
+            return this;
+        });
     }
 
     @Override
diff --git a/test-infra/camel-test-infra-kafka/src/test/java/org/apache/camel/test/infra/kafka/services/ContainerLocalSingletonKafkaService.java b/test-infra/camel-test-infra-kafka/src/test/java/org/apache/camel/test/infra/kafka/services/ContainerLocalSingletonKafkaService.java
index 0e5d974..3f27568 100644
--- a/test-infra/camel-test-infra-kafka/src/test/java/org/apache/camel/test/infra/kafka/services/ContainerLocalSingletonKafkaService.java
+++ b/test-infra/camel-test-infra-kafka/src/test/java/org/apache/camel/test/infra/kafka/services/ContainerLocalSingletonKafkaService.java
@@ -23,7 +23,6 @@ import org.testcontainers.utility.DockerImageName;
 
 public class ContainerLocalSingletonKafkaService extends ContainerLocalKafkaService
         implements ExtensionContext.Store.CloseableResource {
-    private static boolean started;
 
     public ContainerLocalSingletonKafkaService(KafkaContainer container) {
         super(container);
@@ -35,11 +34,10 @@ public class ContainerLocalSingletonKafkaService extends ContainerLocalKafkaServ
 
     @Override
     public void beforeAll(ExtensionContext extensionContext) {
-        if (!started) {
-            started = true;
-            extensionContext.getRoot().getStore(ExtensionContext.Namespace.GLOBAL).put("kafka", this);
+        extensionContext.getRoot().getStore(ExtensionContext.Namespace.GLOBAL).getOrComputeIfAbsent("kafka", s -> {
             super.initialize();
-        }
+            return this;
+        });
     }
 
     @Override
@@ -53,9 +51,8 @@ public class ContainerLocalSingletonKafkaService extends ContainerLocalKafkaServ
     }
 
     public static ContainerLocalSingletonKafkaService kafka3Container() {
-        KafkaContainer container = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.0.1"));
-        container = container.withEmbeddedZookeeper();
-
+        KafkaContainer container
+                = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.0.1")).withEmbeddedZookeeper();
         return new ContainerLocalSingletonKafkaService(container);
     }
 }