Search code examples
springapache-kafkaspring-kafka

How to retry consuming message, then stop consuming, when error occurs in listener


I have Kafka listener writing data to a database, in case of database timeout (JdbcException), I'd like to retry, and if timeouts persist, stop consuming Kafka message.

As far as I understand, Spring Kafka 2.9 has 2 CommonErrorHandler implementations:

I would like to chain both of them: first try to redeliver messages several times, then stop container when delivery doesn't succeed.

How can I do that?


Solution

  • Use a DefaultErrorHandler with a custom recoverer that calls a CommonContainerStoppingErrorHandler after the retries are exhausted.

    See this answer for an example

    How do you exit spring boot application programmatically when retries are exhausted, to prevent kafka offset commit

    (It uses the older SeekToCurrentErrorHandler, but the same concept applies.)