Issue
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:
- DefaultErrorHandler tries to redeliver messages several times and then send failed messages to logs or DLQ
- CommonContainerStoppingErrorHandler stops listener container and message consumption
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
(It uses the older SeekToCurrentErrorHandler
, but the same concept applies.)
Answered By - Gary Russell
Answer Checked By - Marilyn (JavaFixing Volunteer)