Search code examples
javaspringspring-bootspring-kafkadeprecation-warning

Kafka listener - Is it possible to acknowledge message in DefaultErrorHandler , when ACK mode is set to MANUAL_IMMEDIATE


I created kafka listener with ACK mode set to MANUAL_IMMEDIATE.

I'm using DefaultErrorHandler and DeadLetterPublishingRecoverer to retry message few times and if it still fails send it to other topic.

BackOff fixedBackOff = new FixedBackOff(2000, 3);
DeadLetterPublishingRecoverer recoverer = new DeadLetterPublishingRecoverer(kafkaTemplate,
            (kafkaRecord, exception) -> new TopicPartition("OTHER-TOPIC", kafkaRecord.partition()));
DefaultErrorHandler defaultErrorHandler = new DefaultErrorHandler(recoverer, fixedBackOff);

Is it possible to acknowledge failed message after I send it to other topic. I don't want to have duplicates or lost messages.

I tried to pass Acknowledgment object in exception - but original exception is replaced by ListenerExecutionFailedException.

Also I found that setRecoveryCallback method is Deprecated.


Solution

  • The container automatically acks recovered messages (even with manual ack mode) by default (since 2.5.3).

    /**
     * Set to false to prevent the container from committing the offset of a recovered
     * record (when the error handler does not itself throw an exception).
     * @param ack false to not commit.
     */
    default void setAckAfterHandle(boolean ack) {