RetryTemplate用法

 public static void main(String[] args) throws Throwable {
        RetryTemplate retryTemplate = new RetryTemplate();
        //指数退避策略 等待时间= 等待时间*倍数 ,即每一次的等待时间是上一次等待时间的n倍,
        //到达最大的等待时间之后就不在增加了,一直都是以最大的等待时间在等待。默认执行3次
        ExponentialBackOffPolicy exponentialBackOffPolicy = new ExponentialBackOffPolicy();
        //初始等待时间
        exponentialBackOffPolicy.setInitialInterval(1000);
        //时间等待倍数
        exponentialBackOffPolicy.setMultiplier(2);
        //最大等待时间
        exponentialBackOffPolicy.setMaxInterval(5000);

        retryTemplate.setBackOffPolicy(exponentialBackOffPolicy);

        String execute = retryTemplate.execute(new RetryCallback<String, Throwable>() {
            @Override
            public String doWithRetry(RetryContext context) throws Throwable {
                //需要重试的代码
                System.out.println("开始执行======");
                throw new TimeoutException();

            }
        }, new RecoveryCallback<String>() {
            @Override
            public String recover(RetryContext context) throws Exception {
                //重试失败后执行的代码
                return "failed callback";
            }
        });
        System.out.println(execute);

    }

    /**
     * 根据不同的异常选择不同的重试策略
     *
     * @throws Throwable
     */
    public static void exceptionClassifier() throws Throwable {
        RetryTemplate retryTemplate = new RetryTemplate();
        ExceptionClassifierRetryPolicy policy = new ExceptionClassifierRetryPolicy();

        policy.setExceptionClassifier(new Classifier<Throwable, RetryPolicy>() {
            @Override
            public RetryPolicy classify(Throwable classifiable) {

                if (classifiable instanceof NullPointerException) {
                    //NullPointerException 异常就是SimpleRetryPolicy策略
                    return new SimpleRetryPolicy();
                }
                if (classifiable instanceof TimeoutException) {
                    //如果是TimeoutException 异常就是AlwaysRetryPolicy(一直执行直到成功)策略
                    return new AlwaysRetryPolicy();
                }
                //不执行直接走 RecoveryCallback。recover()
                return new NeverRetryPolicy();
            }
        });

        retryTemplate.setRetryPolicy(policy);

        String execute = retryTemplate.execute(new RetryCallback<String, Throwable>() {
            @Override
            public String doWithRetry(RetryContext context) throws Throwable {
                //需要重试的代码
                System.out.println("开始执行======");
                throw new TimeoutException();

            }
        }, new RecoveryCallback<String>() {
            @Override
            public String recover(RetryContext context) throws Exception {
                //重试失败后执行的代码
                return "failed callback";
            }
        });
        System.out.println(execute);

    }

    public static void simpleExceptionRetry() throws Throwable {
        Map<Class<Exception>, Boolean> classBooleanMap = Collections.singletonMap(Exception.class, true);
        RetryTemplate retryTemplate = new RetryTemplate();
        //重试的代码块如果有NullPointerException就重复执行,最多执行5遍。
        //如果发生的异常不是NullPointerException 就不会重复执行直接执行RecoveryCallback。recover()
        SimpleRetryPolicy policy2 = new SimpleRetryPolicy(5, Collections.singletonMap(NullPointerException.class, true));
        retryTemplate.setRetryPolicy(policy2);

        String execute = retryTemplate.execute(new RetryCallback<String, Throwable>() {
            @Override
            public String doWithRetry(RetryContext context) throws Throwable {
                //需要重试的代码
                System.out.println("开始执行======");
                throw new TimeoutException();

            }
        }, new RecoveryCallback<String>() {
            @Override
            public String recover(RetryContext context) throws Exception {
                //重试失败后执行的代码
                return "failed callback";
            }
        });
        System.out.println(execute);
    }

    public static void simpleRetry() throws Throwable {
        RetryTemplate retryTemplate = new RetryTemplate();
        //最多重试3次
        SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy(3);
        retryTemplate.setRetryPolicy(simpleRetryPolicy);
        String execute = retryTemplate.execute(new RetryCallback<String, Throwable>() {
            @Override
            public String doWithRetry(RetryContext context) throws Throwable {
                //需要重试的代码
                System.out.println("开始执行======");
                int i = 1 / 0;
                return "success";
            }
        }, new RecoveryCallback<String>() {
            @Override
            public String recover(RetryContext context) throws Exception {
                //重试失败后执行的代码
                return "failed callback";
            }
        });
        System.out.println(execute);
    }

    /**
     * 超时时间内重试策略
     *
     * @throws Throwable
     */
    public static void timeOutRetry() throws Throwable {
        RetryTemplate retryTemplate = new RetryTemplate();
        TimeoutRetryPolicy timeoutRetryPolicy = new TimeoutRetryPolicy();
        retryTemplate.setRetryPolicy(timeoutRetryPolicy);
        //在这个时间内如果失败会不停的重试
        timeoutRetryPolicy.setTimeout(10);
        String execute = retryTemplate.execute(new RetryCallback<String, Throwable>() {
            @Override
            public String doWithRetry(RetryContext context) throws Throwable {
                //需要重试的代码
                System.out.println("开始执行======");
                TimeUnit.SECONDS.sleep(3);
                int i = 1 / 0;
                return "success";
            }
        }, new RecoveryCallback<String>() {
            @Override
            public String recover(RetryContext context) throws Exception {
                //重试失败后执行的代码
                return "failed callback";
            }
        });
        System.out.println(execute);
    }
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值