Class ResponseFuture

java.lang.Object
com.mastfrog.netty.http.client.ResponseFuture
All Implemented Interfaces:
Comparable<ResponseFuture>

public final class ResponseFuture extends Object implements Comparable<ResponseFuture>
Returned from launching an HTTP request; attach handlers using the on(Class<EventType>, Receiver<State<T>>)) method. Note that it is preferable to attach handlers when constructing the request, unless you can guarantee that the request won't be completed before your handler is attached.
Author:
Tim Boudreau
  • Method Details

    • sendOn

      public ResponseFuture sendOn(StateType stateType, Object o)
      Send some objects through the channel once a given state is reached (if the state already has been, they will be sent immediately if the channel is not closed).
      Parameters:
      stateType - The state to trigger sending. Must NOT be a state that indicates a closed channel, error or failure condition, or pre-connection state, because there is no channel to use.
      o - What to send - must be an object Netty's channel pipeline has a handler for it, such as an HttpContent or ByteBuf or WebSocketFrame (the main use for this method)
      Returns:
      this
      Throws:
      IllegalArgumentException - if the state type is one that has no associated network channel
    • await

      public ResponseFuture await() throws InterruptedException
      Wait for the channel to be closed. Dangerous without a timeout!

      Note - blocking while waiting for a response defeats the purpose of using an asynchronous HTTP client; this sort of thing is sometimes useful in unit tests, but should not be done in production code. Where possible, find a way to attach a callback and finish work there, rather than use this method.

      Throws:
      InterruptedException
    • await

      public ResponseFuture await(long l, TimeUnit tu) throws InterruptedException
      Wait for a timeout for the request to be complleted. This is realy for use in unit tests - normal users of this library should use callbacks.

      Note - blocking while waiting for a response defeats the purpose of using an asynchronous HTTP client; this sort of thing is sometimes useful in unit tests, but should not be done in production code. Where possible, find a way to attach a callback and finish work there, rather than use this method.

      Parameters:
      l - A number of time units
      tu - Time units
      Returns:
      follows the contract of CountDownLatch.await()
      Throws:
      InterruptedException
    • cancel

      public boolean cancel()
      Cancel the associated request. This will make a best-effort, but cannot guarantee, that no state changes will be fired after the final Cancelled.
      Returns:
      true if it succeeded, false if it was already canceled
    • throwIfError

      public ResponseFuture throwIfError() throws Throwable
      If an error was encountered, throw it
      Returns:
      this
      Throws:
      Throwable - a throwable
    • lastState

      public final StateType lastState()
    • onAnyEvent

      public ResponseFuture onAnyEvent(Receiver<State<?>> r)
      Add a listener which is notified of all state changes (there are many!).
      Parameters:
      r -
      Returns:
    • on

      public <T> ResponseFuture on(StateType state, Receiver<T> receiver)
      Add a listener for a particular type of event
    • on

      public <T> ResponseFuture on(Class<? extends State<T>> state, Receiver<T> receiver)
    • compareTo

      public int compareTo(ResponseFuture t)
      Specified by:
      compareTo in interface Comparable<ResponseFuture>