From 110a3ce1cbde4964f94b9fa7bd1cb984d22d0947 Mon Sep 17 00:00:00 2001 From: Mikhail Polivakha <68962645+mipo256@users.noreply.github.com> Date: Wed, 28 May 2025 19:15:43 +0300 Subject: [PATCH] Altered the example in EmbeddedKafkaHolder usage (#3929) Integration tests with an EmbeddedKafka instance are sometimes run in parallel in Junit. The current example is not thread safe, so I think the oficial documentation should account for that. Signed-off-by: mipo256 (cherry picked from commit 579e7cb8ff21e47aa99127686e07ee5b15dd05db) --- .../antora/modules/ROOT/pages/testing.adoc | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc b/spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc index 052e5efa..b19eef30 100644 --- a/spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc +++ b/spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc @@ -180,21 +180,20 @@ public final class EmbeddedKafkaHolder { public static EmbeddedKafkaBroker getEmbeddedKafka() { if (!started) { - try { - embeddedKafka.afterPropertiesSet(); - } - catch (Exception e) { - throw new KafkaException("Embedded broker failed to start", e); - } - started = true; + synchronized (this) { + if (!started) { + try { + embeddedKafka.afterPropertiesSet(); + } + catch (Exception e) { + throw new KafkaException("Embedded broker failed to start", e); + } + started = true; + } + } } return embeddedKafka; } - - private EmbeddedKafkaHolder() { - super(); - } - } ----