/** * 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); }
@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); } }
@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(); } }
@Test public void testNeverStop() { assertFalse(StopStrategies.neverStop().shouldStop(failedAttempt(3, 6546L))); }
@Bean public Retryer<PaymentLog> retryer() { return RetryerBuilder.<PaymentLog>newBuilder() .retryIfExceptionOfType(OptimisticLockingFailureException.class) .retryIfExceptionOfType(OptimisticLockException.class) .withWaitStrategy(randomWait(1000L, TimeUnit.MILLISECONDS)) .withStopStrategy(StopStrategies.neverStop()) .build(); }
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();