in my current project, I'm required to use Hystrix for request fallback handling(mainly on request timeout fallback). I have tested a simple case, which is putting the @HystrixCommand annotation above one spring rest controller method, like the one below:
@RestController
public class xxxxxx {
@RequestMapping(value = "xxxxxxx")
@HystrixCommand(fallbackMethod="fallback", commandProperties = {
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "2000"),
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
})
public String xxxxxx(@RequestParam(value = "xxxx", required = true) String xxxx) {
....
return json.toString();
}
}
This works pretty well. Timeout is triggerred after 2 seconds and it goes into the fallback method I predefined. Now the problem comes: There are too many methods in on controller and there are many controllers in the project. Copy&paste the @HystrixCommand method by method would not be a good idea, and I'm required to implement it by spring aop. Then I wrote something as below:
@Aspect
@Configuration
public class TimeoutMonitor {
@Pointcut("execution(xxxxxxxx)")
public void excuteService() {}
@Around("excuteService()")
@HystrixCommand(fallbackMethod="fallback", commandProperties = {
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "2000"),
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
})
public Object monitor(ProceedingJoinPoint pjp){
try {
Object output = pjp.proceed();
return output;
}catch(Throwable e) {
return null;
}
}
@HystrixCommand
public String fallback(ProceedingJoinPoint pjp) {
JSONObject json = new JSONObject();
json.put("message", "request timeout");
return json.toString();
}
}
And it's not working... The fallback method is never reached
During the debugging, I have made sure that the logic flow goes through the flow of excuteService()->monitor()->matched method, and it's already passing 2 seconds, but the fallback method is never reached. I have researched on this problem and find that @HystrixCommand is also implemented by AOP. I guess putting one aop in another is the reason that causes this problem, but unfortunately I can't think of an approach to resolve it.
I would be grateful if someone can provide a solution. Solutions not implemented by aop are also admitted, but absolutely copy&paste @HystrixCommand above each method is not allowed.