private <T, E extends Exception> T execute(Request request, ResponseHandler<T, E> responseHandler, AtomicReference<String> state)
throws E
{
state.set("PROCESSING_REQUEST");
long requestStart = System.nanoTime();
Response response;
try {
response = processor.handle(request);
}
catch (Throwable e) {
state.set("FAILED");
long responseStart = System.nanoTime();
Duration requestProcessingTime = new Duration(responseStart - requestStart, TimeUnit.NANOSECONDS);
if (e instanceof Exception) {
try {
return responseHandler.handleException(request, (Exception) e);
}
finally {
stats.record(request.getMethod(),
0,
0,
0,
requestProcessingTime,
Duration.nanosSince(responseStart));
}
}
else {
stats.record(request.getMethod(),
0,
0,
0,
requestProcessingTime,
new Duration(0, TimeUnit.NANOSECONDS));
throw (Error) e;
}
}
checkState(response != null, "response is null");
// notify handler
state.set("PROCESSING_RESPONSE");
long responseStart = System.nanoTime();
Duration requestProcessingTime = new Duration(responseStart - requestStart, TimeUnit.NANOSECONDS);
try {
return responseHandler.handle(request, response);
}
finally {
state.set("DONE");
stats.record(request.getMethod(),
response.getStatusCode(),
response.getBytesRead(),
response.getBytesRead(),
requestProcessingTime,
Duration.nanosSince(responseStart));
}
}