public class AsyncHttpClient extends Object implements Closeable
The code above will block until the response is fully received. To execute asynchronous HTTP request, you create anAsyncHttpClient c = new AsyncHttpClient(); Futuref = c.prepareGet("http://www.ning.com/").execute();
AsyncHandler
or its abstract implementation, AsyncCompletionHandler
AsyncHttpClient c = new AsyncHttpClient(); Futuref = c.prepareGet("http://www.ning.com/").execute(new AsyncCompletionHandler () { @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 f = c.prepareGet("http://www.ning.com/").execute(new AsyncCompletionHandler () { @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(com.ning.http.client.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.
You can also have more control about the how the response is asynchronously processed by using a AsyncHandler
This class can also be used without the need ofAsyncHttpClient c = new AsyncHttpClient(); Futuref = c.prepareGet("http://www.ning.com/").execute(new AsyncHandler () { 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
Finally, you can configure the AsyncHttpClient using anAsyncHttpClient c = new AsyncHttpClient(); Futuref = c.prepareGet(TARGET_URL).execute(); Response r = f.get();
AsyncHttpClientConfig
instance
An instance of this class will cache every HTTP 1.1 connections and close them when theAsyncHttpClient c = new AsyncHttpClient(new AsyncHttpClientConfig.Builder().setRequestTimeout(...).build()); Futuref = c.prepareGet(TARGET_URL).execute(); Response r = f.get();
AsyncHttpClientConfig.getReadTimeout()
expires. This object can hold many persistent connections to different host.Modifier and Type | Class and Description |
---|---|
class |
AsyncHttpClient.BoundRequestBuilder |
Modifier and Type | Field and Description |
---|---|
protected SignatureCalculator |
signatureCalculator
Default signature calculator to use for all requests constructed by this client instance.
|
Constructor and Description |
---|
AsyncHttpClient()
Create a new HTTP Asynchronous Client using the default
AsyncHttpClientConfig configuration. |
AsyncHttpClient(AsyncHttpClientConfig config)
Create a new HTTP Asynchronous Client using a
AsyncHttpClientConfig configuration and the
DEFAULT_PROVIDER |
AsyncHttpClient(AsyncHttpProvider provider)
Create a new HTTP Asynchronous Client using an implementation of
AsyncHttpProvider and
the default AsyncHttpClientConfig configuration. |
AsyncHttpClient(AsyncHttpProvider httpProvider,
AsyncHttpClientConfig config)
Create a new HTTP Asynchronous Client using a
AsyncHttpClientConfig configuration and
and a AsyncHttpProvider . |
AsyncHttpClient(String providerClass,
AsyncHttpClientConfig config)
Create a new HTTP Asynchronous Client using a
AsyncHttpClientConfig configuration and
and a AsyncHttpProvider class' name. |
protected SignatureCalculator signatureCalculator
public AsyncHttpClient()
AsyncHttpClientConfig
configuration. The
default AsyncHttpProvider
will be used (NettyAsyncHttpProvider
public AsyncHttpClient(AsyncHttpProvider provider)
AsyncHttpProvider
and
the default AsyncHttpClientConfig
configuration.provider
- a AsyncHttpProvider
public AsyncHttpClient(AsyncHttpClientConfig config)
AsyncHttpClientConfig
configuration and the
DEFAULT_PROVIDER
config
- a AsyncHttpClientConfig
public AsyncHttpClient(AsyncHttpProvider httpProvider, AsyncHttpClientConfig config)
AsyncHttpClientConfig
configuration and
and a AsyncHttpProvider
.config
- a AsyncHttpClientConfig
httpProvider
- a AsyncHttpProvider
public AsyncHttpClient(String providerClass, AsyncHttpClientConfig config)
AsyncHttpClientConfig
configuration and
and a AsyncHttpProvider class' name.config
- a AsyncHttpClientConfig
providerClass
- a AsyncHttpProvider
public AsyncHttpProvider getProvider()
AsyncHttpProvider
AsyncHttpProvider
public void close()
close
in interface Closeable
close
in interface AutoCloseable
public void closeAsynchronously()
AsyncHttpProvider
by spawning a thread and avoid blocking.protected void finalize() throws Throwable
public boolean isClosed()
public AsyncHttpClientConfig getConfig()
AsyncHttpClientConfig
AsyncHttpClientConfig
public AsyncHttpClient setSignatureCalculator(SignatureCalculator signatureCalculator)
public AsyncHttpClient.BoundRequestBuilder prepareGet(String url)
url
- A well formed URL.RequestBuilder
public AsyncHttpClient.BoundRequestBuilder prepareConnect(String url)
url
- A well formed URL.RequestBuilder
public AsyncHttpClient.BoundRequestBuilder prepareOptions(String url)
url
- A well formed URL.RequestBuilder
public AsyncHttpClient.BoundRequestBuilder prepareHead(String url)
url
- A well formed URL.RequestBuilder
public AsyncHttpClient.BoundRequestBuilder preparePost(String url)
url
- A well formed URL.RequestBuilder
public AsyncHttpClient.BoundRequestBuilder preparePut(String url)
url
- A well formed URL.RequestBuilder
public AsyncHttpClient.BoundRequestBuilder prepareDelete(String url)
url
- A well formed URL.RequestBuilder
public AsyncHttpClient.BoundRequestBuilder preparePatch(String url)
url
- A well formed URL.RequestBuilder
public AsyncHttpClient.BoundRequestBuilder prepareTrace(String url)
url
- A well formed URL.RequestBuilder
public AsyncHttpClient.BoundRequestBuilder prepareRequest(Request request)
RequestBuilder
using a Request
request
- a Request
RequestBuilder
public <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 Tpublic ListenableFuture<Response> executeRequest(Request request)
protected AsyncHttpClient.BoundRequestBuilder requestBuilder(String method, String url)
protected AsyncHttpClient.BoundRequestBuilder requestBuilder(Request prototype)
Copyright © 2015. All Rights Reserved.