public interface Promise<V>
Future
with the following differences:
Future
and its derivatives to implement activities and workflow starting and querying code.
get()
doesn't throw InterruptedException. The only way to unblock get()
is to complete the Promise
CompletablePromise.completeExceptionally(RuntimeException)
are
not wrapped. It is possible as CompletablePromise.completeExceptionally(RuntimeException)
accepts only runtime
exceptions. So wrapping must be done by the caller of that method.
CancellationScope
to cancel and
handle cancellations. The pattern is that a canceled operation completes its Promise with
CanceledFailure
when canceled.
handle(Functions.Func2)
and similar callback operations do not allow blocking
calls inside functions
Modifier and Type | Method and Description |
---|---|
static <V> Promise<java.lang.Void> |
allOf(java.lang.Iterable<Promise<V>> promises)
Returns Promise that becomes completed when all promises in the collection are completed.
|
static Promise<java.lang.Void> |
allOf(Promise<?>... promises)
Returns Promise that becomes completed when all arguments are completed.
|
static <V> Promise<V> |
anyOf(java.lang.Iterable<Promise<V>> promises)
Returns Promise that becomes completed when any of the arguments is completed.
|
static Promise<java.lang.Object> |
anyOf(Promise<?>... promises)
Returns Promise that becomes completed when any of the arguments is completed.
|
V |
cancellableGet()
Waits if necessary for the computation to complete or fail, and then returns its result.
|
V |
cancellableGet(long timeout,
java.util.concurrent.TimeUnit unit)
Waits if necessary for at most the given time for the computation to complete, and then returns
its result, if available.
|
Promise<V> |
exceptionally(Functions.Func1<java.lang.Throwable,? extends V> fn)
Returns a new Promise that, when this promise completes exceptionally, is executed with this
promise's exception as the argument to the supplied function.
|
V |
get()
Waits if necessary for the computation to complete or fail, and then returns its result.
|
V |
get(long timeout,
java.util.concurrent.TimeUnit unit)
Waits if necessary for at most the given time for the computation to complete, and then returns
its result, if available.
|
java.lang.RuntimeException |
getFailure()
Waits if necessary for the computation to complete or fail, and then returns the failure or
null.
|
<U> Promise<U> |
handle(Functions.Func2<? super V,java.lang.RuntimeException,? extends U> fn)
Returns Promise that contains a result of a function.
|
boolean |
isCompleted()
Returns
true if this promise is completed. |
<U> Promise<U> |
thenApply(Functions.Func1<? super V,? extends U> fn)
Returns Promise that contains a result of a function.
|
<U> Promise<U> |
thenCompose(Functions.Func1<? super V,? extends Promise<U>> fn)
Returns a new Promise that, when this promise completes normally, is executed with this promise
as the argument to the supplied function.
|
boolean isCompleted()
true
if this promise is completed.
Completion may be due to normal termination, an exception, or cancellation -- in all of
these cases, this method will return true
.
true
if this promise completedV get()
java.lang.RuntimeException
- if the computation failed.V cancellableGet()
CanceledFailure
without waiting for this Promise to become
ready. Note that in the most situations it is better to let the operation that returned a
Promise to perform cleanup and then complete the promise with CanceledException. So calling
get()
on an asynchronous activity or child workflow invocation result is preferable.java.lang.RuntimeException
- if the computation failed.CanceledFailure
- if surrounding @CancellationScope
is canceled.V get(long timeout, java.util.concurrent.TimeUnit unit) throws java.util.concurrent.TimeoutException
timeout
- the maximum time to waitunit
- the time unit of the timeout argumentjava.lang.RuntimeException
- if the computation failed.java.util.concurrent.TimeoutException
- if the wait timed outV cancellableGet(long timeout, java.util.concurrent.TimeUnit unit) throws java.util.concurrent.TimeoutException
CanceledFailure
without waiting
for this Promise to become ready. Note that in the most situations it is better to let the
operation that returned a Promise to perform cleanup and then complete the promise with
CanceledException. So calling get(long, TimeUnit)
on an asynchronous activity or child
workflow invocation result is preferable.timeout
- the maximum time to waitunit
- the time unit of the timeout argumentjava.lang.RuntimeException
- if the computation failed.java.util.concurrent.TimeoutException
- if the wait timed outCanceledFailure
- if surrounding @CancellationScope
is canceled.java.lang.RuntimeException getFailure()
<U> Promise<U> thenApply(Functions.Func1<? super V,? extends U> fn)
Note that no blocking calls are allowed inside of the function.
<U> Promise<U> handle(Functions.Func2<? super V,java.lang.RuntimeException,? extends U> fn)
RuntimeException
it fails the resulting promise.
Note that no blocking calls are allowed inside of the function.
<U> Promise<U> thenCompose(Functions.Func1<? super V,? extends Promise<U>> fn)
U
- the type of the returned CompletionStage's resultfn
- the function returning a new PromisePromise<V> exceptionally(Functions.Func1<java.lang.Throwable,? extends V> fn)
fn
- the function to use to compute the value of the returned CompletionPromise if this
CompletionPromise completed exceptionallystatic <V> Promise<java.lang.Void> allOf(java.lang.Iterable<Promise<V>> promises)
promises
- promises to wait for.static Promise<java.lang.Void> allOf(Promise<?>... promises)
static <V> Promise<V> anyOf(java.lang.Iterable<Promise<V>> promises)