Class RequestFuture<T>

java.lang.Object
org.apache.kafka.clients.consumer.internals.RequestFuture<T>
Type Parameters:
T - Return type of the result (Can be Void if there is no response)
All Implemented Interfaces:
ConsumerNetworkClient.PollCondition

public class RequestFuture<T> extends Object implements ConsumerNetworkClient.PollCondition
Result of an asynchronous request from ConsumerNetworkClient. Use ConsumerNetworkClient.poll(Timer) (and variants) to finish a request future. Use isDone() to check if the future is complete, and succeeded() to check if the request completed successfully. Typical usage might look like this:
     RequestFuture future = client.send(api, request);
     client.poll(future);

     if (future.succeeded()) {
         ClientResponse response = future.value();
         // Handle response
     } else {
         throw future.exception();
     }
 
  • Constructor Details

    • RequestFuture

      public RequestFuture()
  • Method Details

    • isDone

      public boolean isDone()
      Check whether the response is ready to be handled
      Returns:
      true if the response is ready, false otherwise
    • awaitDone

      public boolean awaitDone(long timeout, TimeUnit unit) throws InterruptedException
      Throws:
      InterruptedException
    • value

      public T value()
      Get the value corresponding to this request (only available if the request succeeded)
      Returns:
      the value set in complete(Object)
      Throws:
      IllegalStateException - if the future is not complete or failed
    • succeeded

      public boolean succeeded()
      Check if the request succeeded;
      Returns:
      true if the request completed and was successful
    • failed

      public boolean failed()
      Check if the request failed.
      Returns:
      true if the request completed with a failure
    • isRetriable

      public boolean isRetriable()
      Check if the request is retriable (convenience method for checking if the exception is an instance of RetriableException.
      Returns:
      true if it is retriable, false otherwise
      Throws:
      IllegalStateException - if the future is not complete or completed successfully
    • exception

      public RuntimeException exception()
      Get the exception from a failed result (only available if the request failed)
      Returns:
      the exception set in raise(RuntimeException)
      Throws:
      IllegalStateException - if the future is not complete or completed successfully
    • complete

      public void complete(T value)
      Complete the request successfully. After this call, succeeded() will return true and the value can be obtained through value().
      Parameters:
      value - corresponding value (or null if there is none)
      Throws:
      IllegalStateException - if the future has already been completed
      IllegalArgumentException - if the argument is an instance of RuntimeException
    • raise

      public void raise(RuntimeException e)
      Raise an exception. The request will be marked as failed, and the caller can either handle the exception or throw it.
      Parameters:
      e - corresponding exception to be passed to caller
      Throws:
      IllegalStateException - if the future has already been completed
    • raise

      public void raise(org.apache.kafka.common.protocol.Errors error)
      Raise an error. The request will be marked as failed.
      Parameters:
      error - corresponding error to be passed to caller
    • addListener

      public void addListener(RequestFutureListener<T> listener)
      Add a listener which will be notified when the future completes
      Parameters:
      listener - non-null listener to add
    • compose

      public <S> RequestFuture<S> compose(RequestFutureAdapter<T,S> adapter)
      Convert from a request future of one type to another type
      Type Parameters:
      S - The type of the future adapted to
      Parameters:
      adapter - The adapter which does the conversion
      Returns:
      The new future
    • chain

      public void chain(RequestFuture<T> future)
    • failure

      public static <T> RequestFuture<T> failure(RuntimeException e)
    • voidSuccess

      public static RequestFuture<Void> voidSuccess()
    • coordinatorNotAvailable

      public static <T> RequestFuture<T> coordinatorNotAvailable()
    • noBrokersAvailable

      public static <T> RequestFuture<T> noBrokersAvailable()
    • shouldBlock

      public boolean shouldBlock()
      Description copied from interface: ConsumerNetworkClient.PollCondition
      Return whether the caller is still awaiting an IO event.
      Specified by:
      shouldBlock in interface ConsumerNetworkClient.PollCondition
      Returns:
      true if so, false otherwise.