public interface AsyncHttpClient extends Closeable
AsyncHttpClient c = new AsyncHttpClient(); Future<Response> f = c.prepareGet(TARGET_URL).execute();
AsyncHandler
or its abstract implementation, AsyncCompletionHandler
TheAsyncHttpClient c = new AsyncHttpClient(); Future<Response> f = c.prepareGet(TARGET_URL).execute(new AsyncCompletionHandler<Response>() { @Override public Response onCompleted(Response response) throws IOException { // Do something return response; } @Override public void onThrowable(Throwable t) { } }); Response response = f.get(); // We are just interested to retrieve the status code. Future<Integer> f = c.prepareGet(TARGET_URL).execute(new AsyncCompletionHandler<Integer>() { @Override public Integer onCompleted(Response response) throws IOException { // Do something return response.getStatusCode(); } @Override public void onThrowable(Throwable t) { } }); Integer statusCode = f.get();
AsyncCompletionHandler.onCompleted(Response)
will be invoked once the http response has been fully read, which include
the http headers and the response body. Note that the entire response will be buffered in memory.
AsyncHandler
You can asynchronously process the response status,headers and body and decide when to stop the processing the response by returning a newAsyncHttpClient c = new AsyncHttpClient(); Future<String> f = c.prepareGet(TARGET_URL).execute(new AsyncHandler<String>() { private StringBuilder builder = new StringBuilder(); @Override public STATE onStatusReceived(HttpResponseStatus s) throws Exception { // return STATE.CONTINUE or STATE.ABORT return STATE.CONTINUE } @Override public STATE onHeadersReceived(HttpResponseHeaders bodyPart) throws Exception { // return STATE.CONTINUE or STATE.ABORT return STATE.CONTINUE } @Override public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception { builder.append(new String(bodyPart)); // return STATE.CONTINUE or STATE.ABORT return STATE.CONTINUE } @Override public String onCompleted() throws Exception { // Will be invoked once the response has been fully read or a ResponseComplete exception // has been thrown. return builder.toString(); } @Override public void onThrowable(Throwable t) { } }); String bodyResponse = f.get();
AsyncHandler.State.ABORT
at any moment.
This class can also be used without the need of AsyncHandler
.
Finally, you can configure the AsyncHttpClient using anAsyncHttpClient c = new AsyncHttpClient(); Future<Response> f = c.prepareGet(TARGET_URL).execute(); Response r = f.get();
DefaultAsyncHttpClientConfig
instance.
AsyncHttpClient c = new AsyncHttpClient(new AsyncHttpClientConfig.Builder().setRequestTimeoutInMs(...).build()); Future<Response> f = c.prepareGet(TARGET_URL).execute(); Response r = f.get();
DefaultAsyncHttpClientConfig.getReadTimeout()
expires. This object can hold many persistent connections to different host.Modifier and Type | Method and Description |
---|---|
ListenableFuture<Response> |
executeRequest(Request request)
Execute an HTTP request.
|
<T> ListenableFuture<T> |
executeRequest(Request request,
AsyncHandler<T> handler)
Execute an HTTP request.
|
ListenableFuture<Response> |
executeRequest(RequestBuilder requestBuilder)
Execute an HTTP request.
|
<T> ListenableFuture<T> |
executeRequest(RequestBuilder requestBuilder,
AsyncHandler<T> handler)
Execute an HTTP request.
|
boolean |
isClosed()
Return true if closed
|
BoundRequestBuilder |
prepareConnect(String url)
Prepare an HTTP client CONNECT request.
|
BoundRequestBuilder |
prepareDelete(String url)
Prepare an HTTP client DELETE request.
|
BoundRequestBuilder |
prepareGet(String url)
Prepare an HTTP client GET request.
|
BoundRequestBuilder |
prepareHead(String url)
Prepare an HTTP client HEAD request.
|
BoundRequestBuilder |
prepareOptions(String url)
Prepare an HTTP client OPTIONS request.
|
BoundRequestBuilder |
preparePatch(String url)
Prepare an HTTP client PATCH request.
|
BoundRequestBuilder |
preparePost(String url)
Prepare an HTTP client POST request.
|
BoundRequestBuilder |
preparePut(String url)
Prepare an HTTP client PUT request.
|
BoundRequestBuilder |
prepareRequest(Request request)
Construct a
RequestBuilder using a Request |
BoundRequestBuilder |
prepareRequest(RequestBuilder requestBuilder)
Construct a
RequestBuilder using a RequestBuilder |
BoundRequestBuilder |
prepareTrace(String url)
Prepare an HTTP client TRACE request.
|
AsyncHttpClient |
setSignatureCalculator(SignatureCalculator signatureCalculator)
Set default signature calculator to use for requests build by this client instance
|
boolean isClosed()
AsyncHttpClient setSignatureCalculator(SignatureCalculator signatureCalculator)
signatureCalculator
- a signature calculatorRequestBuilder
BoundRequestBuilder prepareGet(String url)
url
- A well formed URL.RequestBuilder
BoundRequestBuilder prepareConnect(String url)
url
- A well formed URL.RequestBuilder
BoundRequestBuilder prepareOptions(String url)
url
- A well formed URL.RequestBuilder
BoundRequestBuilder prepareHead(String url)
url
- A well formed URL.RequestBuilder
BoundRequestBuilder preparePost(String url)
url
- A well formed URL.RequestBuilder
BoundRequestBuilder preparePut(String url)
url
- A well formed URL.RequestBuilder
BoundRequestBuilder prepareDelete(String url)
url
- A well formed URL.RequestBuilder
BoundRequestBuilder preparePatch(String url)
url
- A well formed URL.RequestBuilder
BoundRequestBuilder prepareTrace(String url)
url
- A well formed URL.RequestBuilder
BoundRequestBuilder prepareRequest(Request request)
RequestBuilder
using a Request
request
- a Request
RequestBuilder
BoundRequestBuilder prepareRequest(RequestBuilder requestBuilder)
RequestBuilder
using a RequestBuilder
requestBuilder
- a RequestBuilder
RequestBuilder
<T> ListenableFuture<T> executeRequest(Request request, AsyncHandler<T> handler)
T
- Type of the value that will be returned by the associated Future
request
- Request
handler
- an instance of AsyncHandler
Future
of type T<T> ListenableFuture<T> executeRequest(RequestBuilder requestBuilder, AsyncHandler<T> handler)
T
- Type of the value that will be returned by the associated Future
requestBuilder
- RequestBuilder
handler
- an instance of AsyncHandler
Future
of type TListenableFuture<Response> executeRequest(Request request)
ListenableFuture<Response> executeRequest(RequestBuilder requestBuilder)
requestBuilder
- RequestBuilder
Future
of type ResponseCopyright © 2016. All Rights Reserved.