Search code examples
spring-bootresilience4j

Trying to create Retry object from application.yml


I'm relatively new to Spring-Boot + resilience4j and I'm trying to create a Retry object using the config in my .yml file. Currently I'm trying to decorate a Mono with very similar syntax to what is given in the docs:

Retry retry = Retry.of("backendName", sampleRetryConfig);
Mono.fromCallable(backendService::doSomething)
    .transformDeferred(RetryOperator.of(retry))

In the above code snippet I'm explicitly declaring the sampleRetryConfig in the code and using that to create my Retry, but is there a way for me to create the Retry object using the RetryConfig pulled from my .yml file?

resilience4j.retry:
instances:
    apiRetry:
        maxAttempts: 3
        waitDuration: 2s
        enableExponentialBackoff: true
        ignoreExceptions:
            - example.exceptions

Support seems to be there for using the @Retry annotation, but I haven't found anything about support for what I'm trying to do.


Solution

  • Late answer but here's what I ended up doing. Using the RetryRegistry and declaring the Retry objects as beans I was able to make it work. Here's the contents of the .yml

    resilience4j:
      retry:
          api-path:
            maxAttempts: 1
            waitDuration: 1s
            ignoreExceptions:
     
    

    And the class where the retry beans were created:

    private final RetryRegistry retryRegistry;
    
    @Bean
    public Retry apiPathRetry() {
        return retryRegistry.retry("api-path");
    }
    

    Then finally, using the objects in a class.

    return Mono.fromSupplier(() -> method) 
    .flatMap(genericData-> {business logic})
    .transformDeferred(RetryOperator.of(apiPathRetry));