【发布时间】:2019-08-15 21:55:04
【问题描述】:
我有一个 spring boot kafka 消费者,它使用来自主题的数据并将其存储在数据库中,并在存储后确认它。 它工作正常,但是如果应用程序在使用记录后未能获得数据库连接,就会出现问题,在这种情况下,我们不会发送确认,但消息仍然不会被消费,直到或除非我们更改组 ID 并重新启动消费者
我的消费者如下所示
@KafkaListener(id = "${group.id}", topics = {"${kafka.edi.topic}"})
public void onMessage(ConsumerRecord record, Acknowledgment acknowledgment) {
boolean shouldAcknowledge = false;
try {
String tNo = getTrackingNumber((String) record.key());
log.info("Check Duplicate By Comparing With DB records");
if (!ediRecordService.isDuplicate(tNo)) {---this checks the record in my DB
shouldAcknowledge = insertEDIRecord(record, tNo); --this return true
} else {
log.warn("Duplicate record found.");
shouldAcknowledge = true;
}
if (shouldAcknowledge) {
acknowledgment.acknowledge();
}```
So if you see the above snippet we did not sent acknowledgment.
【问题讨论】:
标签: spring-boot apache-kafka kafka-consumer-api