Package org.asynchttpclient
Interface AsyncHttpClient
- 
- All Superinterfaces:
- AutoCloseable,- Closeable
 - All Known Implementing Classes:
- DefaultAsyncHttpClient
 
 public interface AsyncHttpClient extends Closeable This class support asynchronous and synchronous HTTP requests.
 To execute a synchronous HTTP request, you just need to doAsyncHttpClient c = new AsyncHttpClient(); Future<Response> f = c.prepareGet(TARGET_URL).execute();
 The code above will block until the response is fully received. To execute an asynchronous HTTP request, you create anAsyncHandleror 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 in retrieving 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)method will be invoked once the http response has been fully read. TheResponseobject includes the http headers and the response body. Note that the entire response will be buffered in memory.
 You can also have more control about how the response is asynchronously processed by using anAsyncHandler
 You can asynchronously process the response status, headers and body and decide when to stop 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.ABORTat any moment.This class can also be used without the need of AsyncHandler.
 AsyncHttpClient c = new AsyncHttpClient(); Future<Response> f = c.prepareGet(TARGET_URL).execute(); Response r = f.get();Finally, you can configure the AsyncHttpClient using an DefaultAsyncHttpClientConfiginstance.
 AsyncHttpClient c = new AsyncHttpClient(new DefaultAsyncHttpClientConfig.Builder().setRequestTimeout(...).build()); Future<Response> f = c.prepareGet(TARGET_URL).execute(); Response r = f.get();
 An instance of this class will cache every HTTP 1.1 connection and close them when theDefaultAsyncHttpClientConfig.getReadTimeout()expires. This object can hold many persistent connections to different hosts.
- 
- 
Method SummaryAll Methods Instance Methods Abstract Methods Modifier and Type Method Description ListenableFuture<Response>executeRequest(Request request)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.<T> ListenableFuture<T>executeRequest(Request request, AsyncHandler<T> handler)Execute an HTTP request.voidflushChannelPoolPartitions(Predicate<Object> predicate)Flush ChannelPool partitions based on a predicateClientStatsgetClientStats()Return details about pooled connections.AsyncHttpClientConfiggetConfig()Return the config associated to this client.booleanisClosed()Return true if closedBoundRequestBuilderprepare(String method, String url)Prepare an HTTP client request.BoundRequestBuilderprepareConnect(String url)Prepare an HTTP client CONNECT request.BoundRequestBuilderprepareDelete(String url)Prepare an HTTP client DELETE request.BoundRequestBuilderprepareGet(String url)Prepare an HTTP client GET request.BoundRequestBuilderprepareHead(String url)Prepare an HTTP client HEAD request.BoundRequestBuilderprepareOptions(String url)Prepare an HTTP client OPTIONS request.BoundRequestBuilderpreparePatch(String url)Prepare an HTTP client PATCH request.BoundRequestBuilderpreparePost(String url)Prepare an HTTP client POST request.BoundRequestBuilderpreparePut(String url)Prepare an HTTP client PUT request.BoundRequestBuilderprepareRequest(Request request)Construct aRequestBuilderusing aRequestBoundRequestBuilderprepareRequest(RequestBuilder requestBuilder)Construct aRequestBuilderusing aRequestBuilderBoundRequestBuilderprepareTrace(String url)Prepare an HTTP client TRACE request.AsyncHttpClientsetSignatureCalculator(SignatureCalculator signatureCalculator)Set default signature calculator to use for requests built by this client instance
 
- 
- 
- 
Method Detail- 
isClosedboolean isClosed() Return true if closed- Returns:
- true if closed
 
 - 
setSignatureCalculatorAsyncHttpClient setSignatureCalculator(SignatureCalculator signatureCalculator) Set default signature calculator to use for requests built by this client instance- Parameters:
- signatureCalculator- a signature calculator
- Returns:
- RequestBuilder
 
 - 
prepareBoundRequestBuilder prepare(String method, String url) Prepare an HTTP client request.- Parameters:
- method- HTTP request method type. MUST BE in upper case
- url- A well-formed URL.
- Returns:
- RequestBuilder
 
 - 
prepareGetBoundRequestBuilder prepareGet(String url) Prepare an HTTP client GET request.- Parameters:
- url- A well-formed URL.
- Returns:
- RequestBuilder
 
 - 
prepareConnectBoundRequestBuilder prepareConnect(String url) Prepare an HTTP client CONNECT request.- Parameters:
- url- A well-formed URL.
- Returns:
- RequestBuilder
 
 - 
prepareOptionsBoundRequestBuilder prepareOptions(String url) Prepare an HTTP client OPTIONS request.- Parameters:
- url- A well-formed URL.
- Returns:
- RequestBuilder
 
 - 
prepareHeadBoundRequestBuilder prepareHead(String url) Prepare an HTTP client HEAD request.- Parameters:
- url- A well-formed URL.
- Returns:
- RequestBuilder
 
 - 
preparePostBoundRequestBuilder preparePost(String url) Prepare an HTTP client POST request.- Parameters:
- url- A well-formed URL.
- Returns:
- RequestBuilder
 
 - 
preparePutBoundRequestBuilder preparePut(String url) Prepare an HTTP client PUT request.- Parameters:
- url- A well-formed URL.
- Returns:
- RequestBuilder
 
 - 
prepareDeleteBoundRequestBuilder prepareDelete(String url) Prepare an HTTP client DELETE request.- Parameters:
- url- A well-formed URL.
- Returns:
- RequestBuilder
 
 - 
preparePatchBoundRequestBuilder preparePatch(String url) Prepare an HTTP client PATCH request.- Parameters:
- url- A well-formed URL.
- Returns:
- RequestBuilder
 
 - 
prepareTraceBoundRequestBuilder prepareTrace(String url) Prepare an HTTP client TRACE request.- Parameters:
- url- A well-formed URL.
- Returns:
- RequestBuilder
 
 - 
prepareRequestBoundRequestBuilder prepareRequest(Request request) Construct aRequestBuilderusing aRequest- Parameters:
- request- a- Request
- Returns:
- RequestBuilder
 
 - 
prepareRequestBoundRequestBuilder prepareRequest(RequestBuilder requestBuilder) Construct aRequestBuilderusing aRequestBuilder- Parameters:
- requestBuilder- a- RequestBuilder
- Returns:
- RequestBuilder
 
 - 
executeRequest<T> ListenableFuture<T> executeRequest(Request request, AsyncHandler<T> handler) Execute an HTTP request.- Type Parameters:
- T- Type of the value that will be returned by the associated- Future
- Parameters:
- request-- Request
- handler- an instance of- AsyncHandler
- Returns:
- a Futureof type T
 
 - 
executeRequest<T> ListenableFuture<T> executeRequest(RequestBuilder requestBuilder, AsyncHandler<T> handler) Execute an HTTP request.- Type Parameters:
- T- Type of the value that will be returned by the associated- Future
- Parameters:
- requestBuilder-- RequestBuilder
- handler- an instance of- AsyncHandler
- Returns:
- a Futureof type T
 
 - 
executeRequestListenableFuture<Response> executeRequest(Request request) Execute an HTTP request.
 - 
executeRequestListenableFuture<Response> executeRequest(RequestBuilder requestBuilder) Execute an HTTP request.- Parameters:
- requestBuilder-- RequestBuilder
- Returns:
- a Futureof type Response
 
 - 
getClientStatsClientStats getClientStats() Return details about pooled connections.- Returns:
- a ClientStats
 
 - 
flushChannelPoolPartitionsvoid flushChannelPoolPartitions(Predicate<Object> predicate) Flush ChannelPool partitions based on a predicate- Parameters:
- predicate- the predicate
 
 - 
getConfigAsyncHttpClientConfig getConfig() Return the config associated to this client.- Returns:
- the config associated to this client.
 
 
- 
 
-