SoFunction
Updated on 2025-03-08

SpringBoot integrated Retry implementation error retry process step by step

Introduce dependencies

<dependency>
    <groupId></groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
    <groupId></groupId>
    <artifactId>spring-retry</artifactId>
</dependency>

Complete dependency

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance"
    xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId></groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId></groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <>1.8</>
    </properties>
    <dependencies>
        <dependency>
            <groupId></groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- spring-retry -->
        <dependency>
            <groupId></groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId></groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId></groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Turn on spring-retry

Add annotations to the startup class@EnableRetry

package ;
import ;
import ;
import ;
@EnableRetry
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        (, args);
    }
}

Use retry annotation

@Retryable annotation

  • value, the type of exception that can be tried. The same meaning as include. Default is empty (if excludes is also empty, then try all exceptions again)
  • include: Retryable exception type. Default is empty (if excludes is also empty, then try all exceptions again)
  • exclude: Exception type that does not need to be retryed. Default is empty (if include is also empty, then try all exceptions again)
  • maxAttempts: Maximum number of retry times (including the first failure), default is 3 times
  • backoff: Retry the waiting policy, the following will be introduced in @Backoff
  • recover: The callback method that indicates the maximum number of retrys reaches the maximum number of retrys

@Backoff annotation

  • delay, waiting time between retry (in milliseconds)
  • maxDelay, the maximum waiting time between retry (in milliseconds)
  • multipler, specifying multiples of delay
  • delayExpression, wait time expression between retry
  • maxDelayExpression, maximum waiting time expression between retry
  • multiplierExpression, specifying multiple expressions for delay
  • random, randomly specify the delay time

Example of usage

package ;
import ;
import ;
import ;
@Component
public class HttpRequest {
    private int count = 0;
    /**
      * Simulate network request exception
      * @return
      */
    @Retryable(recover = "errorHandler")
    public String getResponse() {
        count++;
        ("time: " + count);
        if (count &lt; 4) {
            throw new RuntimeException("count: " + count);
        }
        return "success";
    }
    /**
      * Error handling function
      * Note: String needs to be returned, otherwise the method will not find the exception
      * : Cannot locate recovery method
      *
      * @param e
      * @return
      */
    @Recover
    public String errorHandler(RuntimeException e) {
        ("errorHandler");
        return "ok";
    }
}

test

package ;
import ;
import ;
import ;
@SpringBootTest
public class HttpRequestTest {
    @Autowired
    private HttpRequest httpRequest;
    @Test
    public void getResponse(){
        ();
    }
}

Output result

time: 1
time: 2
time: 3
errorHandler

refer to

Use spring-retry in SpringBoot to easily solve retry

This is the article about the gradual introduction of SpringBoot integration Retry implementation error retry process. For more related SpringBoot Retry error retry content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!