问题
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