resilience4j retry with spring (not spring boot ) and using annotations

23 Views Asked by At

Is it possible to use resilience4j for retry in a spring ( not spring boot application ) while using annotations. Roughly this is what I am doing. But I don't see the retry happening. Adding dependency in pom

<dependency>
                <groupId>io.github.resilience4j</groupId>
                <artifactId>resilience4j-retry</artifactId>
                <version>1.7.1</version> <!-- Use the latest version here -->
            </dependency>
            <dependency>
                <groupId>io.github.resilience4j</groupId>
                <artifactId>resilience4j-spring</artifactId>
                <version>1.7.1</version> <!-- Use the latest version here -->
            </dependency>

Adding beans

package com.xyz;

import io.github.resilience4j.retry.Retry;
import io.github.resilience4j.retry.RetryConfig;
import io.github.resilience4j.retry.RetryRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;
import java.util.EmptyStackException;

@Configuration
public class Resilience4jConfig {

    @Bean
    public RetryConfig retryConfig() {
        return RetryConfig.custom()
                .maxAttempts(10)
                .waitDuration(Duration.ofMillis(1000))
                .retryExceptions(EmptyStackException.class)
                .build();
    }

    @Bean
    public Retry retryRegistry(RetryConfig retryConfig) {
        return Retry.of("id", retryConfig);
    }

}

Using annotation

@Retry(name = "id")
     public returnType servicelayerMethodIwantTORetry(SomeType sometype) {
         
        logger.info(CALMarkers.ALWAYS, "throwing exception to test retry");
        if(false){}else{
            throw new EmptyStackException();
        }

My app works just fine with spring-retry, but due to requirements I need to use resilience library.

0

There are 0 best solutions below