使用KafkaListener时,如何判断topic消息是否读完?

When using KafkaListener, how can I check that a topic message has been read to the end?

使用@KafkaListener时,如何判断主题消息是否读完?

请参阅this answer有关在没有更多记录可读时获取事件通知的信息。

可以使用事件中的Consumer获取当前的position(),以及endOffsets();下面显示了如何获取结束偏移量:

@EventListener
void listen(ListenerContainerIdleEvent event) {
    System.out.println(event);
    try {
        System.out.println(event.getConsumer().assignment());
        System.out.println(event.getConsumer().endOffsets(event.getConsumer().assignment(), Duration.ofSeconds(5)));
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}