Search code examples
spring-cloud-streamspring-retry

spring-cloud-stream retry policy per exception


In the documentation of the spring-cloud-stream project the only retry mechanism that I have saw was a single backoff policy.
The behavior that I would like to achieve is three different retry policies in case that an exception is thrown while consuming a message:

  1. Unrecoverable exception - this kind of messages wouldn't be tried to be processed again.
  2. Recoverable exception - this kind of messages would be retried number of times before being discarded.
  3. Transient exception - this kind of messages would be retried forever.

Is there a way to define a different retry policy for each kind of exception? I know that in spring-kafka there was an ability to provide a RetryTemplate and achieve the above-mentioned behavior.

I thought that maybe with the DLQ mechanism there would be a way to achieve this behavior.

Edit: I am using Kafka as the messaging middleware.


Solution

  • From Spring Cloud Stream 2.0.0 upwards you can define your own RetryTemplate :

    @StreamRetryTemplate
    public RetryTemplate myRetryTemplate() {
        return new RetryTemplate();
    }
    

    Full doc here : https://docs.spring.io/spring-cloud-stream/docs/current/reference/htmlsingle/#_retry_template