@Test public void should_execute_number_of_times() { AtomicInteger counter = new AtomicInteger(); DelegateRetryVoid<VitamRuntimeException> delegate = () -> { counter.incrementAndGet(); if (counter.get() < 3) { throw new VitamRuntimeException("throw"); } }; RetryableParameters parameters = new RetryableParameters(3, 1, 1, 1, MILLISECONDS); RetryableOnException<Void, VitamRuntimeException> retryable = new RetryableOnException<>(parameters); retryable.execute(delegate); assertThat(counter.get()).isEqualTo(3); }
@Override public void execute(DelegateRetryVoid<E> delegate) throws E { while (counter.getAndIncrement() < param.getNbRetry()) { try { delegate.call(); return; } catch (Exception e) { boolean attemptExceedNbRetry = counter.get() >= param.getNbRetry(); boolean shouldStopRetry = retryOn.negate().test(e); if (attemptExceedNbRetry || shouldStopRetry) { throw e; } sleep(counter.get(), delegate.toString(), param, randomSleep, onException, e); } } throw new IllegalStateException("Unreachable statement."); }
RetryableOnException implements Retryable<T, E> { @Override public void execute(DelegateRetryVoid<E> delegate) throws E { while (counter.getAndIncrement() < param.getNbRetry()) { try { delegate.call(); return; } catch (Exception e) { boolean attemptExceedNbRetry = counter.get() >= param.getNbRetry(); boolean shouldStopRetry = retryOn.negate().test(e); if (attemptExceedNbRetry || shouldStopRetry) { throw e; } sleep(counter.get(), delegate.toString(), param, randomSleep, onException, e); } } throw new IllegalStateException("Unreachable statement."); } }
RetryableOnException implements Retryable<T, E> { @Override public void execute(DelegateRetryVoid<E> delegate) throws E { while (counter.getAndIncrement() < param.getNbRetry()) { try { delegate.call(); return; } catch (Exception e) { boolean attemptExceedNbRetry = counter.get() >= param.getNbRetry(); boolean shouldStopRetry = retryOn.negate().test(e); if (attemptExceedNbRetry || shouldStopRetry) { throw e; } sleep(counter.get(), delegate.toString(), param, randomSleep, onException, e); } } throw new IllegalStateException("Unreachable statement."); } RetryableOnException(RetryableParameters param, Predicate<Exception> retryOn, Consumer<Exception> onException);  RetryableOnException(RetryableParameters param, Predicate<Exception> retryOn);  RetryableOnException(RetryableParameters param); }
RetryableOnException implements Retryable<T, E> { @Override public void execute(DelegateRetryVoid<E> delegate) throws E { while (counter.getAndIncrement() < param.getNbRetry()) { try { delegate.call(); return; } catch (Exception e) { boolean attemptExceedNbRetry = counter.get() >= param.getNbRetry(); boolean shouldStopRetry = retryOn.negate().test(e); if (attemptExceedNbRetry || shouldStopRetry) { throw e; } sleep(counter.get(), delegate.toString(), param, randomSleep, onException, e); } } throw new IllegalStateException("Unreachable statement."); } RetryableOnException(RetryableParameters param, Predicate<Exception> retryOn, Consumer<Exception> onException);  RetryableOnException(RetryableParameters param, Predicate<Exception> retryOn);  RetryableOnException(RetryableParameters param); @Override T exec(DelegateRetry<T, E> delegate); @Override void execute(DelegateRetryVoid<E> delegate); }
RetryableOnException implements Retryable<T, E> { @Override public void execute(DelegateRetryVoid<E> delegate) throws E { while (counter.getAndIncrement() < param.getNbRetry()) { try { delegate.call(); return; } catch (Exception e) { boolean attemptExceedNbRetry = counter.get() >= param.getNbRetry(); boolean shouldStopRetry = retryOn.negate().test(e); if (attemptExceedNbRetry || shouldStopRetry) { throw e; } sleep(counter.get(), delegate.toString(), param, randomSleep, onException, e); } } throw new IllegalStateException("Unreachable statement."); } RetryableOnException(RetryableParameters param, Predicate<Exception> retryOn, Consumer<Exception> onException);  RetryableOnException(RetryableParameters param, Predicate<Exception> retryOn);  RetryableOnException(RetryableParameters param); @Override T exec(DelegateRetry<T, E> delegate); @Override void execute(DelegateRetryVoid<E> delegate);  }