0

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).

user1189332
  • 1,773
  • 4
  • 26
  • 46

1 Answers1

0

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.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Thanks for that - Is there anyway I could use the ConsumerSeekAware programatically? (ie without using @KafkaListener annotation). My use case here is using dynamic message listeners. – user1189332 Jul 09 '19 at 16:36
  • Any `MessageListener` can implement `ConsumerSeekAware`; it's not just for `KafkaListener`. The container knows nothing about its listener. – Gary Russell Jul 09 '19 at 17:06