Spring kafka consumer, seek offset at runtime?
See Seeking to a Specific Offset.
In order to seek, your listener must implement
ConsumerSeekAware
which has the following methods:
void registerSeekCallback(ConsumerSeekCallback callback);
void onPartitionsAssigned(Map<TopicPartition, Long> assignments, ConsumerSeekCallback callback);
void onIdleContainer(Map<TopicPartition, Long> assignments, ConsumerSeekCallback callback);
The first is called when the container is started; this callback should be used when seeking at some arbitrary time after initialization. You should save a reference to the callback; if you are using the same listener in multiple containers (or in a
ConcurrentMessageListenerContainer
) you should store the callback in aThreadLocal
or some other structure keyed by the listener Thread.