IllegalStateException with Hystrix



我是Hystrix的新手。我正在尝试将其与Spring AOP一起使用。下面详细介绍了我想要实现的目标。

有一个"ServiceClass",其中注入了一些 RestClient 类。我正在使用弹簧。现在,我想将 Hystrix 与 Spring AOP 一起使用,以便可以从 ServiceClass 同步或异步地对 RestClient 进行方法调用。

到目前为止,我所做的如下。

创建了一个扩展 HystrixCommand 实现 MethodInterceptor 的类 "MyCommand"

在其中实现了一个方法"execute(MethodInvocation m,字符串模式),如下所示:

                      protected Object execute(MethodInvocation m, String mode){
                      this.mode = mode;
                      this.method = m;
                      return execute();}

在(重写方法)"调用"中

                       public Object invoke(MethodInvocation m) throws throwable{
                         try{
                              execute(m,"Sync");
                         } catch(Exception e){
                             e.printStackTrace();
                         }
                        }

这个"MyCommand"被设置为spring配置文件中"ServiceClass"的AOP拦截器。

现在,问题是;在我的"主"应用程序中,当我从ClassPathXmlApplicationContext中检索"ServiceClass"并调用一种方法时,它工作正常。但是,如果我尝试调用"ServiceClass"的两个方法,它会引发以下异常:

              *java.lang.IllegalStateException: This instance can only be executed once. Please instantiate a new instance.*

代码片段:

              ServiceClass srvc = (ServiceClass) ctx.getBean("proxy");
              srvc.getRequest();
              srvc.postRequest(); 

我花了将近三天的时间试图找出此异常的原因和解决方案,但没有任何好处。请帮我把这件事做好。我错过了什么?

一如既往,提前致谢

一个 HystrixCommand 对象只能使用一次,因为它在执行后包含可用于进一步处理的有用状态信息。例如,如果要在调用超时时在事后执行特殊处理run(),则可以执行以下操作:

public class MyHystrixCommand extends HystrixCommand<Object> { ... }
MyHystrixCommand command = new MyHystrixCommand();
command.execute();
if(command.isResponseTimedOut()){
    //do post processing
}

如果可以多次调用execute(),尤其是从多个线程调用,就像 REST 终结点有多个使用者时发生的那样,则无法知道在查询命令的状态时哪个调用超时。

最新更新