CircuitBreaker.decorateSupplier catches Exception but Supplier can only throw RuntimeException
See original GitHub issueResilience4j version: 1.5.0
Java version: 1.11
Thanks for raising a Resilience4j issue.
I see that we catch a general Exception from supplier.get()
& publish an Error event. But Supplier only throws RuntimeExceptions.
If the Supplier can only throw RuntimeException, why not catch RuntimeException
and be explicit?
static <T> Supplier<T> decorateSupplier(CircuitBreaker circuitBreaker, Supplier<T> supplier) {
return () -> {
circuitBreaker.acquirePermission();
final long start = circuitBreaker.getCurrentTimestamp();
try {
T result = supplier.get();
long duration = circuitBreaker.getCurrentTimestamp() - start;
circuitBreaker.onResult(duration, circuitBreaker.getTimestampUnit(), result);
return result;
} catch (Exception exception) {
// Do not handle java.lang.Error
long duration = circuitBreaker.getCurrentTimestamp() - start;
circuitBreaker.onError(duration, circuitBreaker.getTimestampUnit(), exception);
throw exception;
}
};
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Resilience4j.retry with exception is not working - Stack Overflow
If you want to catch checked exceptions, you have to use decorateCallable .
Read more >Examples - resilience4j
Decorate your call to BackendService.doSomething() with a CircuitBreaker and execute the decorated supplier and recover from any exception.
Read more >Implementing Retry with Resilience4j - Reflectoring
This article is a deep dive into the Resilience4j retry module and shows why, when and how to use it to build resilient...
Read more >Guide to Resilience4j - Baeldung
Learn how to use the most useful modules from the Resilience4j library to build resilient systems.
Read more >CircuitBreaker.decorateSupplier - Java - Tabnine
public DecorateSupplier withCircuitBreaker(CircuitBreaker circuitBreaker) { supplier = CircuitBreaker.decorateSupplier(circuitBreaker, supplier);
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes, you can use
We have an internal
ThrowingSupplier
which is a wrapper around Supplier to allow checked exception in the lambda expressions. We sneakyThrow Exceptions as RuntimeExceptions.We were using
recordExceptions
list & assuming that retry would work for Suppliers. 🤦 My Bad.This is just an observation that we could catch only RuntimeExceptions because Suppliers can only throw RuntimeException.
@RobWin Is there a way to use
recordExceptions
to retry on cause instead of actual exception. We are wrapping our Checked Exceptions and throwing RuntimExceptions in ourThrowingSupplier
Is using CheckedFunction only way to make the Retry work on
recordExceptions
?Also, I can’t seem to find a Decorator for CheckedSupplier for ThreadpoolBulkhead. #1307