Interface AsyncHandler<T>

Type Parameters:
T - Type of object returned by the Future.get()
All Known Subinterfaces:
ProgressAsyncHandler<T>
All Known Implementing Classes:
AsyncCompletionHandler, AsyncCompletionHandlerBase, BodyDeferringAsyncHandler, ResumableAsyncHandler, TransferCompletionHandler, WebDavCompletionHandlerBase, WebSocketUpgradeHandler

public interface AsyncHandler<T>
An asynchronous handler or callback which gets invoked as soon as some data is available when processing an asynchronous response.
Callback methods get invoked in the following order:
  1. onStatusReceived(HttpResponseStatus),
  2. onHeadersReceived(HttpResponseHeaders),
  3. onBodyPartReceived(HttpResponseBodyPart), which could be invoked multiple times,
  4. onCompleted(), once the response has been fully read.

Returning a AsyncHandler.STATE.ABORT from any of those callback methods will interrupt asynchronous response processing, after that only onCompleted() is going to be called.

AsyncHandler aren't thread safe, hence you should avoid re-using the same instance when doing concurrent requests. As an exmaple, the following may produce unexpected results:

   AsyncHandler ah = new AsyncHandler() {....};
   AsyncHttpClient client = new AsyncHttpClient();
   client.prepareGet("http://...").execute(ah);
   client.prepareGet("http://...").execute(ah);
 
It is recommended to create a new instance instead.
  • Method Details

    • onThrowable

      void onThrowable(Throwable t)
      Invoked when an unexpected exception occurs during the processing of the response. The exception may have been produced by implementation of onXXXReceived method invocation.
      Parameters:
      t - a Throwable
    • onBodyPartReceived

      AsyncHandler.STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception
      Invoked as soon as some response body part are received. Could be invoked many times.
      Parameters:
      bodyPart - response's body part.
      Returns:
      a AsyncHandler.STATE telling to CONTINUE or ABORT the current processing.
      Throws:
      Exception - if something wrong happens
    • onStatusReceived

      AsyncHandler.STATE onStatusReceived(HttpResponseStatus responseStatus) throws Exception
      Invoked as soon as the HTTP status line has been received
      Parameters:
      responseStatus - the status code and test of the response
      Returns:
      a AsyncHandler.STATE telling to CONTINUE or ABORT the current processing.
      Throws:
      Exception - if something wrong happens
    • onHeadersReceived

      AsyncHandler.STATE onHeadersReceived(HttpResponseHeaders headers) throws Exception
      Invoked as soon as the HTTP headers has been received. Can potentially be invoked more than once if a broken server sent trailing headers.
      Parameters:
      headers - the HTTP headers.
      Returns:
      a AsyncHandler.STATE telling to CONTINUE or ABORT the current processing.
      Throws:
      Exception - if something wrong happens
    • onCompleted

      T onCompleted() throws Exception
      Invoked once the HTTP response processing is finished.

      Gets always invoked as last callback method.

      Returns:
      T Value that will be returned by the associated Future
      Throws:
      Exception - if something wrong happens