T - the type of the value that will be made availablepublic class Promise<T> extends Composable<T> implements Supplier<T>
Promise is a stateful event processor that accepts a single value or error. In addition to getting or awaiting the value, consumers can be registered to be notified of notified an error, a value, or both.
A
promise also provides methods for composing actions with the future value much like a Stream. However, where
a Stream can process many values, a Promise processes only one value or error.
Reactor's Promise implementation is modeled largely after the Promises/A+
specification, which defines a number of methods and potential actions for promises.END_EVENTFLUSH_EVENT| Constructor and Description |
|---|
Promise(Observable observable,
Environment env,
Composable<?> parent)
Creates a new unfulfilled promise.
|
Promise(java.lang.Throwable error,
Observable observable,
Environment env)
Creates a new promise that has failed with the given
error. |
Promise(T value,
Observable observable,
Environment env)
Creates a new promise that has been fulfilled with the given
value. |
| Modifier and Type | Method and Description |
|---|---|
Promise<T> |
add(Action<T> operation)
Consume events with the passed
Action |
T |
await()
Block the calling thread, waiting for the completion of this
Promise. |
T |
await(long timeout,
java.util.concurrent.TimeUnit unit)
Block the calling thread for the specified time, waiting for the completion of this
Promise. |
Promise<T> |
connect(Composable<T> composable)
Attach another
Composable to this one that will cascade the value or error received by this Composable into the next. |
Promise<T> |
connectErrors(Composable<?> composable)
Forward any error to the argument.
|
Promise<T> |
consume(Consumer<T> consumer)
|
Promise<T> |
consume(java.lang.Object key,
Observable observable)
Pass values accepted by this
Composable into the given Observable, notifying with the given key. |
Promise<T> |
consumeFlush(Flushable<?> action)
Consume flush with the passed
Flushable |
protected void |
errorAccepted(java.lang.Throwable error) |
Promise<java.lang.Boolean> |
filter()
Evaluate each accepted boolean value.
|
Promise<java.lang.Boolean> |
filter(Composable<java.lang.Boolean> elseComposable)
Evaluate each accepted boolean value.
|
Promise<T> |
filter(Function<T,java.lang.Boolean> fn)
Evaluate each accepted value against the given predicate
Function. |
Promise<T> |
filter(Predicate<T> p)
Evaluate each accepted value against the given
Predicate. |
Promise<T> |
filter(Predicate<T> p,
Composable<T> elseComposable)
Evaluate each accepted value against the given
Predicate. |
Promise<T> |
flush()
Flush any cached or unprocessed values through this Stream.
|
T |
get()
Returns the value that completed this promise.
|
boolean |
isComplete()
Indicates whether this
Promise has been completed with either an error or a value |
boolean |
isError()
Indicates whether this
Promise has been completed with an error. |
boolean |
isPending()
Indicates whether this
Promise has yet to be completed with a value or an error. |
boolean |
isSuccess()
Indicates whether this
Promise has been successfully completed a value. |
<V> Promise<V> |
map(Function<T,V> fn)
Assign the given
Function to transform the incoming value T into a V and pass it into
another Composable. |
<V,C extends Composable<V>> |
mapMany(Function<T,C> fn)
Assign the given
Function to transform the incoming value T into a Composable<V> and pass
it into another Composable. |
Promise<T> |
merge(Composable<T>... composables)
this#connect(Composable) all the passed to this Composable,
merging values streams into the current pipeline. |
protected <V> Promise<V> |
newComposable()
Create a
Composable that is compatible with the subclass of Composable in use. |
Promise<T> |
onComplete(Consumer<Promise<T>> onComplete)
Assign a
Consumer that will either be invoked later, when the Promise is completed by either
setting a value or propagating an error, or, if this Promise has already been fulfilled, is immediately
scheduled to be executed on the current Dispatcher. |
Promise<T> |
onError(Consumer<java.lang.Throwable> onError)
Assign a
Consumer that will either be invoked later, when the Promise is completed with an error,
or, if this Promise has already been fulfilled, is immediately scheduled to be executed on the current
Dispatcher. |
Promise<T> |
onSuccess(Consumer<T> onSuccess)
Assign a
Consumer that will either be invoked later, when the Promise is successfully completed
with
a value, or, if this Promise has already been fulfilled, is immediately scheduled to be executed on the
current Dispatcher. |
Promise<T> |
propagate(Supplier<T> supplier)
Create a new
Composable whose values will be generated from . |
java.lang.Throwable |
reason()
Return the error (if any) that has completed this
Promise. |
Promise<T> |
then(Consumer<T> onSuccess,
Consumer<java.lang.Throwable> onError)
|
<V> Promise<V> |
then(Function<T,V> onSuccess,
Consumer<java.lang.Throwable> onError)
Assign a success
Function that will either be invoked later, when the Promise is successfully
completed with a value, or, if this Promise has already been fulfilled, the function is immediately
scheduled to be executed on the current Dispatcher. |
Promise<T> |
timeout(long timeout)
Flush the parent if any or the current composable otherwise when the last notification occurred before milliseconds.
|
Promise<T> |
timeout(long timeout,
Timer timer)
Flush the parent if any or the current composable otherwise when the last notification occurred before milliseconds.
|
java.lang.String |
toString() |
protected void |
valueAccepted(T value) |
<E extends java.lang.Throwable> |
when(java.lang.Class<E> exceptionType,
Consumer<E> onError)
Assign an error handler to exceptions of the given type.
|
connectValues, consumeErrorAndFlush, consumeEvent, debug, getAcceptKey, getAcceptSelector, getEnvironment, getError, getFlush, getObservable, getParentpublic Promise(@Nullable
Observable observable,
@Nullable
Environment env,
@Nullable
Composable<?> parent)
observable is used when notifying the Promise's consumers, determining the thread on which they are
called. The given env is used to determine the default await timeout. If env is null the
default await timeout will be 30 seconds. This Promise will consumer errors from its parent such that if
the parent completes in error then so too will this Promise.observable - The Observable to use to call Consumersenv - The Environment, if any, from which the default await timeout is obtainedparent - The parent, if any, from which errors are consumedpublic Promise(T value, @Nullable Observable observable, @Nullable Environment env)
value.
The observable is used when notifying the Promise's consumers. The given env is used to determine
the default await timeout. If env is null the default await timeout will be 30 seconds.value - The value that fulfills the promiseobservable - The Observable to use to call Consumersenv - The Environment, if any, from which the default await timeout is obtainedpublic Promise(java.lang.Throwable error,
@Nonnull
Observable observable,
@Nullable
Environment env)
error.
The observable is used when notifying the Promise's consumers, determining the thread on which they are
called. The given env is used to determine the default await timeout. If env is null the
default await timeout will be 30 seconds.error - The error the completed the promiseenv - The Environment, if any, from which the default await timeout is obtainedobservable - The Observable to use to call Consumerspublic Promise<T> onComplete(@Nonnull Consumer<Promise<T>> onComplete)
Consumer that will either be invoked later, when the Promise is completed by either
setting a value or propagating an error, or, if this Promise has already been fulfilled, is immediately
scheduled to be executed on the current Dispatcher.onComplete - the completion Consumerpublic Promise<T> onSuccess(@Nonnull Consumer<T> onSuccess)
Consumer that will either be invoked later, when the Promise is successfully completed
with
a value, or, if this Promise has already been fulfilled, is immediately scheduled to be executed on the
current Dispatcher.onSuccess - the success Consumerpublic Promise<T> onError(@Nullable Consumer<java.lang.Throwable> onError)
Consumer that will either be invoked later, when the Promise is completed with an error,
or, if this Promise has already been fulfilled, is immediately scheduled to be executed on the current
Dispatcher.onError - the error Consumerpublic Promise<T> then(@Nonnull Consumer<T> onSuccess, @Nullable Consumer<java.lang.Throwable> onError)
onSuccess - the success ConsumeronError - the error ConsumeronSuccess(Consumer),
onError(Consumer)public <V> Promise<V> then(@Nonnull Function<T,V> onSuccess, @Nullable Consumer<java.lang.Throwable> onError)
Function that will either be invoked later, when the Promise is successfully
completed with a value, or, if this Promise has already been fulfilled, the function is immediately
scheduled to be executed on the current Dispatcher.
A new Promise is returned that will be populated by result of the given transformation Function
that
turns the incoming T into a V.public boolean isComplete()
Promise has been completed with either an error or a valuetrue if this Promise is complete, false otherwise.isPending()public boolean isPending()
Promise has yet to be completed with a value or an error.true if this Promise is still pending, false otherwise.isComplete()public boolean isSuccess()
Promise has been successfully completed a value.true if this Promise is successful, false otherwise.public boolean isError()
Promise has been completed with an error.true if this Promise was completed with an error, false otherwise.public T await() throws java.lang.InterruptedException
Promise. A default timeout as specified in
Reactor's Environment properties using the key reactor.await.defaultTimeout is used. The default is
30 seconds. If the promise is completed with an error a RuntimeException that wraps the error is thrown.Promise or null if the timeout is reached and the Promise has not
completedjava.lang.InterruptedException - if the thread is interruped while awaiting completionjava.lang.RuntimeException - if the promise is completed with an errorpublic T await(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
Promise. If the promise
is completed with an error a RuntimeException that wraps the error is thrown.timeout - the timeout valueunit - the TimeUnit of the timeout valuePromise or null if the timeout is reached and the Promise has not
completedjava.lang.InterruptedException - if the thread is interruped while awaiting completionpublic T get()
null if the promise has not been completed. If the
promise is completed with an error a RuntimeException that wraps the error is thrown.public java.lang.Throwable reason()
Promise. Returns null if the promise has not been
completed, or was completed with a value.public Promise<T> consume(@Nonnull Consumer<T> consumer)
Composableconsume in class Composable<T>consumer - the conumer to invoke on each valuepublic Promise<T> connect(@Nonnull Composable<T> composable)
ComposableComposable to this one that will cascade the value or error received by this Composable into the next.connect in class Composable<T>composable - the next Composable to cascade events topublic Promise<T> consume(@Nonnull java.lang.Object key, @Nonnull Observable observable)
ComposableComposable into the given Observable, notifying with the given key.consume in class Composable<T>key - the key to notify onobservable - the Observable to notifypublic <V,C extends Composable<V>> Promise<V> mapMany(@Nonnull Function<T,C> fn)
ComposableFunction to transform the incoming value T into a Composable<V> and pass
it into another Composable.mapMany in class Composable<T>V - the type of the return value of the transformation functionfn - the transformation functionComposable containing the transformed valuespublic <E extends java.lang.Throwable> Promise<T> when(@Nonnull java.lang.Class<E> exceptionType, @Nonnull Consumer<E> onError)
Composablewhen in class Composable<T>E - type of the exception to handleexceptionType - the type of exceptions to handleonError - the error handler for each exceptionpublic <V> Promise<V> map(@Nonnull Function<T,V> fn)
ComposableFunction to transform the incoming value T into a V and pass it into
another Composable.map in class Composable<T>V - the type of the return value of the transformation functionfn - the transformation functionComposable containing the transformed valuespublic Promise<T> filter(@Nonnull Predicate<T> p)
ComposablePredicate. If the predicate test succeeds, the value is
passed into the new Composable. If the predicate test fails, the value is ignored.filter in class Composable<T>p - the Predicate to test values againstComposable containing only values that pass the predicate testpublic Promise<T> filter(@Nonnull Predicate<T> p, Composable<T> elseComposable)
ComposablePredicate. If the predicate test succeeds, the value is
passed into the new Composable. If the predicate test fails, the value is propagated into the elseComposable.filter in class Composable<T>p - the Predicate to test values againstelseComposable - the optional Composable to pass rejected valuesComposable containing only values that pass the predicate testpublic Promise<java.lang.Boolean> filter()
ComposableComposable. If the predicate test fails, the value is ignored.filter in class Composable<T>Composable containing only values that pass the predicate testpublic Promise<java.lang.Boolean> filter(@Nonnull Composable<java.lang.Boolean> elseComposable)
ComposableComposable. the value is propagated into the .filter in class Composable<T>elseComposable - the Composable to test values againstComposable containing only values that pass the predicate testpublic Promise<T> filter(@Nonnull Function<T,java.lang.Boolean> fn)
ComposableFunction. If the predicate test succeeds, the
value is passed into the new Composable. If the predicate test fails, an exception is propagated into the
new Composable.filter in class Composable<T>fn - the predicate Function to test values againstComposable containing only values that pass the predicate testpublic Promise<T> merge(Composable<T>... composables)
Composablethis#connect(Composable) all the passed to this Composable,
merging values streams into the current pipeline.merge in class Composable<T>composables - the the composables to connectpublic Promise<T> timeout(long timeout)
Composabletimeout in class Composable<T>timeout - the timeout in milliseconds between two notifications on this composableComposablepublic Promise<T> timeout(long timeout, Timer timer)
Composabletimeout in class Composable<T>timeout - the timeout in milliseconds between two notifications on this composabletimer - the reactor timer to run the timeout onComposablepublic Promise<T> propagate(Supplier<T> supplier)
ComposableComposable whose values will be generated from .
Every time flush is triggered, is called.propagate in class Composable<T>supplier - the supplier to drainComposable whose values are generated on each flushpublic Promise<T> flush()
Composablepublic Promise<T> add(Action<T> operation)
ComposableActionpublic Promise<T> consumeFlush(Flushable<?> action)
PipelineFlushableconsumeFlush in interface Pipeline<T>consumeFlush in class Composable<T>action - the action listening for flushpublic Promise<T> connectErrors(Composable<?> composable)
ComposableconnectErrors in class Composable<T>composable - the target sink for errores and flushesprotected <V> Promise<V> newComposable()
ComposableComposable that is compatible with the subclass of Composable in use.newComposable in class Composable<T>V - type the Composable handlesComposable compatible with the current subclass.protected void errorAccepted(java.lang.Throwable error)
protected void valueAccepted(T value)
public java.lang.String toString()
toString in class java.lang.Object