RetryUtils

下面展示一些 内联代码片

package my.common.utils;

import java.util.function.Function;

import io.github.resilience4j.core.IntervalFunction;
import io.github.resilience4j.retry.Retry;
import io.github.resilience4j.retry.RetryConfig;

/**
 * RetryUtils
 */
public class RetryUtils {

    /**
     * 重试
     */
    public static <T, R> R retry(Function<T, R> func, T request, String name, int maxAttempts,
        long initialIntervalMillis) {

        return retry(func, request, name, maxAttempts, initialIntervalMillis, IntervalFunction.DEFAULT_MULTIPLIER,
            IntervalFunction.DEFAULT_RANDOMIZATION_FACTOR);
    }

    /**
     * 重试
     */
    public static <T, R> R retry(Function<T, R> func, T request, String name, int maxAttempts,
                                 long initialIntervalMillis, Class<? extends Throwable> exception) {

        return retry(func, request, name, maxAttempts, initialIntervalMillis, IntervalFunction.DEFAULT_MULTIPLIER,
                IntervalFunction.DEFAULT_RANDOMIZATION_FACTOR,exception);
    }


    /**
     * 重试
     */
    public static <T, R> R retry(Function<T, R> func, T request, String name, int maxAttempts,
                                 long initialIntervalMillis, double multiplier, double randomizationFactor) {

        IntervalFunction intervalFn =
                IntervalFunction.ofExponentialRandomBackoff(initialIntervalMillis, multiplier, randomizationFactor);

        RetryConfig retryConfig = RetryConfig.custom()
                .maxAttempts(maxAttempts)
                .intervalFunction(intervalFn)
                .build();

        Retry retry = Retry.of(name, retryConfig);

        Function<T, R> retryFn = Retry.decorateFunction(retry, func);

        return retryFn.apply(request);
    }

    /**
     * 重试
     */
    public static <T, R> R retry(Function<T, R> func, T request, String name, int maxAttempts,
                                 long initialIntervalMillis, double multiplier, double randomizationFactor,
                                 Class<? extends Throwable> exception) {

        IntervalFunction intervalFn =
                IntervalFunction.ofExponentialRandomBackoff(initialIntervalMillis, multiplier, randomizationFactor);

        RetryConfig retryConfig = RetryConfig.custom()
                .maxAttempts(maxAttempts)
                .retryExceptions(exception)
                .intervalFunction(intervalFn)
                .build();

        Retry retry = Retry.of(name, retryConfig);

        Function<T, R> retryFn = Retry.decorateFunction(retry, func);

        return retryFn.apply(request);
    }

}
        try {
            RetryUtils.retry(this::doGet,objectDto,"RetryName", 3,
                    5000L, UserDefindException.class);
        }catch (UserDefindException e) {
            //TODO 处理
            }
        }
private boolean doGet(ObjectDTO objectDto) throws UserDefindException {
       
                throw new UserDefindException();

        return true;
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RetryUtils 是一个 Java 库,用于在失败的操作后进行自动重试。以下是一个 RetryUtils 的示例: ```java import com.github.rholder.retry.Retryer; import com.github.rholder.retry.RetryerBuilder; import com.github.rholder.retry.StopStrategies; import com.github.rholder.retry.WaitStrategies; import java.io.IOException; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; public class RetryUtilsExample { public static void main(String[] args) { Callable<Boolean> callable = () -> { // 这里可以放需要重试的操作 // 如果成功了,就返回 true,如果失败了,就抛出异常 // 例如,这里可以尝试打开一个网址,如果打开成功,就返回 true,否则抛出异常 throw new IOException("打开网址失败"); }; Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder() // 最多重试 3 次 .withStopStrategy(StopStrategies.stopAfterAttempt(3)) // 每次重试之间等待 1 秒钟 .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS)) .retryIfExceptionOfType(IOException.class) .build(); try { // 调用 retryer.call() 方法,会自动重试,直到达到最大次数或者成功 boolean result = retryer.call(callable); System.out.println("操作是否成功:" + result); } catch (ExecutionException | Retryer.RetryException e) { // 如果重试次数达到最大值,仍然失败,就会抛出 Retryer.RetryException 异常 // 如果重试过程中发生了其他异常,就会抛出 ExecutionException 异常 System.out.println("操作失败:" + e.getMessage()); } } } ``` 在上面的示例中,我们创建了一个 Callable 对象,它模拟了一个可能会失败的操作。我们使用 RetryerBuilder 创建了一个 Retryer 对象,设置了最大重试次数为 3 次,每次重试之间等待 1 秒钟。然后,我们调用 retryer.call(callable) 方法,让 Retryer 对象自动进行重试,直到达到最大次数或者成功。最后,我们根据操作是否成功的结果,输出相应的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值