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

The following examples show how to use io.github.resilience4j.retry.Retry#decorateSupplier() . 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: SupplierRetryTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecorateSupplierAndInvokeTwice() {
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException())
        .willReturn("Hello world")
        .willThrow(new HelloWorldException())
        .willReturn("Hello world");
    Retry retry = Retry.ofDefaults("id");
    Supplier<String> supplier = Retry
        .decorateSupplier(retry, helloWorldService::returnHelloWorld);

    String result = supplier.get();
    String result2 = supplier.get();

    then(helloWorldService).should(times(4)).returnHelloWorld();
    assertThat(result).isEqualTo("Hello world");
    assertThat(result2).isEqualTo("Hello world");
    assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2);
    assertThat(retry.getMetrics().getNumberOfSuccessfulCallsWithRetryAttempt()).isEqualTo(2);
}
 
Example 2
Source File: RetryMetricsCollectorTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() {
    registry = new CollectorRegistry();
    retryRegistry = RetryRegistry.ofDefaults();
    retry = retryRegistry.retry("backendA");

    RetryMetricsCollector.ofRetryRegistry(retryRegistry).register(registry);

    retry.executeSupplier(() -> "return");

    Supplier<String> supplier = Retry.decorateSupplier(retry, () -> {
        throw new RuntimeException();
    });

    Try.ofSupplier(supplier);
}
 
Example 3
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotRetry() {
    given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
    Retry retry = Retry.ofDefaults("id");
    Supplier<String> supplier = Retry
        .decorateSupplier(retry, helloWorldService::returnHelloWorld);

    String result = supplier.get();

    then(helloWorldService).should().returnHelloWorld();
    assertThat(result).isEqualTo("Hello world");
    assertThat(sleptTime).isEqualTo(0);
}
 
Example 4
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotRetryWithResult() {
    given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
    final RetryConfig tryAgain = RetryConfig.<String>custom()
        .retryOnResult(s -> s.contains("tryAgain"))
        .maxAttempts(2).build();
    Retry retry = Retry.of("id", tryAgain);
    Supplier<String> supplier = Retry
        .decorateSupplier(retry, helloWorldService::returnHelloWorld);

    String result = supplier.get();
    then(helloWorldService).should().returnHelloWorld();
    assertThat(result).isEqualTo("Hello world");
    assertThat(sleptTime).isEqualTo(0);
}
 
Example 5
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRetryWithResult() {
    given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
    final RetryConfig tryAgain = RetryConfig.<String>custom()
        .retryOnResult(s -> s.contains("Hello world"))
        .maxAttempts(2).build();
    Retry retry = Retry.of("id", tryAgain);
    Supplier<String> supplier = Retry
        .decorateSupplier(retry, helloWorldService::returnHelloWorld);

    String result = supplier.get();

    then(helloWorldService).should(times(2)).returnHelloWorld();
    assertThat(result).isEqualTo("Hello world");
}
 
Example 6
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecorateSupplier() {
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException())
        .willReturn("Hello world");
    Retry retry = Retry.ofDefaults("id");
    Supplier<String> supplier = Retry
        .decorateSupplier(retry, helloWorldService::returnHelloWorld);

    String result = supplier.get();

    then(helloWorldService).should(times(2)).returnHelloWorld();
    assertThat(result).isEqualTo("Hello world");
    assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION);
}
 
Example 7
Source File: R4JTemplate.java    From x7 with Apache License 2.0 4 votes vote down vote up
@Override
public String support(String circuitBreakerKey, boolean isRetry, BackendService<String> backendService) {

    if (StringUtil.isNullOrEmpty(circuitBreakerKey)){
        circuitBreakerKey = "";
    }

    final String backendName = circuitBreakerKey.equals("") ? "default" : circuitBreakerKey;

    CircuitBreakerConfig circuitBreakerConfig = circuitBreakerRegistry.getConfiguration(backendName).orElse(circuitBreakerRegistry.getDefaultConfig());

    CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker(backendName,circuitBreakerConfig);
    Supplier<String> decoratedSupplier = CircuitBreaker
            .decorateSupplier(circuitBreaker, backendService::handle);

    if (isRetry) {
        RetryConfig retryConfig = retryRegistry.getConfiguration(backendName).orElse(retryRegistry.getDefaultConfig());
        Retry retry = retryRegistry.retry(backendName,retryConfig);
        if (retry != null) {

            retry.getEventPublisher()
                    .onRetry(event -> {
                        if (logger.isDebugEnabled()) {
                            logger.debug(event.getEventType().toString() + "_" + event.getNumberOfRetryAttempts() + ": backend("
                                    + backendName +")");
                        }
                    });

            decoratedSupplier = Retry
                    .decorateSupplier(retry, decoratedSupplier);
        }
    }

    String logStr = "Backend("+ backendName +")";

    String result = Try.ofSupplier(decoratedSupplier)
            .recover(e ->
                    hanleException(e, logStr, backendService)
            ).get();

    handleRemoteException(result,backendService);
    return result;
}
 
Example 8
Source File: Decorators.java    From resilience4j with Apache License 2.0 4 votes vote down vote up
public DecorateSupplier<T> withRetry(Retry retryContext) {
    supplier = Retry.decorateSupplier(retryContext, supplier);
    return this;
}