/** * An attempt of a call, which resulted either in a result returned by the call, * or in a Throwable thrown by the call. * * @param <V> The type returned by the wrapped callable. * @author JB */ publicinterfaceAttempt<V>
/** * Executes the given callable. If the rejection predicate * accepts the attempt, the stop strategy is used to decide if a new attempt * must be made. Then the wait strategy is used to decide how much time to sleep * and a new attempt is made. * * @param callable the callable task to be executed * @return the computed result of the given callable * @throws ExecutionException if the given callable throws an exception, and the * rejection predicate considers the attempt as successful. The original exception * is wrapped into an ExecutionException. * @throws RetryException if all the attempts failed before the stop strategy decided * to abort, or the thread was interrupted. Note that if the thread is interrupted, * this exception is thrown and the thread's interrupt status is set. */ public V call(Callable<V> callable)throws ExecutionException, RetryException { long startTime = System.nanoTime(); //说明:根据 attemptNumber 进行循环 —— 也就是重试多少次 for (int attemptNumber = 1; ; attemptNumber++) { //说明:进入方法不等待,立即执行一次 Attempt<V> attempt; try { //说明:执行 callable 中的具体业务 //attemptTimeLimiter 限制了每次尝试等待的时常 V result = attemptTimeLimiter.call(callable); //利用调用结果构造新的 attempt attempt = new ResultAttempt<V>(result, attemptNumber, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime)); } catch (Throwable t) { attempt = new ExceptionAttempt<V>(t, attemptNumber, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime)); }
//说明:遍历自定义的监听器 for (RetryListener listener : listeners) { listener.onRetry(attempt); }
//说明:判断是否满足重试条件,来决定是否继续等待并进行重试 if (!rejectionPredicate.apply(attempt)) { return attempt.get(); }
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !