I’m trying to implement ‘Fallback’ functionality (try a different endpoint if the original one fails) using the async-http-client library. The below code is functional and works locally, tested against a small set of requests.
<code> request.execute(new AsyncCompletionHandler<Object>() {
@Override
public State onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
if(responseStatus.getStatusCode() != 201) {
return State.ABORT;
}
return super.onStatusReceived(responseStatus);
}
@Override
public Object onCompleted(Response response) throws Exception {
if(response != null) {
System.out.println("Main Sent sucessfully");
}
//If response is null meaning, getStatusCode != 201
else {
BoundRequestBuilder fallbackRequest = asynClient.preparePost(fallbackUrl).setBody("test");
Future<Response> responseFuture = fallbackRequest.execute();
Response rsp = responseFuture.get();
System.out.println("Sent - RESPCODE: [" + rsp.getStatusCode() + "]");
}
return response;
}
});
</code>
<code> request.execute(new AsyncCompletionHandler<Object>() {
@Override
public State onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
if(responseStatus.getStatusCode() != 201) {
return State.ABORT;
}
return super.onStatusReceived(responseStatus);
}
@Override
public Object onCompleted(Response response) throws Exception {
if(response != null) {
System.out.println("Main Sent sucessfully");
}
//If response is null meaning, getStatusCode != 201
else {
BoundRequestBuilder fallbackRequest = asynClient.preparePost(fallbackUrl).setBody("test");
Future<Response> responseFuture = fallbackRequest.execute();
Response rsp = responseFuture.get();
System.out.println("Sent - RESPCODE: [" + rsp.getStatusCode() + "]");
}
return response;
}
});
</code>
request.execute(new AsyncCompletionHandler<Object>() {
@Override
public State onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
if(responseStatus.getStatusCode() != 201) {
return State.ABORT;
}
return super.onStatusReceived(responseStatus);
}
@Override
public Object onCompleted(Response response) throws Exception {
if(response != null) {
System.out.println("Main Sent sucessfully");
}
//If response is null meaning, getStatusCode != 201
else {
BoundRequestBuilder fallbackRequest = asynClient.preparePost(fallbackUrl).setBody("test");
Future<Response> responseFuture = fallbackRequest.execute();
Response rsp = responseFuture.get();
System.out.println("Sent - RESPCODE: [" + rsp.getStatusCode() + "]");
}
return response;
}
});
My concern is with sending a blocking request in the onCompleted
handler. Even though it works locally on my machine, not confident if it is appropriate for a high load environment.
Below documentation in the AsyncHandler<T>
further reduces confidence.
<code> * Do NOT perform any blocking operation in there, typically trying to send another request and call get() on its future.
* There's a chance you might end up in a dead lock.
* If you really to perform blocking operation, executed it in a different dedicated thread pool.
</code>
<code> * Do NOT perform any blocking operation in there, typically trying to send another request and call get() on its future.
* There's a chance you might end up in a dead lock.
* If you really to perform blocking operation, executed it in a different dedicated thread pool.
</code>
* Do NOT perform any blocking operation in there, typically trying to send another request and call get() on its future.
* There's a chance you might end up in a dead lock.
* If you really to perform blocking operation, executed it in a different dedicated thread pool.
Any suggestions on alternative techniques to implement fallback using the same library?