Spring retry基本使用

背景介绍

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

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

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

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

基本使用

  • 例子1

    1. @Configuration
    2. @EnableRetry
    3. public class Application {
    4. @Bean
    5. public Service service() {
    6. return new Service();
    7. }
    8. }
    9. @Service
    10. class Service {
    11. @Retryable(RemoteAccessException.class)
    12. public void service() {
    13. // ... do something
    14. }
    15. @Recover
    16. public void recover(RemoteAccessException e) {
    17. // ... panic
    18. }
    19. }
  • 例子2

    1. @org.springframework.stereotype.Service
    2. public class Service1 {
    3. @Retryable(value = {RemoteAccessException.class, RuntimeException.class},
    4. maxAttempts = 2,
    5. backoff = @Backoff(value = 2000))
    6. public void service() {
    7. System.out.println("do some things");
    8. // this exception will just trigger recover1, do not trigger recover3
    9. throw new RemoteAccessException("remote access exception");
    10. // this exception will just trigger recover2
    11. // throw new RuntimeException("runtime exception");
    12. // System.out.println("do another things");
    13. }
    14. // 如果使用注解的话,这个recover貌似只能写在本类中,我测试了如果将recover方法写在
    15. // recoverService中,好像找不到
    16. @Recover
    17. public void recover1(RemoteAccessException e) {
    18. System.out.println(e.getMessage());
    19. System.out.println("do recover operation1");
    20. }
    21. @Recover
    22. public void recover2(RuntimeException e) {
    23. System.out.println(e.getMessage());
    24. System.out.println("do recover operation2");
    25. }
    26. @Recover
    27. public void recover3(RemoteAccessException e) {
    28. System.out.println(e.getMessage());
    29. System.out.println("do recover operation3");
    30. }
    31. }
  • 例子3

    1. @Service
    2. public class Service2 {
    3. public void test(){
    4. final RetryTemplate retryTemplate = new RetryTemplate();
    5. final SimpleRetryPolicy policy = new SimpleRetryPolicy(3, Collections.<Class<? extends Throwable>, Boolean>
    6. singletonMap(Exception.class, true));
    7. FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
    8. fixedBackOffPolicy.setBackOffPeriod(100);
    9. retryTemplate.setRetryPolicy(policy);
    10. retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
    11. final RetryCallback<Object, Exception> retryCallback = new RetryCallback<Object, Exception>() {
    12. public Object doWithRetry(RetryContext context) throws Exception {
    13. System.out.println("do some thing");
    14. //设置context一些属性,给RecoveryCallback传递一些属性
    15. context.setAttribute("key1", "value1");
    16. System.out.println(context.getRetryCount());
    17. throw new Exception("exception");
    18. // return null;
    19. }
    20. };
    21. // 如果RetryCallback执行出现指定异常, 并且超过最大重试次数依旧出现指定异常的话,就执行RecoveryCallback动作
    22. final RecoveryCallback<Object> recoveryCallback = new RecoveryCallback<Object>() {
    23. public Object recover(RetryContext context) throws Exception {
    24. System.out.println("do recory operation");
    25. System.out.println(context.getAttribute("key1"));
    26. return null;
    27. }
    28. };
    29. try {
    30. final Object execute = retryTemplate.execute(retryCallback, recoveryCallback);
    31. } catch (Exception e) {
    32. e.printStackTrace();
    33. }
    34. }
    35. }

参考资料

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. zDiaLog弹出层

    zDiaLog弹出层  立即下载 插件描述:zDiaLog弹出层 弹出框: 代替window.open.window.alert.window.confirm:提供良好的用户体验: 水晶质感,设计细腻 ...

  2. SQL Server 2008 master 数据库损坏解决总结

    SQL Server 2008 master数据库损坏后,SQL SERVER服务启动失败,查看错误日志,你会看到下面错误信息: 2015-10-27 10:15:21.01 spid6s      ...

  3. Java之TreeMap

    基本特性: 基于红黑树. 非线程安全. 同步使用: SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...))

  4. 0026 Java学习笔记-面向对象-抽象类、接口

    抽象方法与抽象类 抽象方法用abstract修饰,没有方法体部分,连花括号都不能有: 抽象方法和抽象类都用abstract修饰 包含抽象方法的类一定是抽象类:但不包含抽象方法的类也可以是抽象类 不能创 ...

  5. 【Linux学习】如何了解一个陌生的命令?

    如何了解一个陌生的命令? 有一些命令可以用来了解某个命令本身的情况,比如这个命令的绝对路径. $which ls which 在默认路径中搜索命令,返回该命令的绝对路径. $whereis ls wh ...

  6. Android入门笔记(一)

    第一部分,Android开发环境的搭建 1.去http://www.oracle.com/technetwork/java/javase/downloads/index.html下载最新版本jdk并安 ...

  7. diff/merge configuration in Team Foundation - common Command and Argument values - MSDN Blogs

    One of the extensibility points we have in Team Foundation V1 is that you can configure any other di ...

  8. 自定义shiro的Realm实现和CredentialsMatcher实现以及Token实现

    Realm是shiro比较核心的接口,简单说它的实现类就是校验用户输入的账号信息的地方.如果想自定义实现一般的配置文件如下: <!--自定义Realm 继承自AuthorizingRealm - ...

  9. 某墙尼妹,用个Response.Filter来解决StackExchange.Exceptional中google cdn的问题

    某墙墙了古古路,一些开源的东东里用了古古路CDN,比如Exceptional,Opserver ,导致服务要么慢要么用不了 必须要替换之 Exceptional就只要用Response.Filter替 ...

  10. ClearContainer 网络部分源码分析

    // cc-oci-runtime/src/oci.c /*! * Create the state file, apply mounts and run hooks, but do not star ...