I have an Hystrix command that requires cleanup when timed-out. Our current approach to handle this is as follows:
public class MyCommand extends HystrixCommand<MyResponse> {
@Override
public MyResponse run() {
// do stuff
// Cleanup if timed out
if( this.isResponseTimedOut() ) {
// perform cleanup
}
return myresponse;
}
}
Does the Hystrix framework provide another way for that?
As far as I know you can't determine reliably inside the
run()
method if a timeout has occured: Therun()
method is running in a separated thread while the caller is waiting. While you are checking in therun()
method if a timeout has occured, the calling thread might time out and you will not know about it. The code you suggest will work most of the time, but there is a small time window on each call where it will not work.A save place to check if a timeout has occured would be the
fallback()
method. Please note that the original "do stuff" as described in your method might still be running at this point: Hystrix will send the originalrun()
method ajava.lang.Thread.interrupt()
. Depending on your "do stuff" the results may vary.I hope this is an answer to your question. If you could give more information in your question, I can update my answer to include it.