首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >记录来自hystrix命令的错误

记录来自hystrix命令的错误
EN

Stack Overflow用户
提问于 2015-07-08 00:19:59
回答 1查看 2.2K关注 0票数 4

有了@HystrixCommand注解,可以配置一个回退方法,该方法应该在方法失败的情况下运行。

代码语言:javascript
复制
    public Link defaultDogeLink(Account account) {
         return null;
    }

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

我应该怎么做才能(在一个中心类中)记录所有使用@HystrixCommand注解的方法中抛出的运行时异常?

我使用的是spring-cloud-netflix而不是vanilla hystrix-javanica。我正在寻找一些类似于org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler类的东西(用于Spring的@Async),我需要在我的应用程序中实现。

在hystrix-core中,HystrixCommand类具有方法getFailedExecutionException(),可以在回退方法中使用该方法来记录异常。有人能告诉我在使用hystrix-javanica时如何获得这个异常吗?

EN

回答 1

Stack Overflow用户

发布于 2015-07-08 17:08:00

我在hystrix-javanica的单元测试中找到了获取最后执行的hystrix命令的方法:

代码语言:javascript
复制
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();
}

它比预期的有点冗长,但满足了我的要求。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31274174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档