O - the type of the value that will be made availablepublic class Promise<O>
extends java.lang.Object
implements reactor.fn.Supplier<O>, org.reactivestreams.Processor<O,O>, reactor.fn.Consumer<O>, reactor.core.support.NonBlocking
Promise is a stateful event container that accepts a single value or error. In addition to getting or awaiting the value, consumers can be registered to the outbound stream() or via
, 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.
| Modifier and Type | Class and Description |
|---|---|
static class |
Promise.FinalState |
| Modifier and Type | Field and Description |
|---|---|
protected org.reactivestreams.Subscription |
subscription |
| Constructor and Description |
|---|
Promise()
Creates a new unfulfilled promise.
|
Promise(reactor.core.Dispatcher dispatcher,
reactor.Environment env)
Creates a new unfulfilled promise.
|
Promise(O value,
reactor.core.Dispatcher dispatcher,
reactor.Environment env)
Creates a new promise that has been fulfilled with the given
value. |
Promise(java.lang.Throwable error,
reactor.core.Dispatcher dispatcher,
reactor.Environment env)
Creates a new promise that has failed with the given
error. |
| Modifier and Type | Method and Description |
|---|---|
void |
accept(O o) |
Promise<java.lang.Void> |
after()
Only forward onError and onComplete signals into the returned stream.
|
O |
await()
Block the calling thread, waiting for the completion of this
Promise. |
O |
await(long timeout,
java.util.concurrent.TimeUnit unit)
Block the calling thread for the specified time, waiting for the completion of this
Promise. |
boolean |
awaitSuccess()
Block the calling thread, waiting for the completion of this
Promise. |
boolean |
awaitSuccess(long timeout,
java.util.concurrent.TimeUnit unit)
Block the calling thread for the specified time, waiting for the completion of this
Promise. |
protected void |
completeAccepted() |
StreamUtils.StreamVisitor |
debug() |
protected void |
errorAccepted(java.lang.Throwable error) |
Action<?,?> |
findOldestStream() |
<V> Promise<V> |
flatMap(reactor.fn.Function<? super O,? extends org.reactivestreams.Publisher<? extends V>> transformation)
Assign a
Function 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. |
O |
get()
Returns the value that completed this promise.
|
long |
getCapacity() |
reactor.Environment |
getEnvironment() |
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 |
isReactivePull(reactor.core.Dispatcher dispatcher,
long producerCapacity) |
boolean |
isSuccess()
Indicates whether this
Promise has been successfully completed a value. |
<V> Promise<V> |
map(reactor.fn.Function<? super O,V> transformation)
Assign a
Function 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. |
void |
onComplete() |
Promise<O> |
onComplete(reactor.fn.Consumer<Promise<O>> 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<O> |
onError(reactor.fn.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. |
void |
onError(java.lang.Throwable cause) |
void |
onNext(O element) |
void |
onSubscribe(org.reactivestreams.Subscription subscription) |
Promise<O> |
onSuccess(reactor.fn.Consumer<O> 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. |
O |
poll()
Block the calling thread, waiting for the completion of this
Promise. |
O |
poll(long timeout,
java.util.concurrent.TimeUnit unit)
Block the calling thread for the specified time, waiting for the completion of this
Promise. |
java.lang.Throwable |
reason()
Return the error (if any) that has completed this
Promise. |
Stream<O> |
stream() |
void |
subscribe(org.reactivestreams.Subscriber<? super O> subscriber) |
java.lang.String |
toString() |
protected void |
valueAccepted(O value) |
public Promise()
The dispatcher 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. 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.
public Promise(reactor.core.Dispatcher dispatcher,
@Nullable
reactor.Environment env)
The dispatcher 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.
dispatcher - The Dispatcher to run any downstream subscribersenv - The Environment, if any, from which the default await timeout is obtainedpublic Promise(O value, reactor.core.Dispatcher dispatcher, @Nullable reactor.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 promisedispatcher - The Dispatcher to run any downstream subscribersenv - The Environment, if any, from which the default await timeout is obtainedpublic Promise(java.lang.Throwable error,
reactor.core.Dispatcher dispatcher,
@Nullable
reactor.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 promisedispatcher - The Dispatcher to run any downstream subscribersenv - The Environment, if any, from which the default await timeout is obtainedpublic Promise<O> onComplete(@Nonnull reactor.fn.Consumer<Promise<O>> 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 final Promise<java.lang.Void> after()
public Promise<O> onSuccess(@Nonnull reactor.fn.Consumer<O> 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 <V> Promise<V> map(@Nonnull reactor.fn.Function<? super O,V> transformation)
Function 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.transformation - the function to apply on signal to the transformed Promisepublic <V> Promise<V> flatMap(@Nonnull reactor.fn.Function<? super O,? extends org.reactivestreams.Publisher<? extends V>> transformation)
Function 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.
FlatMap is typically used to listen for a delayed/async publisher, e.g. promise.flatMap( data -> Promise.success (data) ). The result is merged directly on the returned stream.
transformation - the function to apply on signal to the supplied Promise that will be merged back.public Promise<O> onError(@Nonnull reactor.fn.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. The error is recovered and materialized as the next signal to the returned stream.onError - the error Consumerpublic 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 boolean awaitSuccess()
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.java.lang.InterruptedException - if the thread is interruped while awaiting completionjava.lang.RuntimeException - if the promise is completed with an errorpublic boolean awaitSuccess(long timeout,
java.util.concurrent.TimeUnit unit)
throws java.lang.InterruptedException
Promise.timeout - the timeout valueunit - the TimeUnit of the timeout valuejava.lang.InterruptedException - if the thread is interruped while awaiting completionpublic O 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 O await(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
Promise.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 O poll()
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.RuntimeException - if the promise is completed with an errorpublic O poll(long timeout, java.util.concurrent.TimeUnit unit)
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
completedpublic O 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.get in interface reactor.fn.Supplier<O>null if it has not been completedjava.lang.RuntimeException - if the promise was completed with an errorpublic java.lang.Throwable reason()
Promise. Returns null if the promise has not
been
completed, or was completed with a value.public void subscribe(org.reactivestreams.Subscriber<? super O> subscriber)
subscribe in interface org.reactivestreams.Publisher<O>public reactor.Environment getEnvironment()
public void onSubscribe(org.reactivestreams.Subscription subscription)
onSubscribe in interface org.reactivestreams.Subscriber<O>public void onNext(O element)
onNext in interface org.reactivestreams.Subscriber<O>public void onComplete()
onComplete in interface org.reactivestreams.Subscriber<O>public void onError(java.lang.Throwable cause)
onError in interface org.reactivestreams.Subscriber<O>public StreamUtils.StreamVisitor debug()
public Action<?,?> findOldestStream()
protected void errorAccepted(java.lang.Throwable error)
protected void valueAccepted(O value)
protected void completeAccepted()
public boolean isReactivePull(reactor.core.Dispatcher dispatcher,
long producerCapacity)
isReactivePull in interface reactor.core.support.NonBlockingpublic long getCapacity()
getCapacity in interface reactor.core.support.NonBlockingpublic java.lang.String toString()
toString in class java.lang.Object