Package com.ning.http.client
Class AsyncHttpClient
java.lang.Object
com.ning.http.client.AsyncHttpClient
- All Implemented Interfaces:
Closeable,AutoCloseable
This class support asynchronous and synchronous HTTP request.
To execute synchronous HTTP request, you just need to do
AsyncHttpClient c = new AsyncHttpClient(); Future</blockquote The code above will block until the response is fully received. To execute asynchronous HTTP request, you create anf = c.prepareGet("http://www.ning.com/").execute(); AsyncHandleror its abstract implementation,AsyncCompletionHandlerAsyncHttpClient c = new AsyncHttpClient(); Future</blockquote Thef = 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 aAsyncHandlerAsyncHttpClient c = new AsyncHttpClient(); Future</blockquote From any content classes, you can asynchronously process the response status,headers and body and decide when to stop the processing the response by throwing a new {link ResponseComplete} at any moment. This class can also be used without the need off = 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(); AsyncHandlerFinally, you can configure the AsyncHttpClient using anAsyncHttpClient c = new AsyncHttpClient(); Futuref = c.prepareGet(TARGET_URL).execute(); Response r = f.get(); AsyncHttpClientConfiginstanceAn 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.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected SignatureCalculatorDefault signature calculator to use for all requests constructed by this client instance. -
Constructor Summary
ConstructorsConstructorDescriptionCreate a new HTTP Asynchronous Client using the defaultAsyncHttpClientConfigconfiguration.AsyncHttpClient(AsyncHttpClientConfig config) Create a new HTTP Asynchronous Client using aAsyncHttpClientConfigconfiguration and theDEFAULT_PROVIDERAsyncHttpClient(AsyncHttpProvider provider) Create a new HTTP Asynchronous Client using an implementation ofAsyncHttpProviderand the defaultAsyncHttpClientConfigconfiguration.AsyncHttpClient(AsyncHttpProvider httpProvider, AsyncHttpClientConfig config) Create a new HTTP Asynchronous Client using aAsyncHttpClientConfigconfiguration and and aAsyncHttpProvider.AsyncHttpClient(String providerClass, AsyncHttpClientConfig config) Create a new HTTP Asynchronous Client using aAsyncHttpClientConfigconfiguration and and a AsyncHttpProvider class' name. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close the underlying connections.voidAsynchronous close theAsyncHttpProviderby spawning a thread and avoid blocking.executeRequest(Request request) Execute an HTTP request.<T> ListenableFuture<T>executeRequest(Request request, AsyncHandler<T> handler) Execute an HTTP request.protected voidfinalize()Return theAsyncHttpClientConfigReturn the asynchronousAsyncHttpProviderbooleanisClosed()Return true if closedprepareConnect(String url) Prepare an HTTP client CONNECT request.prepareDelete(String url) Prepare an HTTP client DELETE request.prepareGet(String url) Prepare an HTTP client GET request.prepareHead(String url) Prepare an HTTP client HEAD request.prepareOptions(String url) Prepare an HTTP client OPTIONS request.preparePatch(String url) Prepare an HTTP client PATCH request.preparePost(String url) Prepare an HTTP client POST request.preparePut(String url) Prepare an HTTP client PUT request.prepareRequest(Request request) Construct aRequestBuilderusing aRequestprepareTrace(String url) Prepare an HTTP client TRACE request.protected AsyncHttpClient.BoundRequestBuilderrequestBuilder(Request prototype) protected AsyncHttpClient.BoundRequestBuilderrequestBuilder(String method, String url) setSignatureCalculator(SignatureCalculator signatureCalculator) Set default signature calculator to use for requests build by this client instance
-
Field Details
-
signatureCalculator
Default signature calculator to use for all requests constructed by this client instance.- Since:
- 1.1
-
-
Constructor Details
-
AsyncHttpClient
public AsyncHttpClient()Create a new HTTP Asynchronous Client using the defaultAsyncHttpClientConfigconfiguration. The defaultAsyncHttpProviderwill be used (GrizzlyAsyncHttpProvider -
AsyncHttpClient
Create a new HTTP Asynchronous Client using an implementation ofAsyncHttpProviderand the defaultAsyncHttpClientConfigconfiguration.- Parameters:
provider- aAsyncHttpProvider
-
AsyncHttpClient
Create a new HTTP Asynchronous Client using aAsyncHttpClientConfigconfiguration and theDEFAULT_PROVIDER- Parameters:
config- aAsyncHttpClientConfig
-
AsyncHttpClient
Create a new HTTP Asynchronous Client using aAsyncHttpClientConfigconfiguration and and aAsyncHttpProvider.- Parameters:
config- aAsyncHttpClientConfighttpProvider- aAsyncHttpProvider
-
AsyncHttpClient
Create a new HTTP Asynchronous Client using aAsyncHttpClientConfigconfiguration and and a AsyncHttpProvider class' name.- Parameters:
config- aAsyncHttpClientConfigproviderClass- aAsyncHttpProvider
-
-
Method Details
-
getProvider
Return the asynchronousAsyncHttpProvider- Returns:
- an
AsyncHttpProvider
-
close
public void close()Close the underlying connections.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
closeAsynchronously
public void closeAsynchronously()Asynchronous close theAsyncHttpProviderby spawning a thread and avoid blocking. -
finalize
-
isClosed
public boolean isClosed()Return true if closed- Returns:
- true if closed
-
getConfig
Return theAsyncHttpClientConfig- Returns:
AsyncHttpClientConfig
-
setSignatureCalculator
Set default signature calculator to use for requests build by this client instance -
prepareGet
Prepare an HTTP client GET request.- Parameters:
url- A well formed URL.- Returns:
RequestBuilder
-
prepareConnect
Prepare an HTTP client CONNECT request.- Parameters:
url- A well formed URL.- Returns:
RequestBuilder
-
prepareOptions
Prepare an HTTP client OPTIONS request.- Parameters:
url- A well formed URL.- Returns:
RequestBuilder
-
prepareHead
Prepare an HTTP client HEAD request.- Parameters:
url- A well formed URL.- Returns:
RequestBuilder
-
preparePost
Prepare an HTTP client POST request.- Parameters:
url- A well formed URL.- Returns:
RequestBuilder
-
preparePut
Prepare an HTTP client PUT request.- Parameters:
url- A well formed URL.- Returns:
RequestBuilder
-
prepareDelete
Prepare an HTTP client DELETE request.- Parameters:
url- A well formed URL.- Returns:
RequestBuilder
-
preparePatch
Prepare an HTTP client PATCH request.- Parameters:
url- A well formed URL.- Returns:
RequestBuilder
-
prepareTrace
Prepare an HTTP client TRACE request.- Parameters:
url- A well formed URL.- Returns:
RequestBuilder
-
prepareRequest
Construct aRequestBuilderusing aRequest- Parameters:
request- aRequest- Returns:
RequestBuilder
-
executeRequest
Execute an HTTP request.- Type Parameters:
T- Type of the value that will be returned by the associatedFuture- Parameters:
request-Requesthandler- an instance ofAsyncHandler- Returns:
- a
Futureof type T
-
executeRequest
Execute an HTTP request. -
requestBuilder
-
requestBuilder
-