Hystrix
Hystrix copied to clipboard
How to get the reason why hystrix execute fallbackMethod
I want to get the real reason why hystrix execute fallbackMethod ,like timeout , thread pool reject etc..
How can I get this fallback reason for each different method.
e.g.
@HystrixCommand(fallbackMethod=fb) public String foo(){return "bo";} public String fb(){ log.warn("why fall back , the reason is timeout"); return "fo"; }
You can use HystrixMetrics to find what your progarm happend. HystrixMetrics can tell you what kind of event happend in the past.
What's more, you can registe an executionhook to receive excution event; The following code is an example with Spring: `@Configuration public class HystrixConfig {
@Autowired
public void configHystrixPlugin() {
System.err.println("Configing Hystrix Pkugins");
HystrixPlugins.getInstance().registerCommandExecutionHook(new HystrixCommandExecutionHook() {
@Override
public <T> Exception onExecutionError(HystrixInvokable<T> commandInstance, Exception e) {
if(commandInstance instanceof HystrixInvokableInfo) {
HystrixInvokableInfo invokableInfo = (HystrixInvokableInfo)commandInstance;
//do something with specfied exception here
}
return e;
}
});
}
}`
Maybe this is an useful example for you,ex:
@HystrixCommand(fallbackMethod = "fb")
public String foo() {
return "bo";
}
public String fb(Throwable exception) {
logger.warn("real exception : {}", exception.getMessage());
return "fo";
}
@SoulSong I found this code style in hystrix-javanica demo ,thanks!
Javanica provides an ability to get execution exception (exception thrown that caused the failure of a command) within a fallback is being executed. A fallback method signature can be extended with an additional parameter in order to get an exception thrown by a command. Javanica exposes execution exception through additional parameter of fallback method. Execution exception is derived by calling method getExecutionException() as in vanilla hystrix.
Maybe this is an useful example for you,ex:
@HystrixCommand(fallbackMethod = "fb") public String foo() { return "bo"; } public String fb(Throwable exception) { logger.warn("real exception : {}", exception.getMessage()); return "fo"; }
In my case it doesn't work as expected. Throwable exception will present only if code inside HystrixCommand would throw real Exception. In cases of timeout, thread pool overflow, and short circuited state exception wil be equals to null. I'm using Hystrix Javanica 1.4.21.
@KirillLogin did you find a way to accomplish this?
@riser101 You could use the getFailedExecutionException()
method to figure out the cause, message et al. for the underlying exception.
Something like below:
@Override
public String getFallback() {
return getFailedExecutionException().getMessage();
}