Skip to content

Commit 90c414c

Browse files
committed
GH-3954: Add warning about @PostConstruct timing with NewTopic beans
Signed-off-by: Chaedie <[email protected]>
1 parent 73efd2f commit 90c414c

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ 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
78+
if you rely on automatic topic creation via `NewTopic` beans. `@PostConstruct` runs
79+
before the application context is fully ready, which may cause
80+
`UnknownTopicOrPartitionException` on clean brokers.
81+
82+
Instead, consider:
83+
- Using an `ApplicationListener<ContextRefreshedEvent>` to send after the context is fully refreshed.
84+
- Implementing `SmartLifecycle` and starting after `KafkaAdmin`.
85+
- Or pre-creating topics externally.
86+
====
87+
7588
To use the template, you can configure a producer factory and provide it in the template's constructor.
7689
The following example shows how to do so:
7790

0 commit comments

Comments
 (0)