All Downloads are FREE. Search and download functionalities are using the official Maven repository.

matrix.boot.common.utils.RetryUtil Maven / Gradle / Ivy

There is a newer version: 2.1.11
Show newest version
package matrix.boot.common.utils;

import lombok.extern.slf4j.Slf4j;

/**
 * 重试工具
 * @author wangcheng
 * 2021/8/12
 **/
@Slf4j
public class RetryUtil {

    /**
     * 重试
     * @param retryMaxTimes 最大重试次数
     * @param callBack 回调函数
     * @param  传出参数类型
     * @return 传出参数集合
     */
    public static  T retry(int retryMaxTimes, CallBackNoArgs callBack) {
        //获取配置文件
        int failTime = 0;
        //定义返回参数
        T result = null;
        while (failTime < retryMaxTimes) {
            try {
                if (callBack != null) {
                    result = callBack.invoke();
                }
                return result;
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                failTime++;
            }
        }
        log.error("retry " + retryMaxTimes + "times failed");
        return null;
    }

    /**
     * 回调无参
     * @param 
     */
    public interface CallBackNoArgs {

        /**
         * 回调函数
         * @return 处理后需要返回的值
         */
        T invoke();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy