Class TaskUtils

java.lang.Object
cn.dinodev.spring.commons.utils.TaskUtils

public class TaskUtils extends Object
Author:
Cody Lu
  • Constructor Details

    • TaskUtils

      public TaskUtils()
  • Method Details

    • exec

      public static void exec(Runnable task, int attempts)
      无参数,无返回的
      Parameters:
      task - 任务
      attempts - 重试次数
    • exec

      public static void exec(Runnable task, int attempts, Duration nap)
      无参数,无返回的
      Parameters:
      task - 任务
      attempts - 重试次数
      nap - 重试间隔时长
    • exec

      public static <T> void exec(Consumer<T> task, T param, int attempts)
      task接受一个参数,无返回值
      Type Parameters:
      T - 参数类型
      Parameters:
      task - 任务
      param - 传给任务的参数
      attempts - 重试次数
    • exec

      public static <T> void exec(Consumer<T> task, T param, int attempts, Duration nap)
      task接受一个参数,无返回值
      Type Parameters:
      T - 参数类型
      Parameters:
      task - 任务
      param - 传给任务的参数
      attempts - 重试次数
      nap - 重试间隔时长
    • exec

      public <T, U> void exec(BiConsumer<T,U> task, T paramFirst, U paramSecond, int attempts)
      task接受两个参数,无返回值
      Type Parameters:
      T - 第一个参数类型
      U - 第二个参数类型
      Parameters:
      task - 任务
      paramFirst - 第一个参数
      paramSecond - 第二个参数
      attempts - 重试次数
    • exec

      public <T, U> void exec(BiConsumer<T,U> task, T paramFirst, U paramSecond, int attempts, Duration nap)
      task接受两个参数,无返回值
      Type Parameters:
      T - 第一个参数类型
      U - 第二个参数类型
      Parameters:
      task - 任务
      paramFirst - 第一个参数
      paramSecond - 第二个参数
      attempts - 重试次数
      nap - 重试间隔时长
    • exec

      @SafeVarargs public static <R> Promise<R> exec(Callable<R> task, Predicate<R> checker, int attempts, Class<? extends RuntimeException>... throwOutExceptions)
      无参数,有返回的任务调用
      Type Parameters:
      R - 返回值类型
      Parameters:
      task - 任务
      checker - 检查task返回值是否符合要求,不符合则重试
      attempts - 重试次数
      throwOutExceptions - 直接抛出的异常的类型
    • exec

      @SafeVarargs public static <R> Promise<R> exec(Callable<R> task, Predicate<R> checker, int attempts, Duration nap, Class<? extends RuntimeException>... throwOutExceptions)
      无参数,有返回的任务调用
      Type Parameters:
      R - 返回值类型
      Parameters:
      task - 任务
      checker - 检查task返回值是否符合要求,不符合则重试
      attempts - 重试次数, 1表示最多执行一次
      nap - 重试间隔时长
      throwOutExceptions - 直接抛出的异常的类型
    • exec

      @SafeVarargs public <T, R> Promise<R> exec(Function<T,R> task, T param, Predicate<R> checker, int attempts, Class<? extends RuntimeException>... throwOutExceptions)
      task接受一个参数,并返回执行结果
      Type Parameters:
      T - 参数类型
      R - 返回值类型
      Parameters:
      task - 任务
      param - 传给异步任务的参数
      checker - 检查task返回值是否符合要求,不符合则重试
      attempts - 重试次数, 1表示最多执行一次
      throwOutExceptions - 直接抛出的异常的类型
      Returns:
    • exec

      @SafeVarargs public <T, R> Promise<R> exec(Function<T,R> task, T param, Predicate<R> checker, int attempts, Duration nap, Class<? extends RuntimeException>... throwOutExceptions)
      task接受一个参数,并返回执行结果
      Type Parameters:
      T - 参数类型
      R - 返回值类型
      Parameters:
      task - 任务
      param - 传给异步任务的参数
      checker - 检查task返回值是否符合要求,不符合则重试
      attempts - 重试次数, 1表示最多执行一次
      nap - 重试间隔时长
      throwOutExceptions - 直接抛出的异常的类型
      Returns:
    • exec

      @SafeVarargs public <T, U, R> Promise<R> exec(BiFunction<T,U,R> task, T paramFirst, U paramSecond, Predicate<R> checker, int attempts, Class<? extends RuntimeException>... throwOutExceptions)
      task接受两个参数,并返回执行结果
      Type Parameters:
      T - 第一个参数类型
      U - 第二个参数类型
      R - 返回结果类型
      Parameters:
      task - 任务
      paramFirst - 第一个参数
      paramSecond - 第二个参数
      checker - 检查task返回值是否符合要求,不符合则重试
      attempts - 重试次数, 1表示最多执行一次
      throwOutExceptions - 直接抛出的异常的类型
      Returns:
    • exec

      @SafeVarargs public <T, U, R> Promise<R> exec(BiFunction<T,U,R> task, T paramFirst, U paramSecond, Predicate<R> checker, int attempts, Duration nap, Class<? extends RuntimeException>... throwOutExceptions)
      task接受两个参数,并返回执行结果
      Type Parameters:
      T - 第一个参数类型
      U - 第二个参数类型
      R - 返回结果类型
      Parameters:
      task - 任务
      paramFirst - 第一个参数
      paramSecond - 第二个参数
      checker - 检查task返回值是否符合要求,不符合则重试
      attempts - 重试次数, 1表示最多执行一次
      nap - 重试间隔时长
      throwOutExceptions - 直接抛出的异常的类型
      Returns:
    • nap

      public static boolean nap(Duration nap)
      线程休息一会
      Parameters:
      nap - 时间长度
      Returns:
      true:如果没被打断,false:如果被打断。