https://github.com/spring-projects/spring-retry
This project provides declarative retry support for Spring applications. It is used in Spring Batch, Spring Integration, and others. Imperative retry is also supported for explicit usage.
Quick Start
This section provides a quick introduction to getting started with Spring Retry. It includes a declarative example and an imperative example.
Declarative Example
The following example shows how to use Spring Retry in its declarative style:
@Configuration @EnableRetry public class Application { } @Service class Service { @Retryable(retryFor = RemoteAccessException.class) public void service() { // ... do something } @Recover public void recover(RemoteAccessException e) { // ... panic } }
This example calls the service
method and, if it fails with a RemoteAccessException
, retries (by default, up to three times), and then tries the recover
method if unsuccessful. There are various options in the @Retryable
annotation attributes for including and excluding exception types, limiting the number of retries, and setting the policy for backoff.
The declarative approach to applying retry handling by using the @Retryable
annotation shown earlier has an additional runtime dependency on AOP classes. For details on how to resolve this dependency in your project, see the 'Java Configuration for Retry Proxies' section.
Imperative Example
The following example shows how to use Spring Retry in its imperative style (available since version 1.3):
RetryTemplate template = RetryTemplate.builder() .maxAttempts(3) .fixedBackoff(1000) .retryOn(RemoteAccessException.class) .build(); template.execute(ctx -> { // ... do something });
For versions prior to 1.3, see the examples in the RetryTemplate section.
标签:Retry,Spring,RemoteAccessException,declarative,retry,example From: https://www.cnblogs.com/anpeiyong/p/18129317