Class RetryUtil


  • public final class RetryUtil
    extends Object
    • Constructor Detail

      • RetryUtil

        public RetryUtil()
    • Method Detail

      • executeWithRetry

        public static <T> T executeWithRetry​(Callable<T> callable,
                                             int retryTimes,
                                             long sleepTimeInMilliSecond,
                                             boolean exponential)
                                      throws Exception
        重试次数工具方法.
        Type Parameters:
        T - 返回值类型
        Parameters:
        callable - 实际逻辑
        retryTimes - 最大重试次数(>1)
        sleepTimeInMilliSecond - 运行失败后休眠对应时间再重试
        exponential - 休眠时间是否指数递增
        Returns:
        经过重试的callable的执行结果
        Throws:
        Exception
      • executeWithRetry

        public static <T> T executeWithRetry​(Callable<T> callable,
                                             int retryTimes,
                                             long sleepTimeInMilliSecond,
                                             boolean exponential,
                                             List<Class<?>> retryExceptionClasss)
                                      throws Exception
        重试次数工具方法.
        Type Parameters:
        T - 返回值类型
        Parameters:
        callable - 实际逻辑
        retryTimes - 最大重试次数(>1)
        sleepTimeInMilliSecond - 运行失败后休眠对应时间再重试
        exponential - 休眠时间是否指数递增
        retryExceptionClasss - 出现指定的异常类型时才进行重试
        Returns:
        经过重试的callable的执行结果
        Throws:
        Exception
      • asyncExecuteWithRetry

        public static <T> T asyncExecuteWithRetry​(Callable<T> callable,
                                                  int retryTimes,
                                                  long sleepTimeInMilliSecond,
                                                  boolean exponential,
                                                  long timeoutMs,
                                                  ThreadPoolExecutor executor)
                                           throws Exception
        在外部线程执行并且重试。每次执行需要在timeoutMs内执行完,不然视为失败。 执行异步操作的线程池从外部传入,线程池的共享粒度由外部控制。比如,HttpClientUtil共享一个线程池。

        限制条件:仅仅能够在阻塞的时候interrupt线程

        Type Parameters:
        T - 返回值类型
        Parameters:
        callable - 实际逻辑
        retryTimes - 最大重试次数(>1)
        sleepTimeInMilliSecond - 运行失败后休眠对应时间再重试
        exponential - 休眠时间是否指数递增
        timeoutMs - callable执行超时时间,毫秒
        executor - 执行异步操作的线程池
        Returns:
        经过重试的callable的执行结果
        Throws:
        Exception
      • createThreadPoolExecutor

        public static ThreadPoolExecutor createThreadPoolExecutor()
        创建异步执行的线程池。特性如下: core大小为0,初始状态下无线程,无初始消耗。 max大小为5,最多五个线程。 60秒超时时间,闲置超过60秒线程会被回收。 使用SynchronousQueue,任务不会排队,必须要有可用线程才能提交成功,否则会RejectedExecutionException。
        Returns:
        线程池