学校做网站,网站页面设计最宽可做多宽,东莞哪里有网站制作公司,南京网站建设 雷仁网spring-retry每当软件组件相互通信时#xff0c;就有可能出现暂时的自我纠正错误。 这些故障包括服务的暂时不可用#xff0c;网络连接的瞬时丢失或服务繁忙时出现的超时。 在这种情况下#xff0c;适当的重试处理可以减少这些故障可能引起的问题。 在这篇文章中#xff0… spring-retry 每当软件组件相互通信时就有可能出现暂时的自我纠正错误。 这些故障包括服务的暂时不可用网络连接的瞬时丢失或服务繁忙时出现的超时。 在这种情况下适当的重试处理可以减少这些故障可能引起的问题。 在这篇文章中我们将看到如何使用Spring Retry向Spring应用程序添加健壮的重试逻辑。 Spring Retry可能不是很了解因为它没有在Spring文档概述中列出。 但是您可以在Spring Initializr页面上找到它。 建立 要使用Spring Retry我们需要在项目中添加以下依赖项 dependencygroupidorg.springframework.retry/groupidartifactidspring-retry/artifactidversion1.1.2.RELEASE/version
/dependency Spring Retry使用AOP因此请确保Spring AOP可用 dependencygroupidorg.springframework/groupidartifactidspring-aop/artifactidversion4.2.5.RELEASE/version
/dependency
dependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactIdversion1.8.8/version
/dependency 如果您使用的是Spring Boot 那么可以改用spring-boot-starter-aop dependencygroupidorg.springframework.boot/groupidartifactidspring-boot-starter-aop/artifactid
/dependency 要启用Spring Retry我们只需要将EnableRetry添加到我们的应用程序配置类中 EnableRetry
SpringBootApplication // or Configuration if you are not using Spring Boot
public class RetryExampleApplication {// ...
}使用批注添加重试处理 现在我们准备向方法添加重试处理。 为此我们仅需使用Retryable注释适当的方法 Service
public class MyService {Retryablepublic void simpleRetry() {// perform operation that is likely to fail}
} 带有Retryable注释的方法可以像其他任何方法一样调用。 但是每当可重试方法的执行因异常而失败时Spring都会自动重试多达三遍。 默认情况下Spring在方法调用之间使用1秒的延迟。 请注意调用线程在重试处理期间会阻塞。 重试行为可以通过多种方式自定义。 例如 Service
public class MyService {Retryable(value {FooException.class, BarException.class}, maxAttempts 5)public void retryWithException() {// perform operation that is likely to fail}Recoverpublic void recover(FooException exception) {// recover from FooException}
} 在这里我们告诉Spring仅在抛出FooException或BarException类型的Exception时应用重试处理。 其他异常不会导致重试。 maxAttempts 5告诉Spring如果失败最多重试该方法5次。 使用Recover我们为FooException定义了单独的恢复方法。 当可重试的方法因FooException而失败时这使我们可以运行特殊的恢复代码。 使用RetryTemplate添加重试处理 除了注释之外Spring Retry还提供了RetryTemplate可用于在Java代码中定义重试处理。 与其他任何bean一样可以在我们的配置类中简单地配置RetryTemplate EnableRetry
SpringBootApplication // or Configuration if you are not using Spring Boot
public class RetryExampleApplication {Beanpublic RetryTemplate retryTemplate() {SimpleRetryPolicy retryPolicy new SimpleRetryPolicy();retryPolicy.setMaxAttempts(5);FixedBackOffPolicy backOffPolicy new FixedBackOffPolicy();backOffPolicy.setBackOffPeriod(1500); // 1.5 secondsRetryTemplate template new RetryTemplate();template.setRetryPolicy(retryPolicy);template.setBackOffPolicy(backOffPolicy);return template;}// ...
} RetryPolicy确定何时应重试操作。 SimpleRetryPolicy是一个RetryPolicy实现可重试固定次数。 BackOffPolicy是一个策略接口用于控制重试尝试之间的退避。 在继续之前FixedBackOffPolicy会暂停一段固定的时间。 其他一些默认的BackOffPolicy实现是ExponentialBackOffPolicy增加每次重试的退避时间或NoBackOffPolicy重试之间没有延迟。 现在我们可以将RetryTemplate注入我们的服务。 要使用重试处理来运行代码我们只需调用RetryTemplate.execute Service
public class RetryService {Autowiredprivate RetryTemplate retryTemplate;public void withTemplate() {retryTemplate.execute(context - {// perform operation that is likely to fail});}// ...
} RetryTemplate.exeucte以RetryCallback TE作为参数。 RetryCallback是一个功能接口因此可以使用Java 8 Lambda表达式来实现如上所示。 摘要 Spring重试提供了一种向Spring应用程序添加重试处理的简便方法。 可以使用批注Retryable和Recover或通过将RetryCallback传递给RetryTemplate来添加重试处理。 您可以在GitHub上找到完整的示例源代码。 翻译自: https://www.javacodegeeks.com/2016/03/retry-handling-spring-retry.htmlspring-retry