1

How could I go to "@Override public void onFailure(Throwable ex) { ... }"?

It always go "@Override public void onSuccess(SendResult<String, KafkaPresponseDto> result) {...}.

I want to print log.error("Unable to send message=["+kafkaPgResponseDto.toString()+"] due to : " + ex.getMessage());

Please help...

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());
            }
        });
sujinkim
  • 11
  • 1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Nov 15 '21 at 07:27

1 Answers1

1

I believe there is no need in the real Kafka to test your functionality. Consider to use a MockProducer for injection into that KafkaTemplate and emulate an error for that onFailure() case: https://www.baeldung.com/kafka-mockproducer

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118