How can I start reading the topic from the beginning every time my application starts, if I have a @KafkaListener annotation ?
One way is to use an anonymous group management: no id() and no groupId() for that @KafkaListener. If idIsGroup() == true (default), of course.
And you also need use a @TopicPartition like explained here: https://docs.spring.io/spring-kafka/docs/2.6.2/reference/html/#manual-assignment
Starting with version 2.5.5, you can apply an initial offset to all assigned partitions:
topicPartitions = { @TopicPartition(topic = "topic1",
partitionOffsets = @PartitionOffset(partition = "*", initialOffset = "0"))
})
Another way is to implement an AbstractConsumerSeekAware for your @KafkaListener class and call that ConsumerSeekCallback.seekToBeginning() when you get partitions assigned in the onPartitionsAssigned(Map<TopicPartition, Long> assignments, ConsumerSeekCallback callback).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With