I am using AsyncRestTemplate as my HttpClient to execute my URL. And I am using exchange method of AsyncRestTemplate
which returns back a ListenableFuture
on which I am adding a callback to see whether the response was successful or not.
Below is my code:
private final AsyncRestTemplate restTemplate = new AsyncRestTemplate();
@Override
public ListenableFuture<String> getData(String userId) {
final SettableFuture<String> responseFuture = SettableFuture.create();
String hostname = "machineA";
// execute the URL
final org.springframework.util.concurrent.ListenableFuture<ResponseEntity<String>> orig = restTemplate
.exchange(createURL(hostname), HttpMethod.GET, null, String.class);
orig.addCallback(new ListenableFutureCallback<ResponseEntity<String>>() {
@Override
public void onSuccess(ResponseEntity<String> result) {
responseFuture.set(result.getBody());
}
@Override
public void onFailure(Throwable ex) {
// how should I check whether my server is down/not responding.
// What is the concrete type of exception I should check here?
}
});
return responseFuture;
}
Now my question is - Let's say if the server is down/not responding, then what kind of exception I will be getting in my onFailure
method. I want to differentiate between 4xx/5xx error with server not responding/timing out. What is the concrete type I should check in my onFailure
method to figure out whether my server was down or not responding.
Aucun commentaire:
Enregistrer un commentaire