com.ning.http.client
Class AsyncHttpClient

java.lang.Object
  extended by com.ning.http.client.AsyncHttpClient

public class AsyncHttpClient
extends Object

This class support asynchronous and synchronous HTTP request. To execute synchronous HTTP request, you just need to do

    AsyncHttpClient c = new AsyncHttpClient();
    Future f = c.prepareGet("http://www.ning.com/").execute();
 
AsyncHandler or its abstract implementation, AsyncCompletionHandler
       AsyncHttpClient c = new AsyncHttpClient();
       Future f = 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
      AsyncHttpClient c = new AsyncHttpClient();
      Future f = 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();
 
HttpContent sub 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 of AsyncHandler

      AsyncHttpClient c = new AsyncHttpClient();
      Future f = c.prepareGet(TARGET_URL).execute();
      Response r = f.get();
 
Finally, you can configure the AsyncHttpClient using an AsyncHttpClientConfig instance

      AsyncHttpClient c = new AsyncHttpClient(new AsyncHttpClientConfig.Builder().setRequestTimeoutInMs(...).build());
      Future f = c.prepareGet(TARGET_URL).execute();
      Response r = f.get();
 
An instance of this class will cache every HTTP 1.1 connections and close them when the AsyncHttpClientConfig.getIdleConnectionTimeoutInMs() expires. This object can hold many persistent connections to different host.


Nested Class Summary
 class AsyncHttpClient.BoundRequestBuilder
           
 
Field Summary
protected  SignatureCalculator signatureCalculator
          Default signature calculator to use for all requests constructed by this client instance.
 
Constructor Summary
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.
 
Method Summary
 void close()
          Close the underlying connections.
 ListenableFuture<Response> executeRequest(Request request)
          Execute an HTTP request.
<T> ListenableFuture<T>
executeRequest(Request request, AsyncHandler<T> handler)
          Execute an HTTP request.
protected  void finalize()
           
 AsyncHttpClientConfig getConfig()
          Return the AsyncHttpClientConfig
 AsyncHttpProvider getProvider()
          Return the asynchronous AsyncHttpProvider
 boolean isClosed()
          Return true if closed
 AsyncHttpClient.BoundRequestBuilder prepareConnect(String url)
          Prepare an HTTP client CONNECT request.
 AsyncHttpClient.BoundRequestBuilder prepareDelete(String url)
          Prepare an HTTP client DELETE request.
 AsyncHttpClient.BoundRequestBuilder prepareGet(String url)
          Prepare an HTTP client GET request.
 AsyncHttpClient.BoundRequestBuilder prepareHead(String url)
          Prepare an HTTP client HEAD request.
 AsyncHttpClient.BoundRequestBuilder prepareOptions(String url)
          Prepare an HTTP client OPTIONS request.
 AsyncHttpClient.BoundRequestBuilder preparePost(String url)
          Prepare an HTTP client POST request.
 AsyncHttpClient.BoundRequestBuilder preparePut(String url)
          Prepare an HTTP client PUT request.
 AsyncHttpClient.BoundRequestBuilder prepareRequest(Request request)
          Construct a AsyncHttpClient.BoundRequestBuilder using a Request
protected  AsyncHttpClient.BoundRequestBuilder requestBuilder(Request prototype)
           
protected  AsyncHttpClient.BoundRequestBuilder requestBuilder(String reqType, String url)
           
 AsyncHttpClient setSignatureCalculator(SignatureCalculator signatureCalculator)
          Set default signature calculator to use for requests build by this client instance
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

signatureCalculator

protected SignatureCalculator signatureCalculator
Default signature calculator to use for all requests constructed by this client instance.

Since:
1.1
Constructor Detail

AsyncHttpClient

public AsyncHttpClient()
Create a new HTTP Asynchronous Client using the default AsyncHttpClientConfig configuration. The default AsyncHttpProvider will be used (NettyAsyncHttpProvider


AsyncHttpClient

public AsyncHttpClient(AsyncHttpProvider provider)
Create a new HTTP Asynchronous Client using an implementation of AsyncHttpProvider and the default AsyncHttpClientConfig configuration.

Parameters:
provider - a AsyncHttpProvider

AsyncHttpClient

public AsyncHttpClient(AsyncHttpClientConfig config)
Create a new HTTP Asynchronous Client using a AsyncHttpClientConfig configuration and the DEFAULT_PROVIDER

Parameters:
config - a AsyncHttpClientConfig

AsyncHttpClient

public AsyncHttpClient(AsyncHttpProvider httpProvider,
                       AsyncHttpClientConfig config)
Create a new HTTP Asynchronous Client using a AsyncHttpClientConfig configuration and and a AsyncHttpProvider.

Parameters:
config - a AsyncHttpClientConfig
httpProvider - a AsyncHttpProvider

AsyncHttpClient

public AsyncHttpClient(String providerClass,
                       AsyncHttpClientConfig config)
Create a new HTTP Asynchronous Client using a AsyncHttpClientConfig configuration and and a AsyncHttpProvider class' name.

Parameters:
config - a AsyncHttpClientConfig
providerClass - a AsyncHttpProvider
Method Detail

getProvider

public AsyncHttpProvider getProvider()
Return the asynchronous AsyncHttpProvider

Returns:
an AsyncHttpProvider

close

public void close()
Close the underlying connections.


finalize

protected void finalize()
                 throws Throwable
Overrides:
finalize in class Object
Throws:
Throwable

isClosed

public boolean isClosed()
Return true if closed

Returns:
true if closed

getConfig

public AsyncHttpClientConfig getConfig()
Return the AsyncHttpClientConfig

Returns:
AsyncHttpClientConfig

setSignatureCalculator

public AsyncHttpClient setSignatureCalculator(SignatureCalculator signatureCalculator)
Set default signature calculator to use for requests build by this client instance


prepareGet

public AsyncHttpClient.BoundRequestBuilder prepareGet(String url)
Prepare an HTTP client GET request.

Parameters:
url - A well formed URL.
Returns:
AsyncHttpClient.BoundRequestBuilder

prepareConnect

public AsyncHttpClient.BoundRequestBuilder prepareConnect(String url)
Prepare an HTTP client CONNECT request.

Parameters:
url - A well formed URL.
Returns:
AsyncHttpClient.BoundRequestBuilder

prepareOptions

public AsyncHttpClient.BoundRequestBuilder prepareOptions(String url)
Prepare an HTTP client OPTIONS request.

Parameters:
url - A well formed URL.
Returns:
AsyncHttpClient.BoundRequestBuilder

prepareHead

public AsyncHttpClient.BoundRequestBuilder prepareHead(String url)
Prepare an HTTP client HEAD request.

Parameters:
url - A well formed URL.
Returns:
AsyncHttpClient.BoundRequestBuilder

preparePost

public AsyncHttpClient.BoundRequestBuilder preparePost(String url)
Prepare an HTTP client POST request.

Parameters:
url - A well formed URL.
Returns:
AsyncHttpClient.BoundRequestBuilder

preparePut

public AsyncHttpClient.BoundRequestBuilder preparePut(String url)
Prepare an HTTP client PUT request.

Parameters:
url - A well formed URL.
Returns:
AsyncHttpClient.BoundRequestBuilder

prepareDelete

public AsyncHttpClient.BoundRequestBuilder prepareDelete(String url)
Prepare an HTTP client DELETE request.

Parameters:
url - A well formed URL.
Returns:
AsyncHttpClient.BoundRequestBuilder

prepareRequest

public AsyncHttpClient.BoundRequestBuilder prepareRequest(Request request)
Construct a AsyncHttpClient.BoundRequestBuilder using a Request

Parameters:
request - a Request
Returns:
AsyncHttpClient.BoundRequestBuilder

executeRequest

public <T> ListenableFuture<T> executeRequest(Request request,
                                              AsyncHandler<T> handler)
                                   throws IOException
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 Future of type T
Throws:
IOException

executeRequest

public ListenableFuture<Response> executeRequest(Request request)
                                          throws IOException
Execute an HTTP request.

Parameters:
request - Request
Returns:
a Future of type Response
Throws:
IOException

requestBuilder

protected AsyncHttpClient.BoundRequestBuilder requestBuilder(String reqType,
                                                             String url)

requestBuilder

protected AsyncHttpClient.BoundRequestBuilder requestBuilder(Request prototype)


Copyright © 2011. All Rights Reserved.