[Java]自定义重试工具类

Java重试工具类,零依赖。可配置项:接受的异常类型、返回值校验、最大重试次数、重试间隔时间。

1 重试工具类 RetryUtils源码

RetryUtils:

使用了lombok的@Slf4j注解用于打印日志,不用可移除。

java 复制代码
import com.example.exception.RetryException;

import lombok.extern.slf4j.Slf4j;

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

/**
 * <h2>重试工具类</h2>
 *
 * @author GFire
 * @since 2025/4/22 17:58
 */
@Slf4j
public abstract class RetryUtils {
    /**
     * 失败重试
     *
     * @param task            执行的任务,无返回值
     * @param acceptException 可接受的异常类型,执行的任务抛出此异常(及其子类)则失败重试。null表示不接受任何异常
     * @param maxRetryCount   最大重试次数
     * @param waitTime        重试间隔等待时间, 单位毫秒, <=0则不等待
     * @throws RetryException 如果任务重试超过最大次数、或抛出不可接受的异常,则统一包装抛出RetryException
     */
    public static void doWithRetry(Runnable task, Class<? extends Throwable> acceptException, int maxRetryCount, int waitTime) {
        if (task == null) {
            throw new IllegalArgumentException("task can not be null");
        }

        doWithRetry(() -> {
            task.run();
            return 1;
        }, i -> i == 1, acceptException, maxRetryCount, waitTime);
    }

    /**
     * 失败重试
     *
     * @param task            执行的任务,有返回值
     * @param isValid         判断任务返回值是否合法,不合法则失败重试
     * @param acceptException 可接受的异常类型,执行的任务抛出此异常(及其子类)则失败重试。null表示不接受任何异常
     * @param maxRetryCount   最大重试次数
     * @param waitTime        重试间隔等待时间, 单位毫秒, <=0则不等待
     * @return supplier执行结果
     * @throws RetryException 如果任务重试超过最大次数、或抛出不可接受的异常,则统一包装抛出RetryException
     */
    public static <T> T doWithRetry(Supplier<T> task, Predicate<T> isValid, Class<? extends Throwable> acceptException, int maxRetryCount, int waitTime) {
        if (task == null) {
            throw new IllegalArgumentException("task can not be null");
        }
        if (isValid == null) {
            throw new IllegalArgumentException("isValid can not be null");
        }
        if (maxRetryCount <= 0) {
            throw new IllegalArgumentException("maxRetryCount must be > 0");
        }

        T result = null;
        for (int tryCount = 1; tryCount <= maxRetryCount; tryCount++) {
            try {
                result = task.get();
                if (isValid.test(result)) {
                    return result;
                } else {
                    log.error("result invalid, tryCount: {}, result: {}", tryCount, result);
                }
            } catch (Throwable e) {
                handleException(e, acceptException, maxRetryCount, tryCount);
            }

            if (waitTime > 0 && tryCount < maxRetryCount) {
                sleep(waitTime); // 等待一段时间后重试
            }
        }
        throw new RetryException("result: " + result);
    }

    private static void handleException(Throwable e, Class<? extends Throwable> acceptException, int maxRetryCount, int tryCount) {
        log.error("error, tryCount: {}, Exception: ", tryCount, e);
        if (acceptException != null && acceptException.isInstance(e)) {
            if (tryCount == maxRetryCount) { // 最后一次重试仍失败,则抛出
                throw new RetryException(e);
            }
        } else { // 不可接受的异常,直接抛出
            throw new RetryException(e);
        }
    }

    private static void sleep(int waitTime) {
        try {
            Thread.sleep(waitTime);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new RetryException(e);
        }
    }
}

RetryException:

java 复制代码
/**
 * <h2>重试异常</h2>
 *
 * @author GFire
 * @since 2025/4/23 11:20
 */
public class RetryException extends RuntimeException {
    public RetryException(String message) {
        super(message);
    }

    public RetryException(Throwable cause) {
        super(cause);
    }
}

2 使用方式

示例1:任务无返回值

java 复制代码
 // 模拟调用API接口,失败重试
RetryUtils.doWithRetry(() -> apiService.query(), Exception.class, 3, 2000);

解释:任务apiService.query()无返回值、接受Exception异常、最大重试次数为3、重试间隔2秒

示例2:任务有返回值、需校验返回值

java 复制代码
/**
 * 模拟发送通知
 */
public boolean send(String content) {
    try {
        RetryUtils.doWithRetry(() -> noticeService.send(content), this::isValid, Exception.class, 3, 2000);
        return true;
    } catch (RetryException e) {
        log.error("send error: {}", e.toString());
    }
    return false;
}

private boolean isValid(String res) {
    if (StringUtils.isNotEmpty(res)) {
        JSONObject response = JSON.parseObject(res);
        return "ok".equals(response.getString("status"));
    }
    return false;
}

解释:任务noticeService.send(content)有String类型的返回值、isValid方法判断返回值是否合法、接受Exception异常、最大重试次数为3、重试间隔2秒

示例3:任务有返回值、无需校验返回值

java 复制代码
RetryUtils.doWithRetry(() -> noticeService.send(), (res) -> true, Exception.class, 3, 2000);

解释:任务noticeService.send()有String类型的返回值、(res) -> true认为任意返回值都合法(即无校验)、接受Exception异常、最大重试次数为3、重试间隔2秒

相关推荐
黎雁·泠崖几秒前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
qq_12498707531 小时前
基于SSM的动物保护系统的设计与实现(源码+论文+部署+安装)
java·数据库·spring boot·毕业设计·ssm·计算机毕业设计
Coder_Boy_1 小时前
基于SpringAI的在线考试系统-考试系统开发流程案例
java·数据库·人工智能·spring boot·后端
Mr_sun.1 小时前
Day06——权限认证-项目集成
java
瑶山1 小时前
Spring Cloud微服务搭建四、集成RocketMQ消息队列
java·spring cloud·微服务·rocketmq·dashboard
abluckyboy2 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法
2301_818732062 小时前
前端调用控制层接口,进不去,报错415,类型不匹配
java·spring boot·spring·tomcat·intellij-idea
2501_941982052 小时前
深度对比:Java、Go、Python 实现企微外部群推送,哪个效率更高?
java·golang·企业微信
马猴烧酒.2 小时前
【面试八股|JAVA多线程】JAVA多线程常考面试题详解
java·服务器·数据库
sino爱学习3 小时前
高性能线程池实践:Dubbo EagerThreadPool 设计与应用
java·后端