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. Linux AVG ANTIVIRUS FREE使用介绍

    杀毒软件AVG,没有用过估计也有所耳闻.AVG ANTIVIRUS FREE - FOR LINUX 是AVG在Linux下的一款免费杀毒软件.它的官方下载地址供了rpm.deb.源码安装包等多种安装 ...

  2. Symantec Backup Exec Remote Agent 2010在Redhat Enterprise 6.6上启动问题

    在Red Hat Enterprise Linux Server release 6.6 (Santiago)上安装了Symantec Backup Exec Remote Agent 2010后,启 ...

  3. [Java入门笔记] Java语言基础(一):注释、标识符与关键字

    注释 什么是注释? 注释是我们在编写代码时某段代码.某个方法.某个类的说明文字,方便大家对于代码的阅读.被注释的内容不会被编译.执行. Java的注释分为三种类型:单行注释.多行注释.文档注释. 单行 ...

  4. 1 张图秒懂 Nova 16 种操作 - 每天5分钟玩转 OpenStack(44)

    前面我们讨论了 Instance 的若干操作,有的操作功能比较类似,也有各自的适用场景,现在是时候系统地总结一下了. 如上图所示,我们把对 Instance 的管理按运维工作的场景分为两类:常规操作和 ...

  5. 常用ADC滤波处理

    #define N 70 XDATA WORD Value_buf[N]; XDATA DWORD ADCValue; static BYTE v_gu8cnt=0; static BYTE i=0; ...

  6. 发现 OpenStack: 架构、功能和交互

    原文:http://www.ibm.com/developerworks/cn/cloud/library/cl-openstack-overview/index.html OpenStack 是由 ...

  7. Android欢迎界面

    欢迎界面,最典型的表现: 1.是整个应用的启动界面: 2.没有标题栏: 3.几秒之后才进入主界面. 所以实现上面3点,一个最基本的欢迎界面就做出来了. 首先,新建一个Activity,命名为Splas ...

  8. Linux find命令的用法实践

    一.find命令简介 Linux下find命令在目录结构中搜索文件,并执行指定的操作.Linux下find命令提供了相当多的查找条件,功能很强大.由于find具有强大的功能,所以它的选项也很多,其中大 ...

  9. windows常用端口对应表

    端口概念 在网络技术中,端口(Port)大致有两种意思:一是物理意义上的端口,比如,ADSL Modem.集线器.交换机.路由器用于连接其他网络设备的接口,如RJ-45端口.SC端口等等.二是逻辑意义 ...

  10. o(1)复杂度之双边滤波算法的原理、流程、实现及效果。

    一.引言     双边滤波在图像处理领域中有着广泛的应用,比如去噪.去马赛克.光流估计等等,最近,比较流行的Non-Local算法也可以看成是双边滤波的一种扩展.自从Tomasi et al等人提出该 ...