Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Kafka - which batch error handler to use?

I just started to use spring-kafka 2.6.4. I created consumer factory that polls messages by batches:

@Bean
public ConcurrentKafkaListenerContainerFactory<String, String>
            kafkaListenerContainerFactory(MeterRegistry meterRegistry) {
    ConcurrentKafkaListenerContainerFactory<String, String> factory =
        new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(consumerFactory());
    factory.setConcurrency(kafkaProperties.getTopicConcurrency());
    factory.setBatchListener(true);
    factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.BATCH);
    return factory;
}

Now I want to define proper error handler that causes the consumer to stuck on the failed record and not to poll the next batch.

Which error handler should I use?

Thanks.

like image 716
Ron Shimoni Avatar asked Mar 30 '26 17:03

Ron Shimoni


1 Answers

The RecoveringBatchErrorHandler (https://docs.spring.io/spring-kafka/docs/current/reference/html/#recovering-batch-eh) is now the preferred (and default since 2.5) batch error handler. Your listener can throw a specific exception to indicate which record in the batch failed.

There is also a RetryingBatchErrorHandler (https://docs.spring.io/spring-kafka/docs/current/reference/html/#retrying-batch-eh).

like image 161
Gary Russell Avatar answered Apr 02 '26 13:04

Gary Russell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!