您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

春季重试,无需春季申请

春季重试,无需春季申请

我发现我可以使用RetryTemplate:

    RetryTemplate retryTemplate = new RetryTemplate();

    FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
    fixedBackOffPolicy.setBackOffPeriod(2000l);
    retryTemplate.setBackOffPolicy(fixedBackOffPolicy);

    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
    retryPolicy.setMaxAttempts(5);
    retryTemplate.setRetryPolicy(retryPolicy);

    retryTemplate.execute(new RetryCallback<Void, Throwable>() {
            @Override
            public Void doWithRetry(RetryContext context) throws Throwable {
                // do some job
                if(context.getRetryCount() < 3){ // unexpected disconnection
                    log.error("connection Failed");
                    throw new RuntimeException("retry exception"); 
                }
                System.out.println("RETRY" + context);
                return null;
            }
        });
其他 2022/1/1 18:30:01 有486人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶