1

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?

G Quintana
  • 4,556
  • 1
  • 22
  • 23

1 Answers1

1

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.)

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • The idea is to wrap a CommonContainerStoppingErrorHandler in a ConsumerRecordRecoverer, is it? The CommonContainerStoppingErrorHandler.handleOne/handleBatch methods take a Consumer in their signature, how do I get this Consumer instance? How to implement properly for a Batch listener? – G Quintana Nov 08 '22 at 10:21
  • The consumer is not used in that error handler; or you can simply copy the code from its `stopContainer()` method. Batch is a bit more complicated because (unless you throw a `BatchListenerFailedException` to indicate which record in the batch failed) the recoverer is called for all records in the batch. – Gary Russell Nov 08 '22 at 12:42