try/catch,while 循环或者定时任务  这样看起来 好  low

sping boot  retry , 这样代码更简洁

eg:方式一:

    @Retryable(value= {RemoteAccessException.class},maxAttempts = ,backoff = @Backoff(delay = 5000l,multiplier = ))
public void hahha() throws Exception {
System.err.println("************************");
System.out.println("do something...");
throw new RemoteAccessException("调用异常");
} @Recover
public void recover(RemoteAccessException e) {
System.out.println(e.getMessage());
}

eg:方式二 , 说明 1.上面的参数,在recover,可以拿到,需要那那个参数,写进去就可以了

        2. 如果有返回值,那么返回值的类型必须一样,下例中,都是void

 
    @Retryable(value= {RemoteAccessException.class},maxAttempts = ,backoff = @Backoff(delay = 5000l,multiplier = ))
public void hahha(String name ,String addr) throws Exception {
System.err.println("************************");
System.out.println("do something...");
addr="AAAAA";
throw new RemoteAccessException("调用异常");
} @Recover
public void recover(RemoteAccessException e,String addr,String name) {
System.out.println(e.getMessage()); System.out.println("name :"+name);
System.out.println("addr :"+addr);
}

使用:

1.依赖:

<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency> <dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>

2. 应用启动类开启 retry

@EnableRetry
public class Application {
.......
}

3. 在指定方法上标记 @Retryable 来开启重试

@Retryable(value={A异常.class,B异常.class},
maxAttempts=重试次数,
backoff = @Backoff(delay = 延迟毫秒数,multiplier = 延迟倍数))
public void retryTest() throws Exception {
System.out.println(Thread.currentThread().getName()+" do something...");
throw new RemoteAccessException("RemoteAccessException....");
}

4. 在指定方法上标记 @Recover 来开启重试失败后调用的方法 (注意, 需跟重处理方法在同一个类中)

  @Recover
public void recover(A异常 e) {
// ... do something
} @Recover
public void recover(B异常 e) {
// ... do something
}

使用详解

spring-retry 通过 AOP 实现对目的方法的封装,执行在当前线程下,所以重试过程中当前线程会堵塞。如果 BackOff 时间设置比较长,最好起异步线程重试(也可以加 @Async 注解)。

@Retryable 注解

被注解的方法发生异常时会重试

  • value: 指定发生的异常进行重试
  • include: 和 value 一样,默认空,当 exclude 也为空时,所有异常都重试
  • exclude: 指定异常不重试,默认空,当 include 也为空时,所有异常都重试
  • maxAttemps: 重试次数,默认 3
  • backoff: 重试补偿机制,默认没有

@Backoff 注解

  • delay: 指定延迟后重试
  • multiplier: 指定延迟的倍数,比如 delay=5000l,multiplier=2 时,第一次重试为 5 秒后,第二次为 10 秒,第三次为 20 秒

@Recover

  • 当重试到达指定次数时,被注解的方法将被回调,可以在该方法中进行日志处理。需要注意的是发生的异常和入参类型一致时才会回调

本文参考 
http://blog.csdn.net/u014513883/article/details/52371198

springboot retry的更多相关文章

  1. springboot 整合retry(重试机制)

    当我们调用一个接口可能由于网络等原因造成第一次失败,再去尝试就成功了,这就是重试机制,spring支持重试机制,并且在Spring Cloud中可以与Hystaix结合使用,可以避免访问到已经不正常的 ...

  2. Spring Retry 在SpringBoot 中的应用

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

  3. 搭建Springboot监控中心报错A attempt was made to call the method reactor.retry.Retry.retryMax(I)Lreactor/ret)

    服务器还没启动就报错,是因为jar包的版本没对上,看的视频是SpringBoot2.0 ,现在已经是2.1.7了 将spring-boot-admin-starter-server版本改为最新就ok了

  4. springboot 详细配置2

    # =================================================================== # COMMON SPRING BOOT PROPERTIE ...

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

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

  6. SpringBoot进阶教程(二十三)Linux部署Quartz

    在之前的一篇文章中<SpringBoot(九)定时任务Schedule>,已经详细介绍了关于schedule框架的配置和使用,有收到一些朋友关于部署的私信,所以抽时间整理一个linux部署 ...

  7. SpringBoot集成rabbitmq(二)

    前言 在使用rabbitmq时,我们可以通过消息持久化来解决服务器因异常崩溃而造成的消息丢失.除此之外,我们还会遇到一个问题,当消息生产者发消息发送出去后,消息到底有没有正确到达服务器呢?如果不进行特 ...

  8. Java SpringBoot集成RabbitMq实战和总结

    目录 交换器.队列.绑定的声明 关于消息序列化 同一个队列多消费类型 注解将消息和消息头注入消费者方法 关于消费者确认 关于发送者确认模式 消费消息.死信队列和RetryTemplate RPC模式的 ...

  9. Springboot集成Spring Batch

    Spring官网 (https://spring.io/projects/spring-batch#overview)对Spring  Batch的解释: 一个轻量级的.全面的批处理框架,用于开发对企 ...

随机推荐

  1. C++进阶--模板及关键字typename

    //############################################################################ /* * 模板介绍 */ //函数模板 t ...

  2. 记录一次OOM分析过程

    工具: jstat jmap jhat 1.jstat查看gc情况 S0C.S1C.S0U.S1U:Survivor 0/1区容量(Capacity)和使用量(Used) EC.EU:Eden区容量和 ...

  3. bzoj4812: [Ynoi2017]由乃打扑克

    由于查询的是树链的并的信息,同时信息不能高效合并,只能考虑用bitset维护,小范围暴力预处理以便从bitset算出答案 对树分块,保证每块是连通的且直径较小,对分出的块缩点建新树,在新树上建树上ST ...

  4. itertools库 combinations() 和 permutations() 组合 和 排列选项的方法

    combinations方法重点在组合,permutations方法重在排列. combinations和permutations返回的是对象地址,原因是在python3里面,返回值已经不再是list ...

  5. appium 元素文件 -查找元素 封装思路和方法

    方法1. try: target="//android.widget.TextView[@text='立即體驗']" element = WebDriverWait(dr,5,0. ...

  6. Mina2 udp--zhengli

    一.包与类名. 1.所有类和方法严格使用驼峰法命名.例:SSLFilter 更名为 SslFilter.NIO传输类在命名时增加 Nio 前缀.因为NIO 并不是 socket/datagram 传输 ...

  7. 解决 https urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] 错误

    import ssl ssl._create_default_https_context = ssl._create_unverified_context

  8. Eclipse使用Maven创建Web时错误:Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webap

    网上也有好多方法我没有试成功,不过我将maven的conf setting.xml里的 阿里镜像给注释就可以了,你们也可以试试

  9. mysql 删除表 外键出错

    MySQL库中有俩表,table1和table2,相互关联,在删除表的时候出错: Cannot delete or update a parent row: a foreign key constra ...

  10. RHEL7安装配置VNC

    RHEL7安装配置VNC 作者:Eric 微信:loveoracle11g 安装配置VNC服务程序 [root@zhouwanchun yum.repos.d]# cd ~ [root@zhouwan ...