0

I have a function in my @Service class that is marked with @HystrixCommand.

This method acts as a client which sends a request to another service URL and gets back a response.

What I want to do is to trigger a fallback function when the response status code is anything other than 200. It will also trigger a fallback for any other exceptions (RuntimeExceptions etc.).

I want to do this by making use of the @HystrixProperty or @HystrixCommandProperty.

I want the client to ping the URL and listen for a 200 response status and if it does not get back a 200 status within a certain time-frame I want it to fallback.

If it gets back a 200 status normally within a certain time it should not trigger the fallback.

@HystrixCommand(fallbackMethod="fallbackPerformOperation")
    public Future<Object> performOperation(String requestString) throws InterruptedException 

        return new AsyncResult<Object>() {

            @Override
            public Object invoke() {

                Client client = null;
                WebResource webResource  = null;
                ClientResponse response =null;
                String results = null; 
                try{
                    client = Client.create();       
                    webResource = client.resource(URL);
                    client.setConnectTimeout(10000);
                    client.setReadTimeout(10000);
                    response = webResource.type("application/xml")
                       .post(ClientResponse.class, requestString);  

              } finally {
                  client.destroy();
                  webResource = null;
              }

                return results;
            }

        };
}

I specifically want to make use of the @HystrixProperty or @HystrixCommandProperty so performing a check inside the method for response status code not being 200 and then throwing an Exception is not acceptable.

Instead of using Annotations will creating my own Command by extending the HystrixCommand Interface work?

Any ideas or resources for where I can start with this are more than welcome.

Anshuman Tripathy
  • 113
  • 1
  • 3
  • 12

1 Answers1

0

I don’t understand why you don’t want to check the response http status code and throw an exception if it is not 200? Doing that will give you the behaviour you desire. i.e. it will trigger a fall back for exceptions or non 200 responses.

You can set the timeout in the client, however I would opt for using the hystrix timeout values. That way you can use Archaius to dynamically change the value at runtime if desired.

You can use the Hystrix command annotation or extend the HystrixCommand class. Both options will provide you with your desired behaviour

Here is an example using the annotation.

 @HystrixCommand(fallbackMethod = "getRequestFallback")
  public String performGetRequest(String uri) {
    Client client = Client.create();
    WebResource webResource = client.resource(uri);
    ClientResponse response = webResource.get(ClientResponse.class);
    if (response.getStatus() != 200) {
      throw new RuntimeException("Invalid response status");
    }
    return response.getEntity(String.class);
  }

  public String getRequestFallback(String uri) {
    return "Fallback Value";
  }
Davin
  • 233
  • 6
  • 12