Package io.inversion.utils
Class RestClient.FutureResponse
java.lang.Object
io.inversion.utils.RestClient.FutureResponse
- All Implemented Interfaces:
Runnable,Future<Response>,RunnableFuture<Response>
- Enclosing class:
RestClient
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");
}
-
Nested Class Summary
Nested classes/interfaces inherited from interface java.util.concurrent.Future
Future.State -
Method Summary
Modifier and TypeMethodDescriptionbooleancancel(boolean arg0) This does nothing.get()Blocks indefinitely untilresponseis not null.Blocks until the arrival of the response just like get() but will return null after the specified timeout if the response has not arrived.booleanbooleanisDone()booleanRegisters a failure callback.onResponse(Consumer<Response> handler) Registers a listener to be notified regardless of success or failure status.Registers a success callback.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.concurrent.Future
exceptionNow, resultNow, stateMethods inherited from interface java.util.concurrent.RunnableFuture
run
-
Method Details
-
onSuccess
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
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
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
Blocks indefinitely untilresponseis not null. -
get
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:
getin interfaceFuture<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
- Returns:
- the Request being run.
-
isCancelled
public boolean isCancelled()- Specified by:
isCancelledin interfaceFuture<Response>- Returns:
- false
-
cancel
public boolean cancel(boolean arg0) This does nothing. -
isDone
public boolean isDone()
-