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();
            }
        }
    }

参考资料

目录
相关文章
|
1月前
|
Java 数据库连接 API
【Spring】1、Spring 框架的基本使用【读取配置文件、IoC、依赖注入的几种方式、FactoryBean】
【Spring】1、Spring 框架的基本使用【读取配置文件、IoC、依赖注入的几种方式、FactoryBean】
46 0
|
5月前
|
缓存 Java 关系型数据库
我是如何使用Spring Retry减少1000 行代码
在我的日常工作中,我主要负责开发一个庞大的金融应用程序。当客户发送请求时,我们使用他们的用户 ID 从第三方服务获取他们的帐户信息,保存交易并更新缓存中的详细信息。尽管整个流程看起来足够简单,但这些下游系统中的每一个都是不可靠的。我们必须在每一层上实现重试,并且我们必须以一种可以控制重试次数和每次重试之间的延迟的方式来实现,这样我们就不会超载下游系统。由于我无法共享实际代码,我会创建一个演示系统来做简单表示:
35 0
|
8月前
|
Java Linux Docker
Spring Boot入门(二十六) 之 Docker的安装与基本使用
Spring Boot入门(二十六) 之 Docker的安装与基本使用
|
11月前
|
消息中间件 Java Docker
Spring Boot2.x-15 整合RabbitMQ 及RabbitMQ的基本使用
Spring Boot2.x-15 整合RabbitMQ 及RabbitMQ的基本使用
95 0
|
Java Spring
Spring Retry重试框架
实际工作中由于网络波动等原因导致代码执行失败需要重新执行,保证最终能够完成业务功能。通常来说,会用try/catch,while循环或者定时任务重处理。但是这样的做法缺乏统一性,要多写很多代码。spring-retry组件可以通过注解优雅的实现重处理功能。
526 0
|
安全 Dubbo Java
【Spring专题】「实战系列」重新回顾一下异常重试框架Spring Retry的功能指南
【Spring专题】「实战系列」重新回顾一下异常重试框架Spring Retry的功能指南
139 0
【Spring专题】「实战系列」重新回顾一下异常重试框架Spring Retry的功能指南
|
Java 领域建模 开发者
框架来解决优雅重试-spring retry
重试的意义To make processing more robust and less prone to failure, it sometimes helps to automatically retry a failed operation, in case it might succeed on a subsequent attempt. Errors that are susceptib
2536 0
框架来解决优雅重试-spring retry
|
Java 测试技术 API
Spring Retry中那些不为人知的技巧,你知道几个?
外部服务对于调用者来说一般都是不可靠的,尤其是在网络环境比较差的情况下,网络抖动很容易导致请求超时等异常情况,这时候就需要使用失败重试策略重新调用 API 接口来获取。重试策略在服务治理方面也有很广泛的使用,通过定时检测,来查看服务是否存活。
179 0
Spring Retry中那些不为人知的技巧,你知道几个?
|
XML 缓存 JavaScript
学习Spring5必知必会(2)~Spring的基本介绍和Spring基本使用、Spring的核心对象和管理bean的原理
学习Spring5必知必会(2)~Spring的基本介绍和Spring基本使用、Spring的核心对象和管理bean的原理
152 1
学习Spring5必知必会(2)~Spring的基本介绍和Spring基本使用、Spring的核心对象和管理bean的原理
|
设计模式 开发框架 安全
一文带你了解Spring框架的基本使用
Spring作为企业Java最流行的应用程序开发框架,已经被数以百万计的世界各地的开发人员使用,Spring框架用来创建高性能,易于测试的,可重用的代码。是一个开源的Java平台,它最初...
192 0