resilience4j / resilience4j

Resilience4j is a fault tolerance library designed for Java8 and functional programming
Apache License 2.0
9.58k stars 1.32k forks source link

Passing Threadlocal to the fallbackMethod #936

Open wundorki opened 4 years ago

wundorki commented 4 years ago

Hello! Is there any way the Threadlocal can be passed to the fallbackMethod as well? I have tried using a ContextPropagator, but the clear method of the propagator is called before the fallbackMethod. Is there any other way of doing this?

RobWin commented 4 years ago

Hi,

I guess there is no option. Might be a good new enhancement / PR.

esteve8 commented 4 years ago

@RobWin i have a question for you, maybe you can help me.

I have an spring rest controller with differents end points, each endpoint returns differents types of objests. My config is :

@CircuitBreaker(name = "default", fallbackMethod = "fallback")

End points :

@GetMapping(value = "/test1") public Object1 test1() {

    Optional<Object1> object1= Object1Dao.findById(1);
    return object1.orElse(null);

}

@GetMapping(value = "/test2") public Object2 test2() {

    Optional<Object2> object2= Object2Dao.findById(1);
    return object2.orElse(null);
}

And the fallback:

public String fallback(Throwable ex) {
    return "Recovered with fallback: " + ex.getMessage();
}

The problem is that i got an error that the return type of the fallback has to be the same that the object type that returns the end point that throws the error.

Do you know how can i fix this? i appreciate your help so much.

Thanks so much.

RobWin commented 3 years ago

@esteve8 You can define two fallback methods with different return types.

esteve8 commented 3 years ago

ohh thats so great! congratulations for the effort and thanks @RobWin

kumarsen26 commented 8 months ago

any update on this?