WaitStrategies.randomWait(100, TimeUnit.MILLISECONDS, 500, TimeUnit.MILLISECONDS) )) .withStopStrategy(StopStrategies.stopAfterAttempt(retryCount)) .withBlockStrategy(BlockStrategies.threadSleepStrategy()) .withRetryListener(new RetryListener() {
.retryIfResult(Predicates.not(Predicates.equalTo(Boolean.TRUE))) .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS)) .withStopStrategy(StopStrategies.stopAfterDelay(maxWait, timeUnit)) .build();
/** * Builds the retryer. * * @return the built retryer. */ public Retryer<V> build() { AttemptTimeLimiter<V> theAttemptTimeLimiter = attemptTimeLimiter == null ? AttemptTimeLimiters.<V>noTimeLimit() : attemptTimeLimiter; StopStrategy theStopStrategy = stopStrategy == null ? StopStrategies.neverStop() : stopStrategy; WaitStrategy theWaitStrategy = waitStrategy == null ? WaitStrategies.noWait() : waitStrategy; BlockStrategy theBlockStrategy = blockStrategy == null ? BlockStrategies.threadSleepStrategy() : blockStrategy; return new Retryer<V>(theAttemptTimeLimiter, theStopStrategy, theWaitStrategy, theBlockStrategy, rejectionPredicate, listeners); }
/** * Builds the retryer. * * @return the built retryer. */ public Retryer build() { AttemptTimeLimiter theAttemptTimeLimiter = attemptTimeLimiter == null ? AttemptTimeLimiters.noTimeLimit() : attemptTimeLimiter; StopStrategy theStopStrategy = stopStrategy == null ? StopStrategies.neverStop() : stopStrategy; WaitStrategy theWaitStrategy = waitStrategy == null ? WaitStrategies.noWait() : waitStrategy; BlockStrategy theBlockStrategy = blockStrategy == null ? BlockStrategies.threadSleepStrategy() : blockStrategy; return new Retryer( theAttemptTimeLimiter, theStopStrategy, theWaitStrategy, theBlockStrategy, retryPredicates, listeners); }
.retryIfExceptionOfType(DuplicateKeyException.class) .withWaitStrategy(WaitStrategies.randomWait(5, TimeUnit.MILLISECONDS)) .withStopStrategy(StopStrategies.stopAfterAttempt(10)) .build(); try {
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(); }
@Bean public Retryer<PaymentLog> retryer(MonitorAppConfigHolder configHolder) { return RetryerBuilder.<PaymentLog>newBuilder() .retryIfExceptionOfType(OptimisticLockingFailureException.class) .retryIfExceptionOfType(OptimisticLockException.class) .withWaitStrategy(randomWait( configHolder.getTokenConversionMaxTimeWait(), TimeUnit.MILLISECONDS)) .withStopStrategy(StopStrategies.neverStop()) .build(); } }
public BaseJdbcBufferedInserter(State state, Connection conn) { this.conn = conn; this.batchSize = state.getPropAsInt(WRITER_JDBC_INSERT_BATCH_SIZE, DEFAULT_WRITER_JDBC_INSERT_BATCH_SIZE); if (this.batchSize < 1) { throw new IllegalArgumentException(WRITER_JDBC_INSERT_BATCH_SIZE + " should be a positive number"); } int maxWait = state.getPropAsInt(WRITER_JDBC_INSERT_RETRY_TIMEOUT, DEFAULT_WRITER_JDBC_INSERT_RETRY_TIMEOUT); int maxAttempts = state.getPropAsInt(WRITER_JDBC_INSERT_RETRY_MAX_ATTEMPT, DEFAULT_WRITER_JDBC_INSERT_RETRY_MAX_ATTEMPT); //retry after 2, 4, 8, 16... sec, allow at most maxWait sec delay this.retryer = RetryerBuilder.<Boolean> newBuilder().retryIfException() .withWaitStrategy(WaitStrategies.exponentialWait(1000, maxWait, TimeUnit.SECONDS)) .withStopStrategy(StopStrategies.stopAfterAttempt(maxAttempts)).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(); } }
@Override public void createQueue(String queueName) { try { Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder() .retryIfException() .withWaitStrategy(WaitStrategies.fixedWait(61, TimeUnit.SECONDS)) .withStopStrategy(StopStrategies.neverStop()) .withRetryListener(new RetryListener() { @Override public <V> void onRetry(Attempt<V> attempt) { if (attempt.hasException()) { LOG.info("Retrying to create queue in 61 seconds. {}", attempt.getExceptionCause().getMessage()); } } }) .build(); CreateQueueRequest createQueueRequest = getCreateQueueRequest(queueName); LOG.info("Creating queue: {}", queueName); retryer.call(getCreateQueueTask(createQueueRequest)); LOG.info("Created queue: {}", queueName); } catch (RetryException | ExecutionException e) { LOG.error("Received error while trying to create queue", e); throw new RuntimeException(e); } }
/** * Retry to execute callable command. * * @param callable callable command * @param <T> return type * @return execute result */ public <T> Optional<T> execute(final Callable<T> callable) { Retryer<T> retryer = RetryerBuilder.<T>newBuilder() .retryIfExceptionOfType(TimeoutException.class) .retryIfExceptionOfType(ExecutionException.class) .retryIfExceptionOfType(InterruptedException.class) .withWaitStrategy(WaitStrategies.fixedWait(config.getRetryIntervalMilliseconds(), TimeUnit.MILLISECONDS)) .withStopStrategy(StopStrategies.stopAfterAttempt(config.getMaxRetries())) .build(); try { return Optional.fromNullable(retryer.call(callable)); } catch (final ExecutionException | RetryException ex) { throw new RegistryCenterException(ex); } } }
/** * @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(); }
@Test public void testNeverStop() { assertFalse(StopStrategies.neverStop().shouldStop(failedAttempt(3, 6546L))); }
/** * @return RetryerBuilder that retries on all exceptions except NonTransientException with exponential back off */ public static RetryerBuilder<Void> createRetryBuilder(State state) { Predicate<Throwable> transients = new Predicate<Throwable>() { @Override public boolean apply(Throwable t) { return !(t instanceof NonTransientException); } }; long multiplier = state.getPropAsLong(RETRY_MULTIPLIER, 500L); long maxWaitMsPerInterval = state.getPropAsLong(RETRY_MAX_WAIT_MS_PER_INTERVAL, 10000); int maxAttempts = state.getPropAsInt(RETRY_MAX_ATTEMPTS, 5); return RetryerBuilder.<Void> newBuilder() .retryIfException(transients) .withWaitStrategy(WaitStrategies.exponentialWait(multiplier, maxWaitMsPerInterval, TimeUnit.MILLISECONDS)) //1, 2, 4, 8, 16 seconds delay .withStopStrategy(StopStrategies.stopAfterAttempt(maxAttempts)); //Total 5 attempts and fail. }
/** * 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); }
@Bean public Retryer<PaymentLog> retryer() { return RetryerBuilder.<PaymentLog>newBuilder() .retryIfExceptionOfType(OptimisticLockingFailureException.class) .retryIfExceptionOfType(OptimisticLockException.class) .withWaitStrategy(randomWait(1000L, TimeUnit.MILLISECONDS)) .withStopStrategy(StopStrategies.neverStop()) .build(); }
private <T> T callWithRetriesAndTimeout(Callable<T> callable, Optional<Integer> retryCount) throws Exception { RetryerBuilder<T> retryerBuilder = RetryerBuilder.<T>newBuilder() .withAttemptTimeLimiter(AttemptTimeLimiters.<T>fixedTimeLimit(configuration.getDockerClientTimeLimitSeconds(), TimeUnit.SECONDS, executor)); if (retryCount.isPresent()) { retryerBuilder.withStopStrategy(StopStrategies.stopAfterAttempt(retryCount.get())); } return retryerBuilder.build().call(callable); } }
/** * 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); }
Retryer<Map<Integer, HostPort>> simpleConsumerRetryer = RetryerBuilder.<Map<Integer, HostPort>>newBuilder() .retryIfResult(Predicates.<Map<Integer, HostPort>>isNull()).retryIfException().retryIfRuntimeException() .withStopStrategy(StopStrategies.neverStop()) .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS)) .withBlockStrategy(BlockStrategies.threadSleepStrategy()).build();
public SingularityClient(String contextPath, HttpClient httpClient, Provider<List<String>> hostsProvider, Optional<SingularityClientCredentials> credentials, boolean ssl, int retryAttempts, Predicate<HttpResponse> retryStrategy) { this.httpClient = httpClient; this.contextPath = contextPath; this.hostsProvider = hostsProvider; this.random = new Random(); this.credentials = credentials; this.ssl = ssl; this.httpResponseRetryer = RetryerBuilder.<HttpResponse>newBuilder() .withStopStrategy(StopStrategies.stopAfterAttempt(retryAttempts)) .withWaitStrategy(WaitStrategies.exponentialWait()) .retryIfResult(retryStrategy::test) .retryIfException() .build(); }