java 重试策略 java代码如何优雅编写重试架子 简单重试代码 轻量级重试代码

1. 抽象类:

    具体业务实现doProcess()即可。可继承CallBack类,亦可new Callback类。再不明白可查看下方demo

package com.nami.retry;

/**
 * 描述:
 *
 * @Author: lbc
 * @Date: 2024-08-04 15:19
 * @email: 594599620@qq.com
 * @Description: keep coding
 */
public abstract class Callback {

    /**
     * do
     */
    public abstract RetryResult doProcess();

}

2.  执行结果:

package com.nami.retry;

/**
 * 描述:
 *
 * @Author: lbc
 * @Date: 2024-08-04 15:19
 * @email: 594599620@qq.com
 * @Description: keep coding
 */
public class RetryResult {

    private boolean isRetry;

    private Object obj;

    public RetryResult(boolean isRetry, Object obj) {
        this.isRetry = isRetry;
        this.obj = obj;
    }

    public RetryResult(boolean isRetry) {
        this.isRetry = isRetry;
    }

    public boolean isRetry() {
        return isRetry;
    }

    public void setRetry(boolean retry) {
        isRetry = retry;
    }

    public Object getObj() {
        return obj;
    }

    public void setObj(Object obj) {
        this.obj = obj;
    }

    public static RetryResult ofResult(boolean retry, Object obj) {
        return new RetryResult(retry, obj);
    }

    public static RetryResult ofResult(boolean isRetry) {
        return new RetryResult(isRetry);
    }

}

3. 执行器:

package com.nami.retry;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * 描述:
 *
 * @Author: lbc
 * @Date: 2024-08-04 15:23
 * @email: 594599620@qq.com
 * @Description: keep coding
 */
@Slf4j
public class RetryExecutor {

    public static Object execute(int retryCount, Callback callback) {
        for (int curRetryCount = 0; curRetryCount < retryCount; curRetryCount++) {
            RetryResult retryResult = callback.doProcess();
            if (retryResult.isRetry()) {
                continue;
            }
            return retryResult.getObj();
        }
        return null;
    }

    /**
     * 改变for写法,变高级点
     *
     * @param retryCount
     * @param callback
     * @return
     */
    public static Object execute2(int retryCount, Callback callback) {
        int curRetryCount = 0;
        for (;;) {
            if (curRetryCount >= retryCount) {
                break;
            }
            curRetryCount++;
            RetryResult retryResult = callback.doProcess();
            if (retryResult.isRetry()) {
                continue;
            }
            return retryResult.getObj();
        }
        return null;
    }


    /**
     * execute
     *
     * @param retryCount   重试次数
     * @param millsSeconds 线程休眠时间,单位 ms
     * @param callback     执行方法
     * @return
     */
    public static Object execute(int retryCount, long millsSeconds, Callback callback) {
        if (retryCount <= 0) {
            throw new IllegalArgumentException("执行次数必须大于0!");
        }
        RetryResult retryResult = null;
        int executeCount = 0;
        while (true) {
            if (executeCount >= retryCount) {
                break;
            }
            executeCount++;
            // 进入实现类方法,执行具体业务逻辑
            retryResult = callback.doProcess();
            if (retryResult.isRetry()) {
                // 如果需要重试,并且需要间隔时间,那么执行线程等待
                if (millsSeconds > 0) {
                    try {
                        TimeUnit.MILLISECONDS.sleep(millsSeconds);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                continue;
            }
            return retryResult.getObj();
        }
        if (retryResult != null && retryResult.getObj() != null) {
            return retryResult.getObj();
        }
        return null;
    }


    public static void main(String[] args) {
        Object result = execute(3, 3000, new Callback() {
                    @Override
                    public RetryResult doProcess() {
                        log.info("[retry-do] hello, world!");
//                        String result = HttpUtil.get("https://www.baidu.com");
//                        log.info("[retry-do] result: {}", result);
                        return RetryResult.ofResult(false, "结果");
//                        return RetryResult.ofResult(true);
                    }
                }
        );
        log.info("[retry-do] result={}", result);

    }

}

注:笔者水平有限,如有错误,欢迎指正!

4. 如果需要自己的策略,可在此基础,进行魔改!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值