Search code examples
apache-kafkaspring-kafka

Custom logic after Kafka comsumer retry attempts


I have a Kafka Consumer with non blocking retry attempts

@RetryableTopic(attempts = "2", backoff = @Backoff(delay = 3000, multiplier = 1.5, maxDelay = 15000))
@KafkaListener(topics = "my-topic")
public void consume(
    @Header(KafkaHeaders.RECEIVED_TOPIC) String receivedTopic, 
    MyEvent event) {
    ...
    //consumer logic here
    ...
}

In this way when an error occur, the consumer retries to process for 2 times before to send the event into a DLQ topic.

There is a way, when retry attempts are exausted, to simply log a custom error message (or doing some custom logic) avoiding to try to send the event into a DLQ?


Solution

  • retries to process for 2

    attempts includes the initial delivery so 2 means only 1 retry.

    To prevent creation of the DLT, add dltStrategy = DltStrategy.NO_DLT to the @RetryableTopic annotation.

    https://docs.spring.io/spring-kafka/docs/current/reference/html/#configuring-no-dlt