I am trying to implement a consumer with @KafkaListener.
I am using Spring 2.3.7 version.
Here is my code so far,
public class SampleListener {
@KafkaListener(topics = "test-topic",
containerFactory = "sampleKafkaListenerContainerFactory",
groupId = "test-group")
public void onMessage(@Payload String message,
@Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partition,
@Header(KafkaHeaders.RECEIVED_TOPIC) String topic,
@Header(KafkaHeaders.RECEIVED_TIMESTAMP) long receivedTimestamp,
@Header(KafkaHeaders.OFFSET) long offset,
@Headers MessageHeaders messageHeaders) {
LOGGER.info("Received Message for topic={} partition={} offset={} messageHeaders={}",
topic, partition, offset, messageHeaders);
LOGGER.debug("Received Message payload={}", message);
doSomething(message);
}
}
I am new to Kafka and Spring. I read the spring-kafka docs on how to seek offsets but not able to understand completely.
My understanding, for my use case I don't want to read the events again when partitions are assigned to a container or in any other scenarios (Ensuring read only once).
I see most Consumer implementations implements ConsumerSeekAware. I know implementing ConsumerSeekAware gives us the ability to seek offset on events like onIdleContainer or onPartitionsAssigned. I am not able to understand what are the scenarios being handled with these?
What are the scenarios
ConsumerSeekAwareis implemented to handle? What are the best practices or general scenarios in implementing Kafka Consumer which require seeking offset?What is the difference between
registerSeekCallbackandonPartitionsAssigned? For both it says they are called whenever partitions are assigned. What is the difference between callBack for both these methods?