ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

失败重试guava-retry

2021-12-21 15:00:12  阅读:181  来源: 互联网

标签:retry 重试 result import guava com condition


失败重试guava-retry

pom依赖:

<dependency>
    <groupId>com.github.rholder</groupId>
    <artifactId>guava-retrying</artifactId>
    <version>2.0.0</version>
</dependency> 

RetryUtil.java工具类

import com.github.rholder.retry.Attempt;
import com.github.rholder.retry.RetryListener;
import com.github.rholder.retry.Retryer;
import com.github.rholder.retry.RetryerBuilder;
import com.github.rholder.retry.StopStrategies;
import com.github.rholder.retry.WaitStrategies;
import com.google.common.base.Predicate;
import org.apache.commons.lang.time.StopWatch;

import java.text.MessageFormat;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

public class RetryUtil {

    private static final Log log = new Log(RetryUtil.class);

    /**
     * 根据输入的condition重复做task,在规定的次数内达到condition则返回,
     * 如果超过retryTimes则返回null, 重试次数,整个重试时间以及retry exception都会记录log
     *
     * @param condition  重试条件,比如接口返回errorCode为处理中,或不是最终需要的结果
     * @param task       重试做的任务
     * @param sleepTime  重试间隔时间,单位毫秒
     * @param retryTimes 最大重试次数
     * @return targetBean
     */
    public static <V> Optional<V> retry(Predicate<V> condition, Callable<V> task, int sleepTime, int retryTimes) {
        Optional<V> result = Optional.empty();
        StopWatch stopWatch = new StopWatch();
        try {
            stopWatch.start();
            Retryer<V> retry = RetryerBuilder.<V>newBuilder()
                    // 默认任务执行过程中发生异常自动重试
                    .retryIfException()
                    // 重试条件(按照业务场景)
                    .retryIfResult(condition)
                    // 等待策略
                    .withWaitStrategy(WaitStrategies.fixedWait(sleepTime, TimeUnit.MILLISECONDS))
                    // 重试策略
                    .withStopStrategy(StopStrategies.stopAfterAttempt(retryTimes))
                    // 重试监听器
                    .withRetryListener(new RetryListener() {
                        @Override
                        public <V> void onRetry(Attempt<V> attempt) {
                            // 记录重试次数和异常信息
                            log.info(MessageFormat.format("{0}th retry", attempt.getAttemptNumber()));
                            if (attempt.hasException()) {
                                log.error(MessageFormat.format("retry exception:{0}", attempt.getExceptionCause()));
                            }
                        }
                    }).build();

            // 开始执行重试任务
            result = Optional.ofNullable(retry.call(task));
        } catch (Exception e) {
            log.error("retry fail:", e);
        } finally {
            stopWatch.stop();
            log.info("retry execute time" + stopWatch.getTime());
        }
        return result;
    }
}

调用方法:

// 失败重试条件
Predicate<String> condition = result -> Objects.nonNull(result)
        && 20000 != JSONObject.parseObject(result).getIntValue("subCode") || Objects.isNull(result);

Optional<String> result = RetryUtil.retry(
        condition, // 重试条件
        () -> 方法内容,
        sleepTime,
        retryTimes);

if (!result.isPresent() || 20000 != JSONObject.parseObject(result.get()).getIntValue("subCode")) {
    log.error("重试失败");
}

参考文档:

https://segmentfault.com/a/1190000037700698

标签:retry,重试,result,import,guava,com,condition
来源: https://blog.csdn.net/qq_41474525/article/details/122062039

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有