2016-04-14 70 views
2

我們的代碼有幾個@Async方法和ApplicationListeners。這些類和方法最終調用具有@Hystrix註釋的API服務。這些調用拋出以下異常:Spring異步方法或事件拋出HystrixRequestContext.initializeContext()異常

java.lang.IllegalStateException: HystrixRequestContext.initializeContext() must be called at the beginning of each request before RequestVariable functionality can be used. 
at com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableDefault.get(HystrixRequestVariableDefault.java:76) ~[hystrix-core-1.4.10.jar:1.4.10] 
at com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableHolder.get(HystrixRequestVariableHolder.java:68) ~[hystrix-core-1.4.10.jar:1.4.10] 
at com.netflix.hystrix.HystrixRequestLog.getCurrentRequest(HystrixRequestLog.java:85) ~[hystrix-core-1.4.10.jar:1.4.10] 
at com.netflix.hystrix.AbstractCommand.<init>(AbstractCommand.java:274) ~[hystrix-core-1.4.10.jar:1.4.10] 
at com.netflix.hystrix.HystrixCommand.<init>(HystrixCommand.java:142) ~[hystrix-core-1.4.10.jar:1.4.10] 
at com.netflix.hystrix.HystrixCommand.<init>(HystrixCommand.java:128) ~[hystrix-core-1.4.10.jar:1.4.10] 
at com.netflix.hystrix.contrib.javanica.command.AbstractHystrixCommand.<init>(AbstractHystrixCommand.java:55) ~[hystrix-javanica-1.4.10.jar:1.4.10] 
at com.netflix.hystrix.contrib.javanica.command.GenericCommand.<init>(GenericCommand.java:33) ~[hystrix-javanica-1.4.10.jar:1.4.10] 
at com.netflix.hystrix.contrib.javanica.command.GenericHystrixCommandFactory.create(GenericHystrixCommandFactory.java:32) ~[hystrix-javanica-1.4.10.jar:1.4.10] 
at com.netflix.hystrix.contrib.javanica.command.GenericHystrixCommandFactory.create(GenericHystrixCommandFactory.java:21) ~[hystrix-javanica-1.4.10.jar:1.4.10] 
at com.netflix.hystrix.contrib.javanica.command.AbstractHystrixCommandFactory.create(AbstractHystrixCommandFactory.java:78) ~[hystrix-javanica-1.4.10.jar:1.4.10] 
at com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect.methodsAnnotatedWithHystrixCommand(HystrixCommandAspect.java:81) ~[hystrix-javanica-1.4.10.jar:1.4.10] 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60] 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_60] 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_60] 
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_60] 
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621) ~[spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610) ~[spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:68) ~[spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:168) [spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85) ~[spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at com.intuit.platform.common.aop.InstrumentationAspect.profile(InstrumentationAspect.java:25) ~[classes/:na] 
at sun.reflect.GeneratedMethodAccessor204.invoke(Unknown Source) ~[na:na] 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_60] 
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_60] 

在異步方法並不能幫助增加HystrixRequestContext.initializeContext()。

回答

0

您需要在請求發送之前初始化該值。 覆蓋HystrixRequestVariableDefault

欲瞭解更多信息這個問題在Github上:鼓勵https://github.com/Netflix/Hystrix/issues/895

+0

鏈接到外部資源,但請周圍添加鏈接的上下文,以便其他用戶將有一些想法是什麼,爲什麼它的存在。如果目標網站無法訪問或永久離線,請始終引用重要鏈接中最相關的部分。 – pableiros