question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Retry isn't happening when using CompletableFuture return type

See original GitHub issue

Resilience4j version: 1.3.1

Java version: 1.8

I am doing simple unit test to test fallback method. For sync methods the Retry is working as expected i.e., calling the method 3 times, and then calling fallback method configured. But when using CompletableFuture return type the fallback method isn’t called. Can you let me know the correct usage when using CompletableFuture return type?

Test method

@Test
    public void testCreateHelloWorldCircuitBreaker() {
        for (int i = 0; i < 100; i++) {
            helloWorldService.helloWorldCircuitAndRetry(i);
        }
    }

Sync method sample, which is working great.

@Retry(name = HELLO_WORLD, fallbackMethod = "recover1")
	public void helloWorldCircuitAndRetry(int in) {
		
			if (in%2 == 0) {
				LOG.error("Test error!!!" + in);
				throw new RuntimeException("Intentional error to test Resiliency features");
			} else {
				LOG.info("Hello World!" + in);
			}
	}

@SuppressWarnings("unused")
	private void recover1(int in, RuntimeException ex) {
		LOG.info("Recovered ex: " + ex.getMessage());
    }

Async method sample,

@Retry(name = HELLO_WORLD, fallbackMethod = "recover1")
	public CompletableFuture<String> helloWorldCircuitAndRetry(int in) {
		
		return CompletableFuture.completedFuture(process(in));
	}

private String process(int in) {
		if (in%2 == 0) {
			LOG.error("Test error!!!" + in);
			throw new RuntimeException("Intentional error to test Resiliency features");
		} else {
			LOG.info("Hello World!" + in);
		}
		return "Hello World!" + in;
	}

@SuppressWarnings("unused")
	private CompletableFuture<String> recover1(int in, RuntimeException ex) {
		LOG.info("Recovered ex: " + ex.getMessage());
		return CompletableFuture.completedFuture("Recovered ex: " + in + " " + ex.getMessage());
    }

In case of async method, it’s throwing the error back to client, instead of retrying and calling fallback method.

java.util.concurrent.CompletionException: java.lang.RuntimeException: Intentional error to test Resiliency features
	at io.github.resilience4j.retry.configure.RetryAspect.lambda$handleJoinPointCompletableFuture$0(RetryAspect.java:196)
	at io.github.resilience4j.retry.Retry$AsyncRetryBlock.run(Retry.java:652)
	at io.github.resilience4j.retry.Retry.lambda$decorateCompletionStage$0(Retry.java:117)
	at io.github.resilience4j.retry.Retry.executeCompletionStage(Retry.java:487)
	at io.github.resilience4j.retry.configure.RetryAspect.handleJoinPointCompletableFuture(RetryAspect.java:192)
	at io.github.resilience4j.retry.configure.RetryAspect.proceed(RetryAspect.java:129)
	at io.github.resilience4j.retry.configure.RetryAspect.lambda$retryAroundAdvice$d44ce8ae$1(RetryAspect.java:123)
	at io.vavr.CheckedFunction0.lambda$andThen$ca02ab3$1(CheckedFunction0.java:265)
	at io.github.resilience4j.retry.configure.RetryAspect.retryAroundAdvice(RetryAspect.java:123)

application properties.

resilience4j.retry:
    configs:
        default:
            maxRetryAttempts: 3
            waitDuration: 100
            ignoreExceptions:
              - io.github.resilience4j.circuitbreaker.CallNotPermittedException
    instances:
        helloWorldService:
            baseConfig: default

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
rgampacommented, May 8, 2020

@RobWin with below update try/catch it’s working now.

try {
			future.complete(process(in));
		} catch (Exception ex) {
			future.completeExceptionally(ex);
		}
0reactions
rgampacommented, May 8, 2020

Yes, closing now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Retry logic with CompletableFuture - java - Stack Overflow
This currently doesn't compile because the return type of the lambda is wrong: it expects a Result , but the executeActionAsync returns a...
Read more >
Retry In The Future - Java Code Geeks - 2022
This infinite loop runs until the operation succeeds, or it throws an error that we don't like (not 'Retryable' ) or we run...
Read more >
CompletableFuture (Java Platform SE 8 ) - Oracle Help Center
Returns a new CompletableFuture that is asynchronously completed by a task running in the given executor with the value obtained by calling the...
Read more >
Asynchronous Retry Pattern - DZone
Please look carefully! getWithRetry() does not block. It returns CompletableFuture immediately and invokes given function asynchronously.
Read more >
FutureUtils (Flink : 1.12-SNAPSHOT API)
Retry the given operation the given number of times in case of a failure only when an exception is retryable. Type Parameters: T...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found