다시 시도 utils

3174 단어 자바Tools
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;

import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * @description   utils
 */
public class RetryUtils {

    /**
     *     (    3 ),   ,  null          
     */
    public static  R retryDo(Function doFunction, T doFunctionRequest, Predicate checkFunction, Logger log, String logDesc) {
        return retryDo(doFunction, doFunctionRequest, checkFunction, 2, log, logDesc);
    }

    /**
     *     (    3 ),   ,  null          
     */
    public static  R retryDo(Supplier supplier, Predicate checkFunction, Logger log, String logDesc) {
        return retryDo(supplier, checkFunction, 2, log, logDesc);
    }


    /**
     *   null          
     */
    public static  R retryDo(Function doFunction, T doFunctionRequest, Predicate checkFunction, Integer maxRetryTimes, Logger log, String logDesc) {
        R result = null;

        boolean checkBool = false;
        int retryTimes = 0;
        while (!checkBool && retryTimes <= maxRetryTimes) {
            try {
                log.info("{},request={},retryTimes={}", logDesc, JSONObject.toJSON(doFunctionRequest), retryTimes);
                result = doFunction.apply(doFunctionRequest);
                log.info("{},request={},retryTimes={},result={}", logDesc, JSONObject.toJSON(doFunctionRequest), retryTimes, JSONObject.toJSON(result));
                checkBool = checkFunction.test(result);
                if (!checkBool) {
                    log.info("{},request={},retryTimes={},result={},     ", logDesc, JSONObject.toJSON(doFunctionRequest), retryTimes, JSONObject.toJSON(result));
                }
            } catch (Throwable throwable) {
                log.error(logDesc + "-      ", throwable);
            }
            retryTimes++;
        }
        if (!checkBool) {
            return null;
        }
        return result;
    }

    /**
     *   null          
     */
    public static  R retryDo(Supplier supplier, Predicate checkFunction, Integer maxRetryTimes, Logger log, String logDesc) {
        R result = null;

        Boolean checkBool = false;
        int retryTimes = 0;
        while (!checkBool && retryTimes <= maxRetryTimes) {
            try {
                log.info("{},retryTimes={}", logDesc, retryTimes);
                result = supplier.get();
                log.info("{},retryTimes={},result={}", logDesc, retryTimes, JSONObject.toJSON(result));
                checkBool = checkFunction.test(result);
                if (!checkBool) {
                    log.info("{},retryTimes={},result={},     ", logDesc, retryTimes, JSONObject.toJSON(result));
                }
            } catch (Throwable throwable) {
                log.error(logDesc + "-      ", throwable);
            }
            retryTimes++;
        }
        if (!checkBool) {
            return null;
        }
        return result;
    }

}

좋은 웹페이지 즐겨찾기