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.