I was trying to play with seeking of timestamp functionality but for some reason, it's not working for me.
In my producer I have next code:
ProducerRecord<String, Obj> producer = new ProducerRecord<>("topic", 0, System.currentTimeMillis() - 10000, "key", obj);
kafkaTemplate.send(producer);
And in my Kafka listener I was trying to seek offsets from some timestamp that is higher than the above one:
@Component
@RequiredArgsConstructor
@KafkaListener(id = "container",
topics = "topic",
clientIdPrefix = "init_client",
autoStartup = "true")
public class KafkaList implements ConsumerSeekAware {
@Override
public void onPartitionsAssigned(Map<TopicPartition, Long> assignments, ConsumerSeekCallback callback) {
long timestamp = System.currentTimeMillis()+60*1000;
log.info("Search for a time that is great or equal then {}", timestamp);
callback.seekToTimestamp(new ArrayList<>(assignments.keySet()), timestamp);
}
@KafkaHandler
public void listen(@Header(KafkaHeaders.RECEIVED_MESSAGE_KEY) String key, Obj obj,
@Header(KafkaHeaders.RECEIVED_TIMESTAMP) Long timestamp) {
log.info("Received message timestamp: {}, date: {}", timestamp,
Instant.ofEpochMilli(timestamp).atZone(ZoneId.systemDefault()).toLocalDate());
}
}
In logs I see the next output:
Search for a time that is great or equal then 1613079865328
Received message timestamp: 1613079798676, date: 2021-02-11
The timestamp value in Kafka topic 1613079798676 is lower than my search value 1613079865328 so why consumer picked up this offset?