Class RestClient.FutureResponse

java.lang.Object
io.inversion.utils.RestClient.FutureResponse
All Implemented Interfaces:
Runnable, Future<Response>, RunnableFuture<Response>
Enclosing class:
RestClient

public abstract class RestClient.FutureResponse extends Object implements RunnableFuture<Response>
A RunnableFuture that blocks on get() until the execution of the Request has returned the Response.

Here are some example uses:


     client.get("/some/relative/path")
          .onSuccess(response -@gt; System.out.println("Success:" + res.toString()))
          .onFailure(response -@gt; System.out.println("Failure:" + res.toString()))
          .onResponse(response -@gt; System.out.println("I get called on success or failure: " + res.getStatus()));


      //-- instead of using the success/failure callbacks as above
      //-- you can wait for the async process to complete by calling 'get()'

      FutureResponse future = client.post("/some/relative/path", new JSNode("hello", "world"));

      //-- request is asynchronously executing now

      //-- the call to get() blocks indefinitely until the async execution completes
      //-- the fact that this method is called 'get()' is not related to HTTP get.

      Response response = future.get();


      //-- if you want to guarantee that your thread will not be indefinitely blocked
      //-- you can use get(long timeout, TimeUnit units) to wait no more than the specified time

      future = client.get("/some/other/path");
      response = future.get(100, TimeUnit.MILLISECONDS);

      if(response == null)
      {
           System.out.println("the http request still has not completed");
      }


 
  • Method Details

    • onSuccess

      public RestClient.FutureResponse onSuccess(Consumer<Response> handler)
      Registers a success callback.

      If the isDone() is already true the handler will be called synchronously right away.

      Parameters:
      handler - the listener to notify on success
      Returns:
      this
    • onFailure

      public RestClient.FutureResponse onFailure(Consumer<Response> handler)
      Registers a failure callback.

      If the isDone() is already true the handler will be called synchronously right away.

      Parameters:
      handler - the listener to notify on failure
      Returns:
      this
    • onResponse

      public RestClient.FutureResponse onResponse(Consumer<Response> handler)
      Registers a listener to be notified regardless of success or failure status.

      If the isDone() is already true the handler will be called synchronously right away.

      Parameters:
      handler - the listener to notify when the Response has arrived
      Returns:
      this
    • get

      public Response get()
      Blocks indefinitely until response is not null.
      Specified by:
      get in interface Future<Response>
      Returns:
      the response
    • get

      public Response get(long timeout, TimeUnit unit) throws TimeoutException
      Blocks until the arrival of the response just like get() but will return null after the specified timeout if the response has not arrived.
      Specified by:
      get in interface Future<Response>
      Returns:
      the response or null if the call has not asynchronously completed
      Throws:
      TimeoutException
    • isSuccess

      public boolean isSuccess()
      Returns:
      true if the response is not null and response.isSuccess()
    • getRequest

      public Request getRequest()
      Returns:
      the Request being run.
    • isCancelled

      public boolean isCancelled()
      Specified by:
      isCancelled in interface Future<Response>
      Returns:
      false
    • cancel

      public boolean cancel(boolean arg0)
      This does nothing.
      Specified by:
      cancel in interface Future<Response>
      Returns:
      false
    • isDone

      public boolean isDone()
      Specified by:
      isDone in interface Future<Response>
      Returns:
      true when response is not null.