重试工具类

  1. 使用 guava retry 工具包
com.github.rholder guava-retrying 2.0.0
  1. 简单的 retry 工具类,如下:
public abstract class RetryOperation {
    public boolean execute(int retryCount, int waitTime, TimeUnit timeUnit) {
        int count = 0;
        boolean isSuccess = false;
        while (retryCount > count) {
            try {
                isSuccess = process();
                if (isSuccess) {
                    break;
                }
            } catch (Exception e) {
                LOGGER.info("execute exception", e);
            }
            try {
                timeUnit.sleep(waitTime);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new RuntimeException(e);
            }
            count++;
            LOGGER.info("retry count {}", count);
        }
        return isSuccess;
    }

    public abstract boolean process() throws Exception;
posted @ 2023-09-04 16:52  大熊猫同学  阅读(21)  评论(0编辑  收藏  举报