From 90c414c516b47bdd13adddbdb23fa2d1f90f918e Mon Sep 17 00:00:00 2001 From: Chaedie Date: Sun, 31 Aug 2025 01:17:13 +0900 Subject: [PATCH 1/2] GH-3954: Add warning about @PostConstruct timing with NewTopic beans Signed-off-by: Chaedie --- .../modules/ROOT/pages/kafka/sending-messages.adoc | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/sending-messages.adoc b/spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/sending-messages.adoc index 3ddf88477a..a95c1db92c 100644 --- a/spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/sending-messages.adoc +++ b/spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/sending-messages.adoc @@ -72,6 +72,19 @@ If the topic is configured to use `LOG_APPEND_TIME`, the user-specified timestam The `metrics` and `partitionsFor` methods delegate to the same methods on the underlying javadoc:org.apache.kafka.clients.producer.Producer[]. The `execute` method provides direct access to the underlying javadoc:org.apache.kafka.clients.producer.Producer[]. +[NOTE] +==== +When sending messages from Spring components, avoid using `@PostConstruct` methods +if you rely on automatic topic creation via `NewTopic` beans. `@PostConstruct` runs +before the application context is fully ready, which may cause +`UnknownTopicOrPartitionException` on clean brokers. + +Instead, consider: +- Using an `ApplicationListener` to send after the context is fully refreshed. +- Implementing `SmartLifecycle` and starting after `KafkaAdmin`. +- Or pre-creating topics externally. +==== + To use the template, you can configure a producer factory and provide it in the template's constructor. The following example shows how to do so: From 0f8b7d925029d53339367174eef47db2e720b158 Mon Sep 17 00:00:00 2001 From: Chaedie Date: Wed, 3 Sep 2025 13:29:30 +0900 Subject: [PATCH 2/2] Docs: Improve messaging component guidance based on review feedback Signed-off-by: Chaedie --- .../ROOT/pages/kafka/sending-messages.adoc | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/sending-messages.adoc b/spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/sending-messages.adoc index a95c1db92c..6380823169 100644 --- a/spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/sending-messages.adoc +++ b/spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/sending-messages.adoc @@ -74,15 +74,14 @@ The `execute` method provides direct access to the underlying javadoc:org.apache [NOTE] ==== -When sending messages from Spring components, avoid using `@PostConstruct` methods -if you rely on automatic topic creation via `NewTopic` beans. `@PostConstruct` runs -before the application context is fully ready, which may cause -`UnknownTopicOrPartitionException` on clean brokers. - -Instead, consider: -- Using an `ApplicationListener` to send after the context is fully refreshed. -- Implementing `SmartLifecycle` and starting after `KafkaAdmin`. -- Or pre-creating topics externally. +When sending messages from Spring components, avoid using `@PostConstruct` methods if relying on automatic topic creation via `NewTopic` beans. +`@PostConstruct` runs before the application context is fully ready, which may cause `UnknownTopicOrPartitionException` on clean brokers. + +Instead, consider these alternatives: + +- Use an `ApplicationListener` to ensure the context is fully refreshed before sending. +- Implement `SmartLifecycle` to start after the `KafkaAdmin` bean has completed its initialization. +- Pre-create topics externally. ==== To use the template, you can configure a producer factory and provide it in the template's constructor.