Skip to content

Commit b942c4b

Browse files
committed
add topic assignment @KafkaListener docs
Signed-off-by: moonyoungCHAE <xpf_fl@naver.com>
1 parent 6999a18 commit b942c4b

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/receiving-messages/listener-annotation.adoc

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,34 @@ public void listen(String data) {
8080
}
8181
----
8282

83-
[[manual-assignment]]
84-
== Explicit Partition Assignment
83+
[[topic-partition-assignment]]
84+
== Topic Partition Assignment
8585

86-
You can also configure POJO listeners with explicit topics and partitions (and, optionally, their initial offsets).
86+
You can configure the topic for `@KafkaListener` in three ways. You must configure the topic in one of these ways.
87+
[source, java]
88+
----
89+
@KafkaListener(id = "myListener", topics = "myTopic")
90+
public void listen(String data) {
91+
...
92+
}
93+
94+
@KafkaListener(id = "myListener", topicPattern = "my.*")
95+
public void listen(String data) {
96+
...
97+
}
98+
99+
@KafkaListener(id = "myListener", topicPartitions = { @TopicPartition(topic = "myTopic", partitions = { "0", "1" })})
100+
public void listen(String data) {
101+
...
102+
}
103+
----
104+
105+
106+
You can simply configure the topic directly by name. In this case, you can also configure the multiple topics like `topics = {"myTopic1", myTopic2"}`.
107+
108+
You can also configure topics using topicPattern, which enables topic subscription based on a regular expression.
109+
110+
When you configure topics using either of these methods (topic or topic pattern), Kafka automatically assigns partitions according to the consumer group. Alternatively, you can configure POJO listeners with explicit topics and partitions (and, optionally, their initial offsets).
87111
The following example shows how to do so:
88112

89113
[source, java]

0 commit comments

Comments
 (0)