@Test(dataProvider = "retriesTestProvider") public void testRetries(Exception exceptionToThrow, final int exceptionCount, final int maxRetries, final int expectedAttemptCount) { setupTest(exceptionToThrow, exceptionCount, maxRetries, null, null); try { retryableExecutor.execute(); if(exceptionCount > maxRetries) { Assert.fail("Expected " + exceptionToThrow.getClass() + " exception."); } } catch(Exception ex) { Assert.assertEquals(ex, exceptionToThrow); } Assert.assertEquals(testExecutor.getAttemptCount(), expectedAttemptCount); }
@Override public T execute() { logger.debug("Initializing retry"); RetryPolicy<Object> policy = new RetryPolicy<>(); if (retryableExceptions != null && retryableExceptions.size() > 0) { policy.handle(retryableExceptions); } if (nonRetryableExceptions != null && nonRetryableExceptions.size() > 0) { policy.abortOn(nonRetryableExceptions); } policy = policy.withMaxRetries(maxRetries); return Failsafe .with(policy) .onFailure(executionCompletedEvent -> { currentRetry++; logger.error("Retrying Operation {} :", currentRetry, executionCompletedEvent.getFailure()); }) .get((() -> { logger.debug("Running Operation - Attempt: {}", currentRetry); return executor.execute(); })); }
RetryableExecutor implements Executor<T> { @Override public T execute() { logger.debug("Initializing retry"); RetryPolicy<Object> policy = new RetryPolicy<>(); if (retryableExceptions != null && retryableExceptions.size() > 0) { policy.handle(retryableExceptions); } if (nonRetryableExceptions != null && nonRetryableExceptions.size() > 0) { policy.abortOn(nonRetryableExceptions); } policy = policy.withMaxRetries(maxRetries); return Failsafe .with(policy) .onFailure(executionCompletedEvent -> { currentRetry++; logger.error("Retrying Operation {} :", currentRetry, executionCompletedEvent.getFailure()); }) .get((() -> { logger.debug("Running Operation - Attempt: {}", currentRetry); return executor.execute(); })); } }
RetryableExecutor implements Executor<T> { @Override public T execute() { logger.debug("Initializing retry"); RetryPolicy<Object> policy = new RetryPolicy<>(); if (retryableExceptions != null && retryableExceptions.size() > 0) { policy.handle(retryableExceptions); } if (nonRetryableExceptions != null && nonRetryableExceptions.size() > 0) { policy.abortOn(nonRetryableExceptions); } policy = policy.withMaxRetries(maxRetries); return Failsafe .with(policy) .onFailure(executionCompletedEvent -> { currentRetry++; logger.error("Retrying Operation {} :", currentRetry, executionCompletedEvent.getFailure()); }) .get((() -> { logger.debug("Running Operation - Attempt: {}", currentRetry); return executor.execute(); })); } RetryableExecutor(
        Executor<T> executor,
        int maxRetries);  RetryableExecutor(
        Executor<T> executor,
        int maxRetries,
        List<Class<? extends Throwable>> retryableExceptions);  RetryableExecutor(
        Executor<T> operationPerformer,
        int maxRetries,
        List<Class<? extends Throwable>> retryableExceptions,
        List<Class<? extends Throwable>> nonRetryableExceptions); }
RetryableExecutor implements Executor<T> { @Override public T execute() { logger.debug("Initializing retry"); RetryPolicy<Object> policy = new RetryPolicy<>(); if (retryableExceptions != null && retryableExceptions.size() > 0) { policy.handle(retryableExceptions); } if (nonRetryableExceptions != null && nonRetryableExceptions.size() > 0) { policy.abortOn(nonRetryableExceptions); } policy = policy.withMaxRetries(maxRetries); return Failsafe .with(policy) .onFailure(executionCompletedEvent -> { currentRetry++; logger.error("Retrying Operation {} :", currentRetry, executionCompletedEvent.getFailure()); }) .get((() -> { logger.debug("Running Operation - Attempt: {}", currentRetry); return executor.execute(); })); } RetryableExecutor(
        Executor<T> executor,
        int maxRetries);  RetryableExecutor(
        Executor<T> executor,
        int maxRetries,
        List<Class<? extends Throwable>> retryableExceptions);  RetryableExecutor(
        Executor<T> operationPerformer,
        int maxRetries,
        List<Class<? extends Throwable>> retryableExceptions,
        List<Class<? extends Throwable>> nonRetryableExceptions); @Override T execute(); }
RetryableExecutor implements Executor<T> { @Override public T execute() { logger.debug("Initializing retry"); RetryPolicy<Object> policy = new RetryPolicy<>(); if (retryableExceptions != null && retryableExceptions.size() > 0) { policy.handle(retryableExceptions); } if (nonRetryableExceptions != null && nonRetryableExceptions.size() > 0) { policy.abortOn(nonRetryableExceptions); } policy = policy.withMaxRetries(maxRetries); return Failsafe .with(policy) .onFailure(executionCompletedEvent -> { currentRetry++; logger.error("Retrying Operation {} :", currentRetry, executionCompletedEvent.getFailure()); }) .get((() -> { logger.debug("Running Operation - Attempt: {}", currentRetry); return executor.execute(); })); } RetryableExecutor(
        Executor<T> executor,
        int maxRetries);  RetryableExecutor(
        Executor<T> executor,
        int maxRetries,
        List<Class<? extends Throwable>> retryableExceptions);  RetryableExecutor(
        Executor<T> operationPerformer,
        int maxRetries,
        List<Class<? extends Throwable>> retryableExceptions,
        List<Class<? extends Throwable>> nonRetryableExceptions); @Override T execute();  }