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 do
        AsyncHttpClient 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 an AsyncHandler or its abstract implementation, AsyncCompletionHandler
           AsyncHttpClient 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();
     
    The AsyncCompletionHandler.onCompleted(Response) method will be invoked once the http response has been fully read. The Response object 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 the how the response is asynchronously processed by using an AsyncHandler
          AsyncHttpClient 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();
     
    You can asynchronously process the response status, headers and body and decide when to stop processing the response by returning a new AsyncHandler.State.ABORT at 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 DefaultAsyncHttpClientConfig instance.

          AsyncHttpClient c = new AsyncHttpClient(new AsyncHttpClientConfig.Builder().setRequestTimeoutInMs(...).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 the DefaultAsyncHttpClientConfig.getReadTimeout() expires. This object can hold many persistent connections to different hosts.