Search code examples
javaspringresilience4j

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)


Solution

  • 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))
    }