我有一个重试5次的Kafka使用者,并且我将Spring Kafka与重试模板一起使用。现在,如果所有重试都失败了,那么在这种情况下如何确认工作。另外,如果我将确认模式设置为手动,那么如何确认这些消息
消费者
@Bean("kafkaListenerContainerFactory")
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory(RetryTemplate retryTemplate) {
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL_IMMEDIATE);
factory.setRetryTemplate(retryTemplate);
factory.setRecoveryCallback(context -> {
log.error("Maximum retry policy has been reached {}", context.getAttribute("record"));
Acknowledgment ack = (Acknowledgment) context.getAttribute(RetryingMessageListenerAdapter.CONTEXT_ACKNOWLEDGMENT);
ack.acknowledge();
return null;
});
factory.setConcurrency(Integer.parseInt(kafkaConcurrency));
return factory;
}
Kafka监听器
@KafkaListener(topics = "${kafka.topic.json}", containerFactory = "kafkaListenerContainerFactory")
public void recieveSegmentService(String KafkaPayload, Acknowledgment acknowledgment) throws Exception {
KafkaSegmentTrigger kafkaSegmentTrigger;
kafkaSegmentTrigger = TransformUtil.fromJson(KafkaPayload, KafkaSegmentTrigger.class);
log.info("Trigger recieved from segment service {}", kafkaSegmentTrigger);
processMessage(kafkaSegmentTrigger);
acknowledgment.acknowledge();
}
答案 0 :(得分:1)
如果您有RecoveryCallback
可以在重试用尽时处理记录,并且您没有使用手动方式,则容器将提交偏移量。
如果使用的是MANUAL ack,则可以在恢复回调中提交偏移量。
传递给回调的上下文对象具有属性RetryingMessageListenerAdapter.CONTEXT_ACKNOWLEDGMENT
(“确认”)。
它也有CONTEXT_CONSUMER
和CONTEXT_RECORD
。