Skip to content

Instantly share code, notes, and snippets.

@MaxDavila
Last active August 31, 2016 00:57
Show Gist options
  • Save MaxDavila/11c2a71f8c1ff1c0417acf237d2dda3f to your computer and use it in GitHub Desktop.
Save MaxDavila/11c2a71f8c1ff1c0417acf237d2dda3f to your computer and use it in GitHub Desktop.
Calling external service from qa and sbx
public TimedHttpResponse send(final HttpRequestBase request, Header ... additionalHeaders) throws TimedHttpCommunicationException {
String uriString = request.getURI().toString();
//set additional headers if any
request.setHeader("Accept", "application/json");
//let's run the actual http execution in another thread then wait for it
Future<HttpResponse> task = timeoutService.submit(new Callable<HttpResponse>() {
@Override
public HttpResponse call() throws Exception {
HttpResponse response = execute(request);
if (aborted.get()) {
//if we got aborted, we need to consume the response entity
//this will trigger a cleanup of the stream in addition to closing it
EntityUtils.consumeQuietly(response.getEntity());
}
return response;
}
});
HttpResponse httpResponse = task.get(timeoutMillis, TimeUnit.MILLISECONDS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment