.retryIfException().retryIfRuntimeException() .withWaitStrategy(retryWaitStrategy) .withStopStrategy(retryStopStrategy)
Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder() .retryIfExceptionOfType(RuntimeException.class) .retryIfRuntimeException() .withWaitStrategy(WaitStrategies.fixedWait(configuration.getRetryWaitMs(), TimeUnit.MILLISECONDS)) .withStopStrategy(StopStrategies.stopAfterAttempt(configuration.getRetryCount()))
protected void uploadSingle(int sequence, Path file) throws Exception { Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder() .retryIfExceptionOfType(AmazonS3Exception.class) .retryIfRuntimeException() .withWaitStrategy(WaitStrategies.fixedWait(configuration.getRetryWaitMs(), TimeUnit.MILLISECONDS)) .withStopStrategy(StopStrategies.stopAfterAttempt(configuration.getRetryCount()))
@Bean public Retryer retryer(RatesAppConfigHolder ratesAppConfig) { return RetryerBuilder.newBuilder() .retryIfExceptionOfType(IOException.class) .retryIfRuntimeException() .withWaitStrategy(randomWait(ratesAppConfig.getMinTimeWait(), TimeUnit.MILLISECONDS, ratesAppConfig.getMaxTimeWait(), TimeUnit.MILLISECONDS)) .withStopStrategy(stopAfterAttempt(ratesAppConfig.getMaxAttempts())) .build(); }
.retryIfRuntimeException() .withWaitStrategy(WaitStrategies.exponentialWait(5, 20, TimeUnit.SECONDS)) .withStopStrategy(StopStrategies.stopAfterAttempt(3))
@Bean public Retryer retryer(KycConfigHolder kycConfigHolder) { return RetryerBuilder.newBuilder() .retryIfExceptionOfType(Exception.class) .retryIfRuntimeException() .withWaitStrategy(randomWait(kycConfigHolder.getMinTimeWait(), TimeUnit.MILLISECONDS, kycConfigHolder.getMaxTimeWait(), TimeUnit.MILLISECONDS)) .withStopStrategy(stopAfterAttempt(kycConfigHolder.getMaxAttempts())) .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(); } }
@Override public RemoteConnection createRemoteConnection(String remoteAddress, OSFamily osFamily, LoginCredential loginCredential, int port) { Callable<RemoteConnection> callable = () -> remoteConnectionFactory .createRemoteConnection(remoteAddress, osFamily, loginCredential, port); Retryer<RemoteConnection> remoteConnectionRetryer = RetryerBuilder.<RemoteConnection>newBuilder().retryIfRuntimeException() .retryIfException(throwable -> throwable instanceof RemoteException) .withStopStrategy(StopStrategies.stopAfterAttempt(CONNECTION_RETRIES)) .withWaitStrategy(WaitStrategies .exponentialWait(INCREASE_TIMEOUT_FACTOR, MAXIMUM_TIMEOUT, TimeUnit.SECONDS)) .build(); try { return remoteConnectionRetryer.call(callable); } catch (ExecutionException | RetryException e) { throw new RuntimeException(e); } } }
.retryIfResult(Predicates.<Map<Integer, HostPort>>isNull()).retryIfException().retryIfRuntimeException() .withStopStrategy(StopStrategies.neverStop()) .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS))
@Bean public Retryer retryer(KycConfigHolder kycConfigHolder) { return RetryerBuilder.newBuilder() .retryIfExceptionOfType(Exception.class) .retryIfRuntimeException() .withWaitStrategy(randomWait(kycConfigHolder.getMinTimeWait(), TimeUnit.MILLISECONDS, kycConfigHolder.getMaxTimeWait(), TimeUnit.MILLISECONDS)) .withStopStrategy(stopAfterAttempt(kycConfigHolder.getMaxAttempts())) .build(); }
private Retryer<MessageSendingResult> createRetryer(final MessageBatch batch, int messageBackoff, long messageTtlMillis, boolean retryClientErrors) { return RetryerBuilder.<MessageSendingResult>newBuilder() .retryIfExceptionOfType(IOException.class) .retryIfRuntimeException() .retryIfResult(result -> consuming && !result.succeeded() && shouldRetryOnClientError(retryClientErrors, result)) .withWaitStrategy(fixedWait(messageBackoff, MILLISECONDS)) .withStopStrategy(attempt -> attempt.getDelaySinceFirstAttempt() > messageTtlMillis) .withRetryListener(getRetryListener(result -> { batch.incrementRetryCounter(); monitoring.markSendingResult(batch, subscription, result); })) .build(); }
private Retryer<MessageSendingResult> createRetryer(final MessageBatch batch, int messageBackoff, long messageTtlMillis, boolean retryClientErrors) { return RetryerBuilder.<MessageSendingResult>newBuilder() .retryIfExceptionOfType(IOException.class) .retryIfRuntimeException() .retryIfResult(result -> consuming && !result.succeeded() && shouldRetryOnClientError(retryClientErrors, result)) .withWaitStrategy(fixedWait(messageBackoff, MILLISECONDS)) .withStopStrategy(attempt -> attempt.getDelaySinceFirstAttempt() > messageTtlMillis) .withRetryListener(getRetryListener(result -> { batch.incrementRetryCounter(); monitoring.markSendingResult(batch, subscription, result); })) .build(); }
@Test void testCallThatIsInterrupted() throws Exception { Retryer retryer = RetryerBuilder.newBuilder() .retryIfRuntimeException() .withStopStrategy(StopStrategies.stopAfterAttempt(10)) .build(); Interrupter thrower = new Interrupter(4); boolean interrupted = false; try { retryer.call(thrower); fail("Should have thrown"); } catch(InterruptedException ignored) { interrupted = true; } catch(Exception e) { System.out.println(e); } //noinspection ConstantConditions assertTrue(interrupted); assertEquals(4, thrower.invocations); }
@Test public void testRetryIfRuntimeException() throws Exception { Callable<Boolean> callable = noIOExceptionAfter5Attempts(); Retryer retryer = RetryerBuilder.newBuilder() .retryIfRuntimeException() .build(); try { retryer.call(callable); fail("IOException expected"); } catch (RetryException ignored) { } callable = noIllegalStateExceptionAfter5Attempts(); assertTrue(retryer.call(callable)); callable = noIllegalStateExceptionAfter5Attempts(); retryer = RetryerBuilder.newBuilder() .retryIfRuntimeException() .withStopStrategy(StopStrategies.stopAfterAttempt(3)) .build(); try { retryer.call(callable); fail("Exception expected"); } catch (RetryException ignored) { } }
@Test public void testMultipleRetryConditions() throws Exception { Callable<Boolean> callable = notNullResultOrIOExceptionOrRuntimeExceptionAfter5Attempts(); Retryer retryer = RetryerBuilder.newBuilder() .retryIfResult(Objects::isNull) .retryIfExceptionOfType(IOException.class) .retryIfRuntimeException() .withStopStrategy(StopStrategies.stopAfterAttempt(3)) .build(); try { retryer.call(callable); fail("Exception expected"); } catch (RetryException ignored) { } callable = notNullResultOrIOExceptionOrRuntimeExceptionAfter5Attempts(); retryer = RetryerBuilder.newBuilder() .retryIfResult(Objects::isNull) .retryIfExceptionOfType(IOException.class) .retryIfRuntimeException() .build(); assertTrue(retryer.call(callable)); }
@Test void testRunThatIsInterrupted() throws Exception { Retryer retryer = RetryerBuilder.newBuilder() .retryIfRuntimeException() .withStopStrategy(StopStrategies.stopAfterAttempt(10)) .build(); Interrupter thrower = new Interrupter(4); boolean interrupted = false; try { retryer.run(thrower); fail("Should have thrown"); } catch(InterruptedException ignored) { interrupted = true; } //noinspection ConstantConditions assertTrue(interrupted); assertEquals(4, thrower.invocations); }