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

The following examples show how to use io.github.resilience4j.retry.Retry#decorateCallable() . 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: ResilienceHandler.java    From cloud-espm-cloud-native with Apache License 2.0 7 votes vote down vote up
/**
 * This method returns a desired Tax(taxAmount, taxPercentage) value when the TaxService is up. 
 * If the TaxService is down, it applies a combination of following fault tolerance patterns 
 * in a sequence: TimeLimiter, CircuitBreaker and Retry using a Callable. When all the attempts 
 * are exhausted it calls a fallback method to recover from failure and offers the default tax value.
 * 
 * @param amount
 * @return
 */
public Tax applyResiliencePatterns(BigDecimal amount) {

	CircuitBreaker circuitBreaker = configureCircuitBreaker();
	TimeLimiter timeLimiter = configureTimeLimiter();
	Retry retry = configureRetry();
	
	Supplier<CompletableFuture<Tax>> futureSupplier = () -> CompletableFuture.supplyAsync(() -> salesOrderService.supplyTax(amount));
	Callable<Tax> callable = TimeLimiter.decorateFutureSupplier(timeLimiter, futureSupplier);
	callable = CircuitBreaker.decorateCallable(circuitBreaker, callable);
	callable = Retry.decorateCallable(retry, callable);
	
	//Executing the decorated callable and recovering from any exception by calling the fallback method
	Try<Tax> result = Try.ofCallable(callable).recover(throwable -> taxServiceFallback(amount));
	return result.get();
}
 
Example 2
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecorateCallableWithRetryResult() throws Exception {
    given(helloWorldService.returnHelloWorldWithException())
        .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);
    Callable<String> callable = Retry
        .decorateCallable(retry, helloWorldService::returnHelloWorldWithException);

    String result = callable.call();

    then(helloWorldService).should(times(2)).returnHelloWorldWithException();
    assertThat(result).isEqualTo("Hello world");
    assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION);
}
 
Example 3
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecorateCallable() throws Exception {
    given(helloWorldService.returnHelloWorldWithException())
        .willThrow(new HelloWorldException())
        .willReturn("Hello world");
    Retry retry = Retry.ofDefaults("id");
    Callable<String> callable = Retry
        .decorateCallable(retry, helloWorldService::returnHelloWorldWithException);

    String result = callable.call();

    then(helloWorldService).should(times(2)).returnHelloWorldWithException();
    assertThat(result).isEqualTo("Hello world");
    assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION);
}
 
Example 4
Source File: Decorators.java    From resilience4j with Apache License 2.0 4 votes vote down vote up
public DecorateCallable<T> withRetry(Retry retryContext) {
    callable = Retry.decorateCallable(retryContext, callable);
    return this;
}