diff --git a/spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/receiving-messages/listener-annotation.adoc b/spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/receiving-messages/listener-annotation.adoc index 8023e4aa67..21c2892d9a 100644 --- a/spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/receiving-messages/listener-annotation.adoc +++ b/spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/receiving-messages/listener-annotation.adoc @@ -80,10 +80,37 @@ public void listen(String data) { } ---- -[[manual-assignment]] -== Explicit Partition Assignment +[[topic-partition-assignment]] +== Topic Partition Assignment -You can also configure POJO listeners with explicit topics and partitions (and, optionally, their initial offsets). +You can configure the topic for `@KafkaListener` in three ways. +You must configure the topic in one of these ways. +[source, java] +---- +@KafkaListener(id = "myListener", topics = "myTopic") +public void listen(String data) { + ... +} + +@KafkaListener(id = "myListener", topicPattern = "my.*") +public void listen(String data) { + ... +} + +@KafkaListener(id = "myListener", topicPartitions = { @TopicPartition(topic = "myTopic", partitions = { "0", "1" })}) +public void listen(String data) { + ... +} +---- + + +You can simply configure the topic directly by name. +In this case, you can also configure the multiple topics like `topics = {"myTopic1", myTopic2"}`. + +You can also configure topics using topicPattern, which enables topic subscription based on a regular expression. + +When you configure topics using either of these ways (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). The following example shows how to do so: [source, java]