Spring retry基本使用

背景介绍

在实际工作过程中,重试是一个经常使用的手段。比如MQ发送消息失败,会采取重试手段,比如工程中使用RPC请求外部服务,可能因为网络

波动出现超时而采取重试手段......可以看见重试操作是非常常见的一种处理问题,系统设计的手段

而在之前我们项目中处理重拾操作依赖MQ自身的重试机制,但是这种机制不是很灵活,如果某些功能没有使用MQ的话,那么就不是那么方便了,而本文介绍的

Spring-Retry却能够以一种很优雅的方式解决这种问题,当然目前版本的Spring-retry还不是完美的,还是有待改进的.不过已经很不错了.

基本使用

  • 例子1

      @Configuration
    @EnableRetry
    public class Application { @Bean
    public Service service() {
    return new Service();
    } } @Service
    class Service {
    @Retryable(RemoteAccessException.class)
    public void service() {
    // ... do something
    }
    @Recover
    public void recover(RemoteAccessException e) {
    // ... panic
    }
    }
  • 例子2

     @org.springframework.stereotype.Service
    public class Service1 { @Retryable(value = {RemoteAccessException.class, RuntimeException.class},
    maxAttempts = 2,
    backoff = @Backoff(value = 2000))
    public void service() {
    System.out.println("do some things");
    // this exception will just trigger recover1, do not trigger recover3
    throw new RemoteAccessException("remote access exception");
    // this exception will just trigger recover2
    // throw new RuntimeException("runtime exception"); // System.out.println("do another things");
    } // 如果使用注解的话,这个recover貌似只能写在本类中,我测试了如果将recover方法写在
    // recoverService中,好像找不到 @Recover
    public void recover1(RemoteAccessException e) {
    System.out.println(e.getMessage());
    System.out.println("do recover operation1");
    } @Recover
    public void recover2(RuntimeException e) {
    System.out.println(e.getMessage());
    System.out.println("do recover operation2");
    } @Recover
    public void recover3(RemoteAccessException e) {
    System.out.println(e.getMessage());
    System.out.println("do recover operation3");
    } }
  • 例子3

     @Service
    public class Service2 { public void test(){
    final RetryTemplate retryTemplate = new RetryTemplate();
    final SimpleRetryPolicy policy = new SimpleRetryPolicy(3, Collections.<Class<? extends Throwable>, Boolean>
    singletonMap(Exception.class, true));
    FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
    fixedBackOffPolicy.setBackOffPeriod(100);
    retryTemplate.setRetryPolicy(policy);
    retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
    final RetryCallback<Object, Exception> retryCallback = new RetryCallback<Object, Exception>() {
    public Object doWithRetry(RetryContext context) throws Exception {
    System.out.println("do some thing");
    //设置context一些属性,给RecoveryCallback传递一些属性
    context.setAttribute("key1", "value1");
    System.out.println(context.getRetryCount());
    throw new Exception("exception");
    // return null;
    }
    }; // 如果RetryCallback执行出现指定异常, 并且超过最大重试次数依旧出现指定异常的话,就执行RecoveryCallback动作
    final RecoveryCallback<Object> recoveryCallback = new RecoveryCallback<Object>() {
    public Object recover(RetryContext context) throws Exception {
    System.out.println("do recory operation");
    System.out.println(context.getAttribute("key1"));
    return null;
    }
    }; try {
    final Object execute = retryTemplate.execute(retryCallback, recoveryCallback);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

参考资料

Spring retry基本使用的更多相关文章

  1. 自己动手实践 spring retry 重试框架

    前序 马上过年了,预祝大家,新年快乐,少写bug 什么是spring retry? spring retry是从spring batch独立出来的一个能功能,主要实现了重试和熔断. 什么时候用? 远程 ...

  2. Spring Retry

    最近组内准备将项目中原有的重试功能抽取出来重构为一个重试平台,由于对重试的功能要求比较高,采用了不少中间件和框架(jimdb,jproxy, Elastic-Job ,JMQ,Hbase, Disru ...

  3. Spring retry实践

    在开发中,重试是一个经常使用的手段.比如MQ发送消息失败,会采取重试手段,比如工程中使用RPC请求外部服务,可能因为网络波动出现超时而采取重试手段......可以看见重试操作是非常常见的一种处理问题, ...

  4. Spring异常重试框架Spring Retry

    Spring Retry支持集成到Spring或者Spring Boot项目中,而它支持AOP的切面注入写法,所以在引入时必须引入aspectjweaver.jar包. 快速集成的代码样例: @Con ...

  5. 【spring】spring retry介绍

    一.为什么需要重试? 我们知道只要是网络请求都有失败的情况,这个时候增加retry机制是必要的.而spring全家桶中就有这么一套机制. 二.spring retry spring系列的spring ...

  6. 异常重试框架Spring Retry实践

    前期准备在Maven项目中添加Spring Retry和切面的依赖 POM: <!-- Spring Retry --> <dependency> <groupId> ...

  7. Spring框架中一个有用的小组件:Spring Retry

    1.概述 Spring Retry 是Spring框架中的一个组件, 它提供了自动重新调用失败操作的能力.这在错误可能是暂时发生的(如瞬时网络故障)的情况下很有帮助. 在本文中,我们将看到使用Spri ...

  8. Spring Retry 在SpringBoot 中的应用

    Spring Boot中使用Spring-Retry重试框架 Spring Retry提供了自动重新调用失败的操作的功能.这在错误可能是暂时的(例如瞬时网络故障)的情况下很有用. 从2.2.0版本开始 ...

  9. Spring Retry 重试

    重试的使用场景比较多,比如调用远程服务时,由于网络或者服务端响应慢导致调用超时,此时可以多重试几次.用定时任务也可以实现重试的效果,但比较麻烦,用Spring Retry的话一个注解搞定所有.话不多说 ...

随机推荐

  1. 【ps】裁剪图片的某一块

    最近做我趣的专辑学了一点小技巧,我们切图的时候可能需要改变图片上的文字.需要图片某一部分的颜色块进行覆盖等.这时候就需要下面的技巧啦: 第一步:点v,变箭头选中图片 第二部:点M,变矩形框,划出需要裁 ...

  2. Java中GC的工作原理

    转文: 一个优秀的Java程序员必须了解GC的工作原理.如何优化GC的性能.如何与GC进行有限的交互,有一些应用程序对性能要求较高,例如嵌入式系统.实时系统等,只有全面提升内存的管理效率,才能提高整个 ...

  3. Vmware虚拟机进入BIOS方法

    在VMware里面如何进入BIOS是个头疼的问题,因为启动界面一闪而过(下面两个图如果不设置bios.bootDelay,基本上很难抓到),即使你狂按F2或ESC键(ESC: 调整启动顺序;F2: 进 ...

  4. Loadrunner代理录制设置

    使用LR代理录制原理 启用LR代理服务器监听设置好的端口号是否有请求信息发送给服务器,有请求时,代理服务器接收带请求,并转发给对应的系统服务器,LR从而获取到请求的信息与数据,生成脚本. 使用代理的前 ...

  5. Centos网络配置

    网上搜索:centos网络配置的方法,主要包括dns.网关.IP地址,主要是配置resolv.conf\network\ifcfg-eth0这些网络配置文件. 稍后我会就centos7的网络配置进行实 ...

  6. 看 nova-scheduler 如何选择计算节点 - 每天5分钟玩转 OpenStack(27)

    本节重点介绍 nova-scheduler 的调度机制和实现方法:即解决如何选择在哪个计算节点上启动 instance 的问题. 创建 Instance 时,用户会提出资源需求,例如 CPU.内存.磁 ...

  7. CentOS 6.3下Samba服务器的安装与配置

    一.简介 Samba是一个能让Linux系统应用Microsoft网络通讯协议的软件,而SMB是Server Message Block的缩写,即为服务器消息块 ,SMB主要是作为Microsoft的 ...

  8. Chrome浏览器设置默认编码

    设置-->高级设置-->网络内容-->自定义字体(滚动条拉到最底部)-->编码

  9. 一些常用的String方法 C#

    String Reference: https://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx Method Strin ...

  10. strus2验证框架

    为什么要用验证框架? 当验证规划比较复杂时,Action类的代码江边的非常繁琐,假如我们要对电话号码进行验证,是非常麻烦的. 验证框架的优点 Struts2中内置了一个验证框架,将常用的验证规则进行了 ...