4
votes

With the @HystrixCommand annotation there can be configured a fallback method which should run in case that the method fails.

    public Link defaultDogeLink(Account account) {
         return null;
    }

    @HystrixCommand(fallbackMethod = "defaultDogeLink")
    public Link buildDogeLink(Account account) {
         // some code that may throw Runtime Exceptions
    }  

What should I do in order to log (in a central class) the runtime exceptions thrown in all the methods annotated with @HystrixCommand ?

I am using spring-cloud-netflix and not vanilla hystrix-javanica. I am looking for something similar to org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler class (for Spring's @Async) that i need to implement in my application.

In hystrix-core, the HystrixCommand class has the method getFailedExecutionException() which can be used within the fallback method for logging the exception. Can somebody maybe point me how to get this exception when working with hystrix-javanica?

1
Plain Hystrix allows you to register a HystrixCommandExecutionHook using HystrixPlugins.getInstance().registerCommandExecutionHook(). I have no experience in a Spring environment with this, but you might want to give it a try. - ahus1
@marius_neo is right, @HystrixCommand is vanilla hystrix-javanica, there are no spring extensions to it right now, so give his advice a try. - spencergibb

1 Answers

3
votes

I found within the unit tests of hystrix-javanica the a way to get the last executed hystrix command:

public Link defaultDogeLink(Account account) {
     LOG.warn("exception occured while building doge link for account " + account, getCommand().getFailedExecutionException());
     return null;
}

@HystrixCommand(fallbackMethod = "defaultDogeLink")
public Link buildDogeLink(Account account) {
     // some code that may throw Runtime Exceptions
}  


private com.netflix.hystrix.HystrixInvokableInfo<?> getCommand() {
    return HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator().next();
}

It is a bit more verbose than expected, but fulfills my requirement.