我怎么能去"覆盖公共void onFailure(Throwable ex({…};?
它总是走"@重写公共void onSuccess(SendResult<String,KafkaPresponseTo>result({…}。
我想打印log.error("无法发送消息=["+kafkaPgResponseDto.toString((+"],原因是:"+例如getMessage(((;
请帮忙。。。
ListenableFuture<SendResult<String, KafkaPgResponseDto>> future = pgResponseKafkaTemplate.send(kurlyTopicNamePgResponse, kafkaPgResponseDto);
future.addCallback(new ListenableFutureCallback<SendResult<String, KafkaPgResponseDto>>(){
@Override
public void onSuccess(SendResult<String, KafkaPgResponseDto> result) {
KafkaPgResponseDto kafkaPgResponseDto = result.getProducerRecord().value();
log.debug("Send message=["+kafkaPgResponseDto.toString()+"] with offset=["+result.getRecordMetadata().offset()+"]");
}
@Override
public void onFailure(Throwable ex) {
log.error("Unable to send message=["+kafkaPgResponseDto.toString()+"] due to : "+ex.getMessage());
kafkaTransactionService.failedProcessingKafkaHistorySave(orderNo, kurlyTopicNamePgResponse, gson.toJson(payload), ex.toString());
}
});
我相信在真正的Kafka中没有必要测试您的功能。考虑使用MockProducer
注入该KafkaTemplate
,并模拟该onFailure()
情况下的错误:https://www.baeldung.com/kafka-mockproducer