I am new to Hystrix. I am trying to use it with Spring AOP. The following details what I am trying to achieve.
There is a "ServiceClass", into which some RestClient class is injected. I am using Spring. Now, I want to use Hystrix together with Spring AOP so that method calls to the RestClient from the ServiceClass could be made synchronously or asynchronously.
What I have done so far is as follows.
created a class "MyCommand" that extends HystrixCommand implements MethodInterceptor
implemented a method "execute(MethodInvocation m, String mode) in it as follows:
protected Object execute(MethodInvocation m, String mode){
this.mode = mode;
this.method = m;
return execute();}
in the (overridden method) "invoke"
public Object invoke(MethodInvocation m) throws throwable{
try{
execute(m,"Sync");
} catch(Exception e){
e.printStackTrace();
}
}
This "MyCommand" is being set as the AOP Interceptor for the "ServiceClass" in the spring-config file.
Now, the issue is; in my "Main" application when I retrieve the "ServiceClass" from the ClassPathXmlApplicationContext and invoke one method, it works fine. But if I try to invoke two of the methods of the "ServiceClass" it throws the following exception:
*java.lang.IllegalStateException: This instance can only be executed once. Please instantiate a new instance.*
Code snippet:
ServiceClass srvc = (ServiceClass) ctx.getBean("proxy");
srvc.getRequest();
srvc.postRequest();
I have spent almost three days trying to figure it out the cause and solution for this exception but without any good. Please help me get this right. What am I missing?
As always, Thanks in advance