Spring Kafka Consumer, rewind consumer offset to go back 'n' records

守給你的承諾、 提交于 2021-01-29 20:37:25

问题


I'm using "programmatic" way of consuming messages from Kafka topic using org.springframework.kafka.listener.ConcurrentMessageListenerContainer

I'm wondering if there's a "spring" way of rewinding offsets for a specific partitions of a topic to go back 'n' messages?

Would like to know the cleanest way of doing this (programmatically and not using the CLI).


回答1:


If you want to reset the offsets during application startup, use a ConsumerAwareRebalanceListener and perform the seeks on the consumer when the partitions are assigned; you can find the current offset(s) by calling Consumer.position().

If you want to arbitrarily rewind the partitions at runtime, have your listener implement ConsumerSeekAware and grab a reference to the ConsumerSeekCallback.

See this answer for an example of using ConsumerSeekAware.

ConsumerSeekAware also has onIdleContainer() which will be called when no records have been received during idleEventInterval; that callback provides you with the current offsets.



来源:https://stackoverflow.com/questions/56953784/spring-kafka-consumer-rewind-consumer-offset-to-go-back-n-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!