1

I have a kafka listener and also there is a KafkaListenerErrorHandler configured. How can I ack or nack the event based on the exception type in the error handler class. Now how can I ack or nack from handleError method of KafkaExceptionHandler?

Code:

@Configuration
@RequiredArgsConstructor
public class KafkaExceptionHandlerConfig {
    
    private final JmsTemplate jmsTemplate;

    @Bean({"kafkaContainerErrorHandler"})
    public ErrorHandler kafkaContainerErrorHandler() {
        return new KafkaContainerHandler(dLQRecoverer());
    }

    @Bean
    public KafkaDLQRecoverer dLQRecoverer() {
        return new KafkaDLQRecoverer(jmsTemplate);
    }

    @Bean
    public KafkaListenerErrorHandler kafkaListenerErrorHandler() {
        return new KafkaExceptionHandler(dLQRecoverer());
    }

}

@Slf4j
@Component
@RequiredArgsConstructor
public class KafkaExceptionHandler implements KafkaListenerErrorHandler {


    @Override
    public Object handleError(final Message<?> message, final ListenerExecutionFailedException exception) {
        return null;
    }

}
2
  • It depends; you need to show your configuration and container properties (ack mode etc). Edit the question, don't try to put it in a comment. Jul 18, 2022 at 17:41
  • Added the configuration code
    – DK93
    Jul 19, 2022 at 5:12

1 Answer 1

0

KafkaListenerErrorHandlers are intended for request/reply scenarios where the error handler might want to return a different reply after some failure.

Those error handlers do not currently have access to the Acknowledgment argument. I have opened a new feature issue: https://github.com/spring-projects/spring-kafka/issues/2355

For custom error handling, you should implement a CommonErrorHandler (if one of the supplied ones does not do what you need).

https://docs.spring.io/spring-kafka/docs/current/reference/html/#error-handlers

2
  • Thanks for the information. Just an additional question though. When we do acknowledgement.nack(10000), the message is being replayed after the mentioned delay but I am not able to receive other events between that period. Is there a work around where we can move the offset and allow other events to be polled?
    – DK93
    Jul 19, 2022 at 16:08
  • 1
    No; that is how it is designed; you have to use the Non-Blocking retries mechanism to do what you want; see docs.spring.io/spring-kafka/docs/current/reference/html/… - the failed message is moved to a different topic (with the consumption delayed for the specified time) and the main container continues to process other records. Jul 19, 2022 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.