首页 > 其他分享 >Spring-Retry

Spring-Retry

时间:2024-04-11 15:22:51浏览次数:14  
标签:Retry Spring RemoteAccessException declarative retry example

 

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

相关文章

  • 记录SpringBoot里JPA配置多数据源
    之前的系统是建立在mysql数据库上的,后来需求变更,需要跟教务系统对接获取教师的课表数据,在Oracle上。baidu了之后找了几篇样例,写完了。发现只能连接@Primary的数据源。思索3天后,觉得应该是系统启动时自动就将Primary的数据源加载好了,并没有根据我的bean和mapper去匹配对应的数据......
  • java+saas模式医院云HIS系统源码Java+Spring+MySQL + MyCat融合BS版电子病历系统,支持
    java+saas模式医院云HIS系统源码Java+Spring+MySQL+MyCat融合BS版电子病历系统,支持电子病历四级云HIS系统是一款满足基层医院各类业务需要的健康云产品。该产品能帮助基层医院完成日常各类业务,提供病患预约挂号支持、病患问诊、电子病历、开药发药、会员管理、统计查询、医......
  • 基于SpringBoot+MySQL+SSM+Vue.js的餐馆订餐系统(附论文)
    演示视频基于SpringBoot+MySQL+SSM+Vue.js的餐馆订餐系统技术描述开发工具:Idea/Eclipse数据库:MySQLJar包仓库:Maven前端框架:Vue/ElementUI后端框架:Spring+SpringMVC+Mybatis+SpringBoot文字描述基于SpringBoot+MySQL+SSM+Vue.js的餐馆订餐系统(附论文),用......
  • SpringBoot应用监控
    在微服务监控与安全 / SpringActuator中已经示例了SpringBoot提供了运行时的应用监控和管理的功能,可以通过http、jmx、ssh协议进行操作。1、http微服务监控与安全 / SpringActuator中已经就示范了这种方式。但是这种方式有个缺点:对于springactuator......
  • ssm基于spring和vue开发的web新闻流媒体平台论文
    摘要如今的时代,是有史以来最好的时代,随着计算机的发展到现在的移动终端的发展,国内目前信息技术已经在世界上遥遥领先,让人们感觉到处于信息大爆炸的社会。信息时代的信息处理肯定不能用之前的手工处理这样的解决方法,必须采用计算机来处理这些信息,因为传统方法对应计算机处......
  • ssm基于SpringMVC网上选课系统的设计与实现论文
    摘要随着课程的增多,学生的选择性越来越大。现在大多数学校选课的方式采用学生上报给老师,老师负责整理。这种方式效率低、灵活性低,每门课程的选报人数有限就给老师的统计整理工作造成困难。目前,网络发展成熟,各类管理系统层出不穷,如果可以有专业的管理系统提供网上选课,则可......
  • 记录一个springcloud-task-core.jar导致CommandLineRunner @order排序失效的问题
    项目中编写了几个CommandLineRunner,并且加上了spring的@order注解,期望在启动时会按顺序执行(从order的value小到大执行),但是实际使用发现排序不生效于是进行debug,CommandLineRunner类的排序是在SpringApplication.class的callRunners方法privatevoidcallRunners(ApplicationCon......
  • Springcloud学习笔记63---RocketMq超时重试,导致重复消费的问题,解决方案
    1.重复消费的背景当Consumer处理时间过长,在超时时间内没有返回给Broker消费状态,那么Broker也会自动重试。设定一个超时时间,达到超时时间的那个消费当作消费失败处理。Java客户端中的DefaultPushConsumer中的构造方法中的consumeTimeout字段(默认15分钟)。packagewilliam.rmq.......
  • Spring Actuator 自定义HealthIndicator
    在SpringActuator实现自定义端点中案例的的基础上,实现自定义HealthIndicator。为什么还要实现HealthIndicator呢?SpringActuator实现自定义端点中案例只是对status的数据进行了监控,至于这个数据是否健康并没有进行评价。实现HealthIndicator就是对自定义监控数据的健康状态根......
  • 京东一面:如何在SpringBoot启动时执行特定代码?有哪些方式?
    引言SpringBoot提供了许多便捷的功能和特性,使得开发者可以更加轻松地构建强大、高效的应用程序。然而,在应用程序启动时执行一些初始化操作是至关重要的,它可以确保应用程序在启动后处于预期的状态,从而提供更好的用户体验和稳定性。在应用程序启动时执行初始化操作有许多好处。......