Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decorate function in Resilience4j circuit breaker with parameter

I want to decorate my service call with the newest resilience4j circuit breaker, my current code looks like:

@Bean
public Function<MyObject1, MyObject2> decoratedFunction(MyService myService) {
    CircuitBreakerRegistry registry = CircuitBreakerRegistry.ofDefaults();
    CircuitBreaker circuitBreaker = registry.circuitBreaker("circuitBreaker");
//decorateFunction method no longer exists :/
    return circuitBreaker.decorateFunction((myObject1) -> myService.makeACall(myObject1))
}

There used to be a method called decorateFunction which I would love to use but for an unknown reason it was removed in the latest version of resilience4j (I'm using latest 1.4 version)

Anyone knows why this function was removed and what is the current replacement for it? I see there are methods like decorateSupplier but I need to pass a parameter to my service (which is not allowed in case of supplier)

like image 750
Kacper Opyrchał Avatar asked Oct 26 '25 21:10

Kacper Opyrchał


2 Answers

Please use our Spring Boot Starter instead of creating your own CircuitBreakerRegistry. Then inject the auto-created CircuitBreakerRegistry into your code and retrieve a CircuitBreaker instance.

CircuitBreaker circuitBreaker = registry.circuitBreaker("circuitBreaker");

In your service code do:

public MyObject2 makeACall(MyObject1 myObject1) {
    return circuitBreaker.executeSupplier(() -> myService.makeACall(myObject1))
}
like image 55
Robert Winkler Avatar answered Oct 28 '25 14:10

Robert Winkler


Looks like in the latest version of resilience this method is static for some reason, so to use it simply:

@Bean
public Function<MyObject1, MyObject2> decoratedFunction(MyService myService) {
    CircuitBreakerRegistry registry = CircuitBreakerRegistry.ofDefaults();
    CircuitBreaker circuitBreaker = registry.circuitBreaker("circuitBreaker");
//decorateFunction method is static now
    return CircuitBreaker.decorateFunction(circuitBreaker, (myObject1) -> myService.makeACall(myObject1))
}
like image 42
Kacper Opyrchał Avatar answered Oct 28 '25 12:10

Kacper Opyrchał



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!