com.evanlennick.retry4j.exception.RetriesExhaustedException Java Examples

The following examples show how to use com.evanlennick.retry4j.exception.RetriesExhaustedException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CallExecutorTest.java    From retry4j with MIT License 6 votes vote down vote up
@Test
public void verifyRetryPolicyTimeoutIsUsed() {
    Callable<Object> callable = () -> {
        throw new RuntimeException();
    };

    Duration delayBetweenTriesDuration = Duration.ofSeconds(17);
    when(mockBackOffStrategy.getDurationToWait(1, delayBetweenTriesDuration)).thenReturn(Duration.ofSeconds(5));

    RetryConfig retryConfig = retryConfigBuilder
            .withMaxNumberOfTries(2)
            .retryOnAnyException()
            .withDelayBetweenTries(delayBetweenTriesDuration)
            .withBackoffStrategy(mockBackOffStrategy)
            .build();

    final long before = System.currentTimeMillis();
    try {
        CallExecutor executor = new CallExecutorBuilder().config(retryConfig).build();
        executor.execute(callable);
    } catch (RetriesExhaustedException ignored) {
    }

    assertThat(System.currentTimeMillis() - before).isGreaterThan(5000);
    verify(mockBackOffStrategy).getDurationToWait(1, delayBetweenTriesDuration);
}
 
Example #2
Source File: TcpTransportTest.java    From bender with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldExhaustRetries() throws TransportException, IOException {

  Sink sink = mock(Sink.class);
  TcpTransport transport = new TcpTransport(sink, 4, 0);

  TcpTransportBuffer transportBuffer = mock(TcpTransportBuffer.class);
  Buffer buffer = new Buffer();
  when(transportBuffer.getInternalBuffer()).thenReturn(buffer);

  doThrow(new IOException()).when(sink).write(eq(buffer), eq(0L));

  try {
    transport.sendBatch(transportBuffer);
    fail("Should exhaust retries");
  } catch (TransportException ex) {
    assertTrue(ex.getCause() instanceof RetriesExhaustedException);
  }

  verify(sink, times(5)).write(eq(buffer), eq(0L));

}
 
Example #3
Source File: CallExecutorTest.java    From retry4j with MIT License 6 votes vote down vote up
@Test
public void verifyRetryingIndefinitely() throws Exception {
    Callable<Boolean> callable = () -> {
        Random random = new Random();
        if (random.nextInt(10000) == 0) {
            return true;
        }
        throw new IllegalArgumentException();
    };

    RetryConfig retryConfig = retryConfigBuilder
            .retryIndefinitely()
            .retryOnAnyException()
            .withFixedBackoff()
            .withDelayBetweenTries(0, ChronoUnit.SECONDS)
            .build();

    try {
        new CallExecutorBuilder().config(retryConfig).build()
                .execute(callable);
    } catch (RetriesExhaustedException e) {
        fail("Retries should never be exhausted!");
    }
}
 
Example #4
Source File: CallExecutor.java    From retry4j with MIT License 6 votes vote down vote up
private void postExecutionCleanup(Callable<T> callable, int maxTries, AttemptStatus<T> attemptStatus) {
    if (!attemptStatus.wasSuccessful()) {
        String failureMsg = String.format("Call '%s' failed after %d tries!", callable.toString(), maxTries);
        if (null != onFailureListener) {
            onFailureListener.onEvent(status);
        } else {
            logger.trace("Throwing retries exhausted exception");
            throw new RetriesExhaustedException(failureMsg, lastKnownExceptionThatCausedRetry, status);
        }
    } else {
        status.setResult(attemptStatus.getResult());
        if (null != onSuccessListener) {
            onSuccessListener.onEvent(status);
        }
    }
}
 
Example #5
Source File: CallExecutorTest.java    From retry4j with MIT License 6 votes vote down vote up
@Test
public void verifyNullCallResultCountsAsValidResult() throws Exception {
    Callable<String> callable = () -> null;

    RetryConfig retryConfig = retryConfigBuilder
            .withMaxNumberOfTries(1)
            .withDelayBetweenTries(0, ChronoUnit.SECONDS)
            .build();

    try {
        new CallExecutorBuilder().config(retryConfig).build()
                .execute(callable);
    } catch (RetriesExhaustedException e) {
        Status status = e.getStatus();
        assertThat(status.getResult()).isNull();
        assertThat(status.wasSuccessful()).isTrue();
    }
}
 
Example #6
Source File: CallExecutorTest_RetryOnCustomLogicTest.java    From retry4j with MIT License 6 votes vote down vote up
@Test
public void verifyShouldRetryOnExceptionMessage() {
    RetryConfig config = new RetryConfigBuilder()
            .retryOnCustomExceptionLogic(ex -> ex.getMessage().contains("should retry!"))
            .withFixedBackoff()
            .withDelayBetweenTries(Duration.ofMillis(1))
            .withMaxNumberOfTries(3)
            .build();

    try {
        new CallExecutorBuilder().config(config).build()
                .execute(() -> {
                    throw new RuntimeException("should retry!");
                });
        fail();
    } catch (RetriesExhaustedException e) {
        assertThat(e.getStatus().getTotalTries()).isEqualTo(3);
    }
}
 
Example #7
Source File: CallExecutorTest_RetryOnCustomLogicTest.java    From retry4j with MIT License 6 votes vote down vote up
@Test
public void verifyShouldRetryOnCustomException() {
    RetryConfig config = new RetryConfigBuilder()
            .retryOnCustomExceptionLogic(ex -> ((CustomTestException) ex).getSomeValue() > 0)
            .withFixedBackoff()
            .withDelayBetweenTries(Duration.ofMillis(1))
            .withMaxNumberOfTries(3)
            .build();

    try {
        new CallExecutorBuilder().config(config).build()
                .execute(() -> {
                    throw new CustomTestException("should retry!", 100);
                });
        fail();
    } catch (RetriesExhaustedException e) {
        assertThat(e.getStatus().getTotalTries()).isEqualTo(3);
    }
}
 
Example #8
Source File: CallExecutorTest.java    From retry4j with MIT License 6 votes vote down vote up
@Test(expectedExceptions = {RetriesExhaustedException.class})
public void shouldMatchExceptionCauseAtGreaterThanALevelDeepAndRetry() throws Exception {
    class CustomException extends Exception {

        CustomException(Throwable cause) {
            super(cause);
        }
    }
    Callable<Boolean> callable = () -> {
        throw new Exception(new CustomException(new RuntimeException(new IOException())));
    };

    RetryConfig retryConfig = retryConfigBuilder
            .retryOnCausedBy()
            .retryOnSpecificExceptions(IOException.class)
            .withMaxNumberOfTries(1)
            .withDelayBetweenTries(0, ChronoUnit.SECONDS)
            .withFixedBackoff()
            .build();

    new CallExecutorBuilder().config(retryConfig).build().execute(callable);
}
 
Example #9
Source File: TcpTransport.java    From bender with Apache License 2.0 6 votes vote down vote up
@Override
public void sendBatch(TransportBuffer buffer) throws TransportException {

  Buffer internalBuffer = ((TcpTransportBuffer) buffer).getInternalBuffer();

  Callable<Boolean> write = () -> {
    try {
      sink.write(internalBuffer, internalBuffer.size());
      return true;
    } catch (IOException ex) {
      throw new TransportException("Error while sending in tcp transport", ex);
    }
  };

  try {
    new CallExecutor(retryConfig).execute(write);
  } catch (RetriesExhaustedException | UnexpectedException ue) {
    throw new TransportException(ue);
  }

}
 
Example #10
Source File: CallExecutorTest.java    From retry4j with MIT License 6 votes vote down vote up
@Test(expectedExceptions = {RetriesExhaustedException.class})
public void shouldMatchExceptionCauseAndRetry() throws Exception {
    Callable<Boolean> callable = () -> {
        throw new Exception(new CustomTestException("message", 3));
    };

    RetryConfig retryConfig = retryConfigBuilder
            .retryOnCausedBy()
            .retryOnSpecificExceptions(CustomTestException.class)
            .withMaxNumberOfTries(1)
            .withDelayBetweenTries(0, ChronoUnit.SECONDS)
            .withFixedBackoff()
            .build();

    new CallExecutorBuilder().config(retryConfig).build().execute(callable);
}
 
Example #11
Source File: CallExecutorTest.java    From retry4j with MIT License 6 votes vote down vote up
@Test(expectedExceptions = {RetriesExhaustedException.class})
public void verifyExceptionFromCallThrowsCallFailureException() throws Exception {
    Callable<Boolean> callable = () -> {
        throw new RuntimeException();
    };

    RetryConfig retryConfig = retryConfigBuilder
            .retryOnAnyException()
            .withMaxNumberOfTries(1)
            .withDelayBetweenTries(0, ChronoUnit.SECONDS)
            .withFixedBackoff()
            .build();

    new CallExecutorBuilder().config(retryConfig).build()
            .execute(callable);
}
 
Example #12
Source File: CallExecutorTest_RetryOnValueTest.java    From retry4j with MIT License 5 votes vote down vote up
private void assertRetryOccurs(RetryConfig config, Callable<?> callable, int expectedNumberOfTries) {
    try {
        new CallExecutorBuilder().config(config).build().execute(callable);
        fail("Expected RetriesExhaustedException but one wasn't thrown!");
    } catch (RetriesExhaustedException e) {
        assertThat(e.getStatus().wasSuccessful()).isFalse();
        assertThat(e.getStatus().getTotalTries()).isEqualTo(expectedNumberOfTries);
    }
}
 
Example #13
Source File: CallExecutorTest.java    From retry4j with MIT License 5 votes vote down vote up
@Test
public void verifyStatusIsPopulatedOnFailedCall() throws Exception {
    Callable<Boolean> callable = () -> {
        throw new FileNotFoundException();
    };

    RetryConfig retryConfig = retryConfigBuilder
            .withMaxNumberOfTries(5)
            .retryOnAnyException()
            .withDelayBetweenTries(0, ChronoUnit.SECONDS)
            .withFixedBackoff()
            .build();

    try {
        CallExecutor<Boolean> executor = new CallExecutorBuilder().config(retryConfig).build();
        executor.execute(callable, "TestCall");
        fail("RetriesExhaustedException wasn't thrown!");
    } catch (RetriesExhaustedException e) {
        Status status = e.getStatus();
        assertThat(status.getResult()).isNull();
        assertThat(status.wasSuccessful()).isFalse();
        assertThat(status.getCallName()).isEqualTo("TestCall");
        assertThat(status.getTotalElapsedDuration().toMillis()).isCloseTo(0, within(25L));
        assertThat(status.getTotalTries()).isEqualTo(5);
        assertThat(e.getCause()).isExactlyInstanceOf(FileNotFoundException.class);
    }
}
 
Example #14
Source File: CallExecutorTest.java    From retry4j with MIT License 5 votes vote down vote up
@Test(expectedExceptions = {RetriesExhaustedException.class})
public void verifyExactSameSpecificExceptionThrowsCallFailureException() throws Exception {
    Callable<Boolean> callable = () -> {
        throw new IllegalArgumentException();
    };

    RetryConfig retryConfig = retryConfigBuilder
            .retryOnSpecificExceptions(IllegalArgumentException.class)
            .withMaxNumberOfTries(1)
            .withDelayBetweenTries(0, ChronoUnit.SECONDS)
            .withFixedBackoff()
            .build();

    new CallExecutorBuilder().config(retryConfig).build().execute(callable);
}
 
Example #15
Source File: CallExecutorTest.java    From retry4j with MIT License 5 votes vote down vote up
@Test(expectedExceptions = {RetriesExhaustedException.class})
public void verifySpecificSubclassExceptionRetries() throws Exception {
    Callable<Boolean> callable = () -> {
        throw new IOException();
    };

    RetryConfig retryConfig = retryConfigBuilder
            .retryOnSpecificExceptions(Exception.class)
            .withMaxNumberOfTries(1)
            .withDelayBetweenTries(0, ChronoUnit.SECONDS)
            .withFixedBackoff()
            .build();

    new CallExecutorBuilder().config(retryConfig).build().execute(callable);
}
 
Example #16
Source File: AsyncCallExecutorTest.java    From retry4j with MIT License 5 votes vote down vote up
@Test
public void verifyOneCall_failDueToTooManyRetries_withExecutorService() throws Exception {
    Callable<Boolean> callable = () -> { throw new RuntimeException(); };

    AsyncCallExecutor<Boolean> executor = new CallExecutorBuilder().config(retryOnAnyExceptionConfig)
            .buildAsync(executorService);

    CompletableFuture<Status<Boolean>> future = executor.execute(callable);

    assertThatThrownBy(future::get)
            .isExactlyInstanceOf(ExecutionException.class)
            .hasCauseExactlyInstanceOf(RetriesExhaustedException.class);
}
 
Example #17
Source File: AsyncCallExecutorTest.java    From retry4j with MIT License 5 votes vote down vote up
@Test
public void verifyOneCall_failDueToTooManyRetries_noExecutorService() throws Exception {
    Callable<Boolean> callable = () -> { throw new RuntimeException(); };

    AsyncCallExecutor<Boolean> executor = new CallExecutorBuilder().config(retryOnAnyExceptionConfig).buildAsync();

    CompletableFuture<Status<Boolean>> future = executor.execute(callable);

    assertThatThrownBy(future::get)
            .isExactlyInstanceOf(ExecutionException.class)
            .hasCauseExactlyInstanceOf(RetriesExhaustedException.class);
}
 
Example #18
Source File: CallExecutorTest_RetryOnAnyExcludingTest.java    From retry4j with MIT License 5 votes vote down vote up
@Test(expectedExceptions = {RetriesExhaustedException.class})
public void verifyMultipleExceptions_retryOnNotExcludedException() {
    Callable<Boolean> callable = () -> {
        throw new NullPointerException();
    };

    RetryConfig retryConfig = retryConfigBuilder
            .retryOnAnyExceptionExcluding(IllegalArgumentException.class, UnsupportedOperationException.class)
            .withMaxNumberOfTries(5)
            .withNoWaitBackoff()
            .build();

    new CallExecutorBuilder().config(retryConfig).build().execute(callable);
}
 
Example #19
Source File: CallExecutorTest_RetryOnAnyExcludingTest.java    From retry4j with MIT License 5 votes vote down vote up
@Test(expectedExceptions = RetriesExhaustedException.class)
public void verifySuperclassOfExcludedExceptionDoesntThrowUnexpectedException() {
    Callable<Boolean> callable = () -> {
        throw new IOException();
    };

    RetryConfig retryConfig = retryConfigBuilder
            .retryOnAnyExceptionExcluding(FileNotFoundException.class)
            .withMaxNumberOfTries(1)
            .withDelayBetweenTries(0, ChronoUnit.SECONDS)
            .withFixedBackoff()
            .build();

    new CallExecutorBuilder().config(retryConfig).build().execute(callable);
}
 
Example #20
Source File: CallExecutorTest_RetryOnAnyExcludingTest.java    From retry4j with MIT License 5 votes vote down vote up
@Test(expectedExceptions = {RetriesExhaustedException.class})
public void verifyRetryOnAnyExcludingCallSucceeds() {
    Callable<Boolean> callable = () -> {
        throw new IllegalArgumentException();
    };

    RetryConfig retryConfig = retryConfigBuilder
            .retryOnAnyExceptionExcluding(UnsupportedOperationException.class)
            .withMaxNumberOfTries(1)
            .withDelayBetweenTries(0, ChronoUnit.SECONDS)
            .withFixedBackoff()
            .build();

    new CallExecutorBuilder().config(retryConfig).build().execute(callable);
}
 
Example #21
Source File: S3EventIterator.java    From bender with Apache License 2.0 5 votes vote down vote up
@Override
public InternalEvent next() {
  updateCursor();

  /*
   * Wrap reading next row in retry logic. This is because there is intermittent socket timeouts
   * when reading from S3 that cause the function to hang/fail.
   */
  Callable<String> callable = () -> {
    return this.lineIterator.next();
  };

  String nextRow;
  try {
    CallResults<Object> results = new CallExecutor(this.config).execute(callable);
    nextRow = (String) results.getResult();
  } catch (RetriesExhaustedException ree) {
    throw new RuntimeException(ree.getCallResults().getLastExceptionThatCausedRetry());
  } catch (UnexpectedException ue) {
    throw ue;
  }

  /*
   * Construct the internal event
   */
  return new S3InternalEvent(nextRow, this.context, this.arrivalTime,
      currentS3Entity.getObject().getKey(), currentS3Entity.getBucket().getName(),
      currentS3Entity.getObject().getVersionId());
}
 
Example #22
Source File: HttpTransport.java    From bender with Apache License 2.0 4 votes vote down vote up
public void sendBatch(byte[] raw) throws TransportException {
  /*
   * Wrap the call with retry logic to avoid intermittent ES issues.
   */
  Callable<HttpResponse> callable = () -> {
    HttpResponse resp;
    String responseString = null;
    HttpPost httpPost = new HttpPost(this.url);

    /*
     * Do the call, read response, release connection so it is available for use again, and
     * finally check the response.
     */
    try {
      if (this.useGzip) {
        resp = sendBatchCompressed(httpPost, raw);
      } else {
        resp = sendBatchUncompressed(httpPost, raw);
      }

      try {
        responseString = EntityUtils.toString(resp.getEntity());
      } catch (ParseException | IOException e) {
        throw new TransportException(
            "http transport call failed because " + resp.getStatusLine().getReasonPhrase());
      }
    } finally {
      /*
       * Always release connection otherwise it blocks future requests.
       */
      httpPost.releaseConnection();
    }

    checkResponse(resp, responseString);
    return resp;
  };

  RetryConfig config = new RetryConfigBuilder()
      .retryOnSpecificExceptions(TransportException.class).withMaxNumberOfTries(this.retries + 1)
      .withDelayBetweenTries(this.retryDelayMs, ChronoUnit.MILLIS).withExponentialBackoff()
      .build();

  try {
    new CallExecutor(config).execute(callable);
  } catch (RetriesExhaustedException ree) {
    logger.warn("transport failed after " + ree.getCallResults().getTotalTries() + " tries.");
    throw new TransportException(ree.getCallResults().getLastExceptionThatCausedRetry());
  } catch (UnexpectedException ue) {
    throw new TransportException(ue);
  }
}
 
Example #23
Source File: RetryExecutor.java    From retry4j with MIT License votes vote down vote up
S execute(Callable<T> callable, String callName) throws RetriesExhaustedException, UnexpectedException; 
Example #24
Source File: RetryExecutor.java    From retry4j with MIT License votes vote down vote up
S execute(Callable<T> callable) throws RetriesExhaustedException, UnexpectedException;