kafka提交消息offset

自動提交

 

手動同步提交

while (true) {
            ConsumerRecords<String, String> records =
                        consumer.poll(Duration.ofSeconds(1));
            process(records); // 處理消息
            try {
                        consumer.commitSync();
            } catch (CommitFailedException e) {
                        handle(e); // 處理提交失敗異常
            }
}

手動異步提交

while (true) {
            ConsumerRecords<String, String> records = 
 consumer.poll(Duration.ofSeconds(1));
            process(records); // 處理消息
            consumer.commitAsync((offsets, exception) -> {
 if (exception != null)
 handle(exception);
 });
}

手動提交位移

try {
           while(true) {
                        ConsumerRecords<String, String> records = 
                                    consumer.poll(Duration.ofSeconds(1));
                        process(records); // 處理消息
                        commitAysnc(); // 使用異步提交規避阻塞
            }
} catch(Exception e) {
            handle(e); // 處理異常
} finally {
            try {
                        consumer.commitSync(); // 最後一次提交使用同步阻塞式提交
 } finally {
      consumer.close();
}
}

手動自由提交偏移量

private Map<TopicPartition, OffsetAndMetadata> offsets = new HashMap<>();
int count = 0;
……
while (true) {
            ConsumerRecords<String, String> records = 
 consumer.poll(Duration.ofSeconds(1));
            for (ConsumerRecord<String, String> record: records) {
                        process(record);  // 處理消息
                        offsets.put(new TopicPartition(record.topic(), record.partition()),
                                   new OffsetAndMetadata(record.offset() + 1);
                       if(count % 100 == 0)
                                    consumer.commitAsync(offsets, null); // 回調處理邏輯是 null
                        count++;
 }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章