Search code examples
javatransactionsspring-kafkaspring-cloud-stream-binder-kafkaretry-logic

Accessing retry attempt number in spring cloud stream kafka transaction


I need to access the retry attempt number in spring cloud stream kafka transactional retry so that for a particular exception, based on the retry attempt number i can post the outcome to different topic


Solution

  • I assume you mean the delivery attempt in the consumer; you can use a ContainerCustomizer bean to set the deliveryAttemptHeader property to true.

    /**
     * Set to true to populate the
     * {@link org.springframework.kafka.support.KafkaHeaders#DELIVERY_ATTEMPT} header when
     * the error handler or after rollback processor implements
     * {@code DeliveryAttemptAware}. There is a small overhead so this is false by
     * default.
     * @param deliveryAttemptHeader true to populate
     * @since 2.5
     */
    public void setDeliveryAttemptHeader(boolean deliveryAttemptHeader) {
        this.deliveryAttemptHeader = deliveryAttemptHeader;
    }
    

    Then consume the entire Message<?> to examine the header.