美文网首页
重试工具类

重试工具类

作者: 团长plus | 来源:发表于2021-03-24 14:46 被阅读0次
package cn.freemud.es.manager.retry;


import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author hongwang.zhang 重试工具类
 */
@Slf4j
public class Retry<T> {
    /**
     * 默认重试次数
     */
    private int maxRetryTimes = 3;
    /**
     * 重试的 对象
     */
    private Operation<T> operation;
    /**
     * 判断是否成功
     */
    private List<Judgement<T>> judgements;

    /**
     * 异常 call
     */
    private ExceptionCallback exceptionCallback;

    public Retry<T> maxRetryTimes(int maxRetryTimes) {
        this.maxRetryTimes = maxRetryTimes;
        return this;
    }

    public Retry<T> operation(Operation<T> operation) {
        this.operation = operation;
        return this;
    }

    public Retry<T> judgement(Judgement<T> judgement) {
        if (this.judgements == null) {
            this.judgements = new ArrayList<>();
        }
        this.judgements.add(judgement);
        return this;
    }

    public Retry<T> exceptionCallback(ExceptionCallback exceptionCallback) {
        this.exceptionCallback = exceptionCallback;
        return this;
    }

    public T execute(){
        return  execute(this.maxRetryTimes,this.operation,this.judgements,this.exceptionCallback);
    }
    /**
     *
     * todo  1、池化 2、方法的超时时间
     * @param maxRetryTimes 重试次数
     * @param operation     重试的方法
     * @param judgements    校验
     * @param exceptionCallback    异常回调
     * @return
     * @throws Exception 重试次数达到 指定次数后如果有异常抛出异常
     */

    public T execute(int maxRetryTimes, Operation<T> operation, List<Judgement<T>> judgements,ExceptionCallback exceptionCallback) {
        if (Objects.isNull(operation)) {
            throw new NullPointerException("operation not is null");
        }
        boolean isBreak = false;
        T operate =null;
        int times = 1;
        while(times <= maxRetryTimes && (!isBreak)){
            try {
                operate = operation.operate();
            }catch (Exception e){
                log.error("Retry.execute,times:{},exception:{}", times,new Gson().toJsonTree(e));
                if(Objects.nonNull(exceptionCallback)){
                    exceptionCallback.call(e);
                }

            }
            if (null != judgements && judgements.size() > 0) {
                boolean judge = false;
                for (Judgement<T> judgement : judgements) {
                     judge = judgement.judge(operate);
                    if(!judge){
                        times ++;
                        break;
                    }
                }
                if(judge){
                    isBreak = true;
                }
            }else {
                isBreak = true;
            }
        }
        return  operate;





    }

    public interface Operation<T> {
        /**
         * 返回的对象
         *
         * @return
         */
        T operate() throws IOException;
    }

    public interface ExceptionCallback<T> {
        /**
         * 异常处理
         * @return
         */
        void call(Exception e);
    }

    /**
     * 断言
     *
     * @param <T>
     */
    public interface Judgement<T> {
        /**
         * 判断是否成功
         *
         * @param t
         * @return
         */
        boolean judge(T t);
    }

    /**
     * 非空判断
     */
    public class IsNullJudgement implements Judgement<T> {

        @Override
        public boolean judge(T t) {
            return t == null;
        }
    }
}



相关文章

网友评论

      本文标题:重试工具类

      本文链接:https://www.haomeiwen.com/subject/auhhhltx.html