我正在使用具有以下设置的KafkaConsumer
enable.auto.commit
= true
auto.commit.interval.ms
= 2000
..并每5秒轮询一次。我有一个虚拟的ConsumerInterceptor
实现
@Override
public void onCommit(Map<TopicPartition, OffsetAndMetadata> map) {
System.out.println("onCommit() invoked with records - " + map.size());
Set<Map.Entry<TopicPartition, OffsetAndMetadata>> committedEntries = map.entrySet();
for (Map.Entry<TopicPartition, OffsetAndMetadata> committedEntry : committedEntries) {
System.out.println("committedRecordTopic " + committedEntry.getKey().topic());
System.out.println("committedRecordPartition " + committedEntry.getKey().partition());
System.out.println("committedRecordOffset " + committedEntry.getValue().offset());
}
}
每当我向Kafka发送记录时,拦截器的onCommit
方法都会被反复调用,并且我一次又一次地看到这种输出(按照上面的拦截器)
onCommit() invoked with records - 1
committedRecordTopic foo
committedRecordPartition 0
committedRecordOffset 12
为什么会这样?如果最后一条记录已提交,为什么onCommit()
被同一条记录调用?当我使用enable.auto.commit
= false
切换到手动提交并在使用者逻辑中调用commitSync()
时,不会发生这种情况