Java Code Examples for io.github.resilience4j.retry.Retry#executeSupplier()

The following examples show how to use io.github.resilience4j.retry.Retry#executeSupplier() . 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: Retryer.java    From charon-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
@Override
public HttpResponse forward(HttpRequest request, HttpRequestExecution execution) {
    logStart(execution.getMappingName());
    Retry retry = getRegistry().retry(execution.getMappingName());
    setupMetrics(registry -> createMetrics(registry, execution.getMappingName()));
    HttpResponse response;
    try {
        response = retry.executeSupplier(() -> {
            getRetryState().nextRetryAttempt();
            HttpResponse httpResponse = execution.execute(request);
            httpResponse.close();
            return httpResponse;
        });
    } finally {
        clearRetryState();
    }
    logEnd(execution.getMappingName());
    return response;
}
 
Example 2
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecuteSupplierWithResult() {
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException())
        .willReturn("Hello world");
    final RetryConfig tryAgain = RetryConfig.<String>custom()
        .retryOnResult(s -> s.contains("Hello world"))
        .maxAttempts(2).build();
    Retry retry = Retry.of("id", tryAgain);

    String result = retry.executeSupplier(helloWorldService::returnHelloWorld);

    then(helloWorldService).should(times(2)).returnHelloWorld();
    assertThat(result).isEqualTo("Hello world");
    assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION);
}
 
Example 3
Source File: AbstractRetryMetricsTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRegisterMetricsWithoutRetry() throws Throwable {
    Retry retry = givenMetricRegistry(metricRegistry);
    given(helloWorldService.returnHelloWorld()).willReturn("Hello world");

    String value = retry.executeSupplier(helloWorldService::returnHelloWorld);

    assertThat(value).isEqualTo("Hello world");
    then(helloWorldService).should(times(1)).returnHelloWorld();
    assertThat(metricRegistry.getMetrics()).hasSize(4);
    assertThat(metricRegistry.getGauges()
        .get("resilience4j.retry.testName." + SUCCESSFUL_CALLS_WITH_RETRY).getValue())
        .isEqualTo(0L);
    assertThat(metricRegistry.getGauges()
        .get("resilience4j.retry.testName." + SUCCESSFUL_CALLS_WITHOUT_RETRY).getValue())
        .isEqualTo(1L);
    assertThat(
        metricRegistry.getGauges().get("resilience4j.retry.testName." + FAILED_CALLS_WITH_RETRY)
            .getValue()).isEqualTo(0L);
    assertThat(metricRegistry.getGauges()
        .get("resilience4j.retry.testName." + FAILED_CALLS_WITHOUT_RETRY).getValue())
        .isEqualTo(0L);
}
 
Example 4
Source File: AbstractRetryMetricsTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldUseCustomPrefix() throws Throwable {
    Retry retry = givenMetricRegistry("testPrefix", metricRegistry);
    given(helloWorldService.returnHelloWorld()).willReturn("Hello world");

    String value = retry.executeSupplier(helloWorldService::returnHelloWorld);

    assertThat(value).isEqualTo("Hello world");
    then(helloWorldService).should(times(1)).returnHelloWorld();
    assertThat(metricRegistry.getMetrics()).hasSize(4);
    assertThat(
        metricRegistry.getGauges().get("testPrefix.testName." + SUCCESSFUL_CALLS_WITH_RETRY)
            .getValue()).isEqualTo(0L);
    assertThat(
        metricRegistry.getGauges().get("testPrefix.testName." + SUCCESSFUL_CALLS_WITHOUT_RETRY)
            .getValue()).isEqualTo(1L);
    assertThat(metricRegistry.getGauges().get("testPrefix.testName." + FAILED_CALLS_WITH_RETRY)
        .getValue()).isEqualTo(0L);
    assertThat(
        metricRegistry.getGauges().get("testPrefix.testName." + FAILED_CALLS_WITHOUT_RETRY)
            .getValue()).isEqualTo(0L);
}
 
Example 5
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecuteSupplier() {
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException())
        .willReturn("Hello world");
    Retry retry = Retry.ofDefaults("id");

    String result = retry.executeSupplier(helloWorldService::returnHelloWorld);

    then(helloWorldService).should(times(2)).returnHelloWorld();
    assertThat(result).isEqualTo("Hello world");
    assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION);
}
 
Example 6
Source File: AbstractRetryMetricsTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRegisterMetricsWithRetry() throws Throwable {
    Retry retry = givenMetricRegistry(metricRegistry);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException())
        .willReturn("Hello world")
        .willThrow(new HelloWorldException())
        .willThrow(new HelloWorldException())
        .willThrow(new HelloWorldException());
    String value1 = retry.executeSupplier(helloWorldService::returnHelloWorld);

    Try.ofSupplier(Retry.decorateSupplier(retry, helloWorldService::returnHelloWorld));

    assertThat(value1).isEqualTo("Hello world");
    then(helloWorldService).should(times(5)).returnHelloWorld();
    assertThat(metricRegistry.getMetrics()).hasSize(4);
    assertThat(metricRegistry.getGauges()
        .get("resilience4j.retry.testName." + SUCCESSFUL_CALLS_WITH_RETRY).getValue())
        .isEqualTo(1L);
    assertThat(metricRegistry.getGauges()
        .get("resilience4j.retry.testName." + SUCCESSFUL_CALLS_WITHOUT_RETRY).getValue())
        .isEqualTo(0L);
    assertThat(
        metricRegistry.getGauges().get("resilience4j.retry.testName." + FAILED_CALLS_WITH_RETRY)
            .getValue()).isEqualTo(1L);
    assertThat(metricRegistry.getGauges()
        .get("resilience4j.retry.testName." + FAILED_CALLS_WITHOUT_RETRY).getValue())
        .isEqualTo(0L);
}