0

I have a method which has been marked as @Async in my @Service Class. This returns a Future type.

This method basically acts as a client which calls a service in another URL (marked here as URL).

@Async
public Future<Object> performOperation(String requestString) throws InterruptedException {
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);
if(response.getStatus()!=200) {
   webResource=null;
   logger.error("request failed with HTTP Status: " + response.getStatus());
   throw new RuntimeException("request failed with HTTP Status: " + response.getStatus());
  }
results=response.getEntity(String.class);
 } finally {
    client.destroy();
    webResource=null;
    }
   return new AsyncResult<>(results);
}

I want to convert this @Async method into an Asynchronous @HystrixCommand Method in the following format:

@HystrixCommand
    public Future<Object> performOperation(String requestString) throws InterruptedException {
        return new AsyncResult<Object>() {

            @Override
            public Product invoke() {
                ...
                return results;
            }
        };
    }

But when I do this it throws the following errors in my Code:

for the line return new AsyncResult<Object>() {...}it says

The Constructor AsyncResult() is Undefined.

When I ask Eclipse to fix the error it adds the requestString Object into the constructor parameter i.e. AsyncResult<Object>(requestString)

Also it asks me to remove the @Override from the invoke() Method.

Its says

The method invoke() of type new AsyncResult(){} must override or implement a supertype method.

But on asking eclipse to fix the error for me it removes the @Override

My Question is How do I make the @Async method into an Asynchronous @HystrixCommand Method without any of these issues?

I would also like to implement an asynchronous fallback for this method which shows a default message to the User in case the response status code is not 200.

How do I go about doing this?

Thank You.

Anshuman Tripathy
  • 113
  • 1
  • 3
  • 12

1 Answers1

0

From this message

Also it asks me to remove the @Override from the invoke() Method., It looks like You are using org.springframework.scheduling.annotation.AsyncResult
and not com.netflix.hystrix.contrib.javanica.command.AsyncResult.

Changing the type should fix the problem.

You can provide a fallback similar to how you would do it in case of synchronous method.

@HystrixCommand(fallbackMethod="myFallbackMethod")
    public Future<Object> performOperation(String requestString) throws InterruptedException {
        return new AsyncResult<Object>() {

            @Override
            public Product invoke() {
                ...
                return results;
            }
        };
    }

 public Future<Object> myFallbackMethod(String requestString) {
      return new AsyncResult<Object>() {

            @Override
            public Product invoke() {
               ....... // return some predefined results;
            }

   }

One more thing. Instead of declaring as Genric Object. Specify it as any Concrete Type like Product

public Future<Product> performOperation(String requestString) throws InterruptedException {
   .....


 }
pvpkiran
  • 25,582
  • 8
  • 87
  • 134
  • try using the synchronous fallback method and see if it works. I mean Change the fallback method signature to return `Object` instead of `Future` – pvpkiran Jan 08 '18 at 17:51
  • check for more information here https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica#fallback – pvpkiran Jan 08 '18 at 17:52
  • Ok I think I can make use of HystrixProperty annotation in the commandProperties to choose when I want the method to fallback. i.e. I can specify in the HystrixProperty that if the response status code is not 200 I want it to fallback. I know people use HystrixProperty to trigger fallback if the timeout exceeds certain seconds i.e. HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="5000"). Is there something I could use to check response status codes? – Anshuman Tripathy Jan 08 '18 at 20:07