.retryIfResult(Predicates.not(Predicates.equalTo(Boolean.TRUE))) .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS)) .withStopStrategy(StopStrategies.stopAfterDelay(maxWait, timeUnit)) .build();
private static <T> Retryer<T> newFixedRetryer(Config config) { return RetryerBuilder.<T> newBuilder() .retryIfException(RETRY_EXCEPTION_PREDICATE) .withWaitStrategy(WaitStrategies.fixedWait(config.getLong(RETRY_INTERVAL_MS), TimeUnit.MILLISECONDS)) .withStopStrategy(StopStrategies.stopAfterDelay(config.getLong(RETRY_TIME_OUT_MS), TimeUnit.MILLISECONDS)) .build(); }
private static <T> Retryer<T> newExponentialRetryer(Config config) { return RetryerBuilder.<T> newBuilder() .retryIfException(RETRY_EXCEPTION_PREDICATE) .withWaitStrategy(WaitStrategies.exponentialWait(config.getLong(RETRY_MULTIPLIER), config.getLong(RETRY_INTERVAL_MS), TimeUnit.MILLISECONDS)) .withStopStrategy(StopStrategies.stopAfterDelay(config.getLong(RETRY_TIME_OUT_MS), TimeUnit.MILLISECONDS)) .build(); } }
/** * @param allocator Allocator * @param waitInterval Wait time between allocation attempts (ms) * @param maxWaitInterval Max time to wait for an allocation (ms) */ public RetryingAllocator(Allocator<V> allocator, long waitInterval, long maxWaitInterval) { this.allocator = allocator; this.retryer = RetryerBuilder.<V>newBuilder() .retryIfException() .withWaitStrategy(WaitStrategies.fixedWait(waitInterval, TimeUnit.MILLISECONDS)) .withStopStrategy(StopStrategies.stopAfterDelay(maxWaitInterval, TimeUnit.MILLISECONDS)) .withRetryListener(new RetryListener() { @Override public <V> void onRetry(Attempt<V> attempt) { if (attempt.hasException()) { log.error("Error allocating slot", attempt.getExceptionCause()); } } }) .build(); }
/** * Returns a stop strategy which stops after a given delay. If an * unsuccessful attempt is made, this {@link StopStrategy} will check if the * amount of time that's passed from the first attempt has exceeded the * given delay amount. If it has exceeded this delay, then using this * strategy causes the retrying to stop. * * @param delayInMillis the delay, in milliseconds, starting from first attempt * @return a stop strategy which stops after {@code delayInMillis} time in milliseconds * @deprecated Use {@link #stopAfterDelay(long, TimeUnit)} instead. */ @Deprecated public static StopStrategy stopAfterDelay(long delayInMillis) { return stopAfterDelay(delayInMillis, TimeUnit.MILLISECONDS); }
/** * Returns a stop strategy which stops after a given delay. If an * unsuccessful attempt is made, this {@link StopStrategy} will check if the * amount of time that's passed from the first attempt has exceeded the * given delay amount. If it has exceeded this delay, then using this * strategy causes the retrying to stop. * * @param delayInMillis the delay, in milliseconds, starting from first attempt * @return a stop strategy which stops after {@code delayInMillis} time in milliseconds * @deprecated Use {@link #stopAfterDelay(long, TimeUnit)} instead. */ @Deprecated public static StopStrategy stopAfterDelay(long delayInMillis) { return stopAfterDelay(delayInMillis, TimeUnit.MILLISECONDS); }
.retryIfResult(Predicates.not(Predicates.equalTo(Boolean.TRUE))) .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS)) .withStopStrategy(StopStrategies.stopAfterDelay(maxWait, timeUnit)) .build();
@Test public void testStopAfterDelayWithTimeUnit() { assertFalse(StopStrategies.stopAfterDelay(1, TimeUnit.SECONDS).shouldStop(failedAttempt(2, 999L))); assertTrue(StopStrategies.stopAfterDelay(1, TimeUnit.SECONDS).shouldStop(failedAttempt(2, 1000L))); assertTrue(StopStrategies.stopAfterDelay(1, TimeUnit.SECONDS).shouldStop(failedAttempt(2, 1001L))); }
private static <T> Retryer<T> newFixedRetryer(Config config) { return RetryerBuilder.<T> newBuilder() .retryIfException(RETRY_EXCEPTION_PREDICATE) .withWaitStrategy(WaitStrategies.fixedWait(config.getLong(RETRY_INTERVAL_MS), TimeUnit.MILLISECONDS)) .withStopStrategy(StopStrategies.stopAfterDelay(config.getLong(RETRY_TIME_OUT_MS), TimeUnit.MILLISECONDS)) .build(); }
@SuppressWarnings("unchecked") protected <T> Retryer<T> getRetryer(Predicate<? super T> predicate, Duration timeout, Duration interval) { return RetryerBuilder.<T> newBuilder() .retryIfResult(Predicates.not((Predicate<T>) predicate)) .retryIfRuntimeException() .withWaitStrategy(fixedWait(interval.getTime(), interval.getUnit())) .withStopStrategy(stopAfterDelay(timeout.getTime(), timeout.getUnit())) .build(); } }
@Test public void testStopAfterDelayWithMilliseconds() { assertFalse(StopStrategies.stopAfterDelay(1000, MILLISECONDS) .shouldStop(failedAttempt(2, 999L))); assertTrue(StopStrategies.stopAfterDelay(1000, MILLISECONDS) .shouldStop(failedAttempt(2, 1000L))); assertTrue(StopStrategies.stopAfterDelay(1000, MILLISECONDS) .shouldStop(failedAttempt(2, 1001L))); }
private static <T> Retryer<T> newExponentialRetryer(Config config) { return RetryerBuilder.<T> newBuilder() .retryIfException(RETRY_EXCEPTION_PREDICATE) .withWaitStrategy(WaitStrategies.exponentialWait(config.getLong(RETRY_MULTIPLIER), config.getLong(RETRY_INTERVAL_MS), TimeUnit.MILLISECONDS)) .withStopStrategy(StopStrategies.stopAfterDelay(config.getLong(RETRY_TIME_OUT_MS), TimeUnit.MILLISECONDS)) .build(); } }
/** * @param allocator Allocator * @param waitInterval Wait time between allocation attempts (ms) * @param maxWaitInterval Max time to wait for an allocation (ms) */ public RetryingAllocator(Allocator<V> allocator, long waitInterval, long maxWaitInterval) { this.allocator = allocator; this.retryer = RetryerBuilder.<V>newBuilder() .retryIfException() .withWaitStrategy(WaitStrategies.fixedWait(waitInterval, TimeUnit.MILLISECONDS)) .withStopStrategy(StopStrategies.stopAfterDelay(maxWaitInterval, TimeUnit.MILLISECONDS)) .withRetryListener(new RetryListener() { @Override public <V> void onRetry(Attempt<V> attempt) { if (attempt.hasException()) { log.error("Error allocating slot", attempt.getExceptionCause()); } } }) .build(); }