public class DeferredResult
extends java.lang.Object
DeferredResult is threadsafe and allows any number of handlers of any type to be added at any time. Handlers added
after the sendSuccess(Object) or sendFailure(Object), could be called immediately if condition is
met (success or failure) with the proper data.
Only first call to sendSuccess(Object) or sendFailure(Object) matters, more calls are silently
ignored.
| Modifier and Type | Class and Description |
|---|---|
static class |
DeferredResult.Error
Exception raised if operation is failed and
getResult() returns Throwable, which is passed as
exception cause (with stack trace this way). |
static class |
DeferredResult.Failure
Exception raised when operation is failed and
getResult() passes non-exception object, which is
available as DeferredResult.Failure.failureData. |
static interface |
DeferredResult.Handler
Callback to be executed on deferred result state changed
|
| Constructor and Description |
|---|
DeferredResult() |
| Modifier and Type | Method and Description |
|---|---|
<T> T |
await() |
<T> T |
await(long millis) |
DeferredResult |
done(DeferredResult.Handler handler)
Adds the callback that will be called when the operation is done, after success() or failure() respecive
invocation.
|
DeferredResult |
failure(DeferredResult.Handler handler)
Adds failure callback.
|
<T> T |
getResult()
Get the result passed with
sendFailure(Object) or sendSuccess(Object). |
boolean |
isDone()
Whether the deferred result is available already.
|
boolean |
isSuccess()
Whether the deferred result is available already and calculated successfully.
|
DeferredResult |
join()
Wait until the operation is somehow finished
|
boolean |
join(long millis)
wait until the operation is finished with maximum timeout
|
void |
sendFailure(java.lang.Object data)
Trigger the failure event and pass argument to all
failure(Handler) and to done(Handler)
callbacks. |
void |
sendSuccess(java.lang.Object data)
Trigger the success event and pass argument to all
success(Handler) and then done(Handler)
callbacks. |
DeferredResult |
success(DeferredResult.Handler handler)
Adds the success callback.
|
<T> T |
waitSuccess()
waits until the operation is finished and return it's result on success, or throws an error on failure.
|
public DeferredResult success(DeferredResult.Handler handler)
sendSuccess(Object)
call. Multiple callbacks allowed; calling in order of registration.
This handler is executed before done(Handler).
If called when isDone() and isSuccess() the callback is invoked immediately.
handler - callbackpublic DeferredResult failure(DeferredResult.Handler handler)
sendFailure(Object) call. Multiple
callbacks allowed; calling in order of registration.
This handler is executed before done(Handler).
If called when isDone() and !isSuccess() the callback is invoked immediately.
handler - RemoteError instance. callbackpublic DeferredResult done(DeferredResult.Handler handler)
isSuccess() value. Multiple callbacks
allowed; calling in order of registration.
If called when isDone() == true the callback is invoked immediately.
handler - callbackpublic void sendSuccess(java.lang.Object data)
success(Handler) and then done(Handler)
callbacks. Can be called only once if sendFailure(Object) was not called before. Further calls to this
method and to sendFailure(Object) are silently ignored.data - to pass to callbackspublic void sendFailure(java.lang.Object data)
failure(Handler) and to done(Handler)
callbacks. Can be called only once if sendSuccess(Object) was not called before. Further calls to this
method and to sendSuccess(Object) are silently ignored.data - to pass to callbackspublic boolean isDone()
public <T> T getResult()
sendFailure(Object) or sendSuccess(Object). Check the state using
isSuccess() call.
Do not call it when isDone() is false.
T - result value.sendSuccess(Object) or sendFailure(Object) call.public <T> T waitSuccess()
throws DeferredResult.Error,
java.lang.InterruptedException
java.lang.Error - or DeferredResult.Failure if operation failsDeferredResult.Errorjava.lang.InterruptedExceptionpublic <T> T await()
throws java.lang.InterruptedException
java.lang.InterruptedExceptionpublic <T> T await(long millis)
throws java.lang.InterruptedException
java.lang.InterruptedExceptionpublic final DeferredResult join() throws java.lang.InterruptedException
java.lang.InterruptedExceptionpublic boolean isSuccess()
public final boolean join(long millis)
throws java.lang.InterruptedException
millis - time to wait for operation to completejava.lang.InterruptedException