public abstract class RestClient.FutureResponse extends java.lang.Object implements java.util.concurrent.RunnableFuture<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");
}
| Modifier and Type | Method and Description |
|---|---|
boolean |
cancel(boolean arg0)
This does nothing.
|
Response |
get()
Blocks indefinitely until
response is not null. |
Response |
get(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.
|
Request |
getRequest() |
boolean |
isCancelled() |
boolean |
isDone() |
boolean |
isSuccess() |
RestClient.FutureResponse |
onFailure(java.util.function.Consumer<Response> handler)
Registers a failure callback.
|
RestClient.FutureResponse |
onResponse(java.util.function.Consumer<Response> handler)
Registers a listener to be notified regardless of success or failure status.
|
RestClient.FutureResponse |
onSuccess(java.util.function.Consumer<Response> handler)
Registers a success callback.
|
public RestClient.FutureResponse onSuccess(java.util.function.Consumer<Response> handler)
If the isDone() is already true the handler will be called synchronously right away.
handler - the listener to notify on successpublic RestClient.FutureResponse onFailure(java.util.function.Consumer<Response> handler)
If the isDone() is already true the handler will be called synchronously right away.
handler - the listener to notify on failurepublic RestClient.FutureResponse onResponse(java.util.function.Consumer<Response> handler)
If the isDone() is already true the handler will be called synchronously right away.
handler - the listener to notify when the Response has arrivedpublic Response get()
response is not null.get in interface java.util.concurrent.Future<Response>public Response get(long timeout, java.util.concurrent.TimeUnit unit) throws java.util.concurrent.TimeoutException
get in interface java.util.concurrent.Future<Response>java.util.concurrent.TimeoutExceptionpublic boolean isSuccess()
public Request getRequest()
public boolean isCancelled()
isCancelled in interface java.util.concurrent.Future<Response>public boolean cancel(boolean arg0)
cancel in interface java.util.concurrent.Future<Response>public boolean isDone()
isDone in interface java.util.concurrent.Future<Response>Copyright © 2021 Rocket Partners, LLC. All rights reserved.