重试工具类

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 {
    /**
     * 默认重试次数
     */
    private int maxRetryTimes = 3;
    /**
     * 重试的 对象
     */
    private Operation operation;
    /**
     * 判断是否成功
     */
    private List> judgements;

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

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

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

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

    public Retry 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 operation, List> 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 judgement : judgements) {
                     judge = judgement.judge(operate);
                    if(!judge){
                        times ++;
                        break;
                    }
                }
                if(judge){
                    isBreak = true;
                }
            }else {
                isBreak = true;
            }
        }
        return  operate;





    }

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

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

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

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

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



你可能感兴趣的:(重试工具类)