Search code examples
javaconfigurationtimeouthystrix

Hystrix ignores timeout on run


I'm experimenting with Hystrix a little.

I under stand the documentation such that even a synchronous call to a Hystrix command via 'run' runs by default in a thread and should be subject to the timeout configured in Hystrix. But when I try it, no timeout seems to happen.

Do I misinterpret the documentation? Or am I doing something wrong? And is there a way to get the timeout behavior with synchronous calls?

More specific: I have a 'SimpleService' that takes 5 seconds to return. This is wrapped in a Hystrix command with a timeout of 500ms:

public class WebRequestCommand extends HystrixCommand<String> {
    private final SimpleService baneService;

    protected WebRequestCommand(SimpleService baneService) {

        super(
                Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("test"))
                        .andCommandPropertiesDefaults(
                                HystrixCommandProperties.Setter()
                                        .withExecutionIsolationThreadTimeoutInMilliseconds(500)));
        this.baneService = baneService;
    }

    @Override
    protected String run() {
        return baneService.connectToBane();

    }

    @Override
    protected String getFallback() {
       return "SERVICE NOT AVAILABLE";
    }
}

If I call it like this:

WebRequestCommand webService = new WebRequestCommand(baneService);
result = webService.run();

I get the result after 5 Seconds => No timeout

If I call it like this:

WebRequestCommand webService = new WebRequestCommand(baneService);
result = webService.queue().get();

Hystrix timeout happens after 500ms and returns the fallback.


Solution

  • I think you should call execute() instead of run() for the synchronous way.