Skip to content

Commit f0324da

Browse files
authored
GH-3954: Add docs warning about @PostConstruct & NewTopic beans
Fixes: github.com//issues/3954 * Docs: Improve messaging component guidance based on review feedback Signed-off-by: Chaedie <[email protected]>
1 parent 73efd2f commit f0324da

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/sending-messages.adoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ If the topic is configured to use `LOG_APPEND_TIME`, the user-specified timestam
7272
The `metrics` and `partitionsFor` methods delegate to the same methods on the underlying javadoc:org.apache.kafka.clients.producer.Producer[].
7373
The `execute` method provides direct access to the underlying javadoc:org.apache.kafka.clients.producer.Producer[].
7474

75+
[NOTE]
76+
====
77+
When sending messages from Spring components, avoid using `@PostConstruct` methods if relying on automatic topic creation via `NewTopic` beans.
78+
`@PostConstruct` runs before the application context is fully ready, which may cause `UnknownTopicOrPartitionException` on clean brokers.
79+
80+
Instead, consider these alternatives:
81+
82+
- Use an `ApplicationListener<ContextRefreshedEvent>` to ensure the context is fully refreshed before sending.
83+
- Implement `SmartLifecycle` to start after the `KafkaAdmin` bean has completed its initialization.
84+
- Pre-create topics externally.
85+
====
86+
7587
To use the template, you can configure a producer factory and provide it in the template's constructor.
7688
The following example shows how to do so:
7789

0 commit comments

Comments
 (0)