Package io.inversion.client
Class ApiClient.FutureResponse
- java.lang.Object
-
- io.inversion.client.ApiClient.FutureResponse
-
- All Implemented Interfaces:
java.lang.Runnable,java.util.concurrent.Future<io.inversion.Response>,java.util.concurrent.RunnableFuture<io.inversion.Response>
- Enclosing class:
- ApiClient
public abstract class ApiClient.FutureResponse extends java.lang.Object implements java.util.concurrent.RunnableFuture<io.inversion.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 JSMap("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 Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancancel(boolean arg0)This does nothing.io.inversion.Responseget()Blocks indefinitely untilresponseis not null.io.inversion.Responseget(long timeout, java.util.concurrent.TimeUnit unit)Blocks until the arrival of the response just like get() but will return null after the specified timeout if the response has not arrived.io.inversion.RequestgetRequest()booleanisCancelled()booleanisDone()booleanisSuccess()ApiClient.FutureResponseonFailure(java.util.function.Consumer<io.inversion.Response> handler)Registers a failure callback.ApiClient.FutureResponseonResponse(java.util.function.Consumer<io.inversion.Response> handler)Registers a listener to be notified regardless of success or failure status.ApiClient.FutureResponseonSuccess(java.util.function.Consumer<io.inversion.Response> handler)Registers a success callback.
-
-
-
Method Detail
-
onSuccess
public ApiClient.FutureResponse onSuccess(java.util.function.Consumer<io.inversion.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 ApiClient.FutureResponse onFailure(java.util.function.Consumer<io.inversion.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 ApiClient.FutureResponse onResponse(java.util.function.Consumer<io.inversion.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 io.inversion.Response get()
Blocks indefinitely untilresponseis not null.- Specified by:
getin interfacejava.util.concurrent.Future<io.inversion.Response>- Returns:
- the response
-
get
public io.inversion.Response get(long timeout, java.util.concurrent.TimeUnit unit) throws java.util.concurrent.TimeoutExceptionBlocks 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:
getin interfacejava.util.concurrent.Future<io.inversion.Response>- Returns:
- the response or null if the call has not asynchronously completed
- Throws:
java.util.concurrent.TimeoutException
-
isSuccess
public boolean isSuccess()
- Returns:
- true if the response is not null and response.isSuccess()
-
getRequest
public io.inversion.Request getRequest()
- Returns:
- the Request being run.
-
isCancelled
public boolean isCancelled()
- Specified by:
isCancelledin interfacejava.util.concurrent.Future<io.inversion.Response>- Returns:
- false
-
cancel
public boolean cancel(boolean arg0)
This does nothing.- Specified by:
cancelin interfacejava.util.concurrent.Future<io.inversion.Response>- Returns:
- false
-
isDone
public boolean isDone()
- Specified by:
isDonein interfacejava.util.concurrent.Future<io.inversion.Response>- Returns:
- true when response is not null.
-
-