From 579e7cb8ff21e47aa99127686e07ee5b15dd05db 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. **Auto-cherry-pick to `3.3.x` & `3.2.x`** Signed-off-by: mipo256 --- .../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 5948e41d..9504bf1c 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 @@ -162,21 +162,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(); - } - } ----