Skip to content

Commit 5ae3100

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

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/configuring-topics.adoc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44
If you define a `KafkaAdmin` bean in your application context, it can automatically add topics to the broker.
55
To do so, you can add a `NewTopic` `@Bean` for each topic to the application context.
66
Version 2.3 introduced a new class `TopicBuilder` to make creation of such beans more convenient.
7+
8+
[NOTE]
9+
====
10+
When relying on topic auto-creation by declaring `NewTopic` beans, avoid producing
11+
messages from `@PostConstruct` methods. `@PostConstruct` runs before
12+
`KafkaAdmin` applies `NewTopic` declarations. On a clean broker, this may cause
13+
`UnknownTopicOrPartitionException`.
14+
15+
Instead, consider:
16+
- Using an `ApplicationListener<ContextRefreshedEvent>` to send after the context is fully refreshed.
17+
- Implementing `SmartLifecycle` and starting after `KafkaAdmin`.
18+
- Or pre-creating topics externally.
19+
====
20+
721
The following example shows how to do so:
822

923
[tabs]

0 commit comments

Comments
 (0)