O
- the type of the value that will be made availablepublic class Promise<O> extends Object implements Supplier<O>, org.reactivestreams.Processor<O,O>, Consumer<O>, 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(Dispatcher dispatcher,
Environment env)
Creates a new unfulfilled promise.
|
Promise(O value,
Dispatcher dispatcher,
Environment env)
Creates a new promise that has been fulfilled with the given
value . |
Promise(Throwable error,
Dispatcher dispatcher,
Environment env)
Creates a new promise that has failed with the given
error . |
Modifier and Type | Method and Description |
---|---|
void |
accept(O o)
Execute the logic of the action, accepting the given parameter.
|
Promise<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,
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,
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(Throwable error) |
Action<?,?> |
findOldestStream() |
<V> Promise<V> |
flatMap(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()
Return defined element capacity, used to drive new
Subscription
request needs. |
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(Dispatcher dispatcher,
long producerCapacity)
Get the assigned
Dispatcher . |
boolean |
isSuccess()
Indicates whether this
Promise has been successfully completed a value. |
<V> Promise<V> |
map(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(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(Consumer<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(Throwable cause) |
void |
onNext(O element) |
void |
onSubscribe(org.reactivestreams.Subscription subscription) |
Promise<O> |
onSuccess(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,
TimeUnit unit)
Block the calling thread for the specified time, waiting for the completion of this
Promise . |
Throwable |
reason()
Return the error (if any) that has completed this
Promise . |
Stream<O> |
stream() |
void |
subscribe(org.reactivestreams.Subscriber<? super O> subscriber) |
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(Dispatcher dispatcher, @Nullable 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, Dispatcher dispatcher, @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 promisedispatcher
- The Dispatcher to run any downstream subscribersenv
- The Environment, if any, from which the default await timeout is obtainedpublic Promise(Throwable error, Dispatcher dispatcher, @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 promisedispatcher
- The Dispatcher to run any downstream subscribersenv
- The Environment, if any, from which the default await timeout is obtainedpublic Promise<O> onComplete(@Nonnull 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 Consumer
public final Promise<Void> after()
public Promise<O> onSuccess(@Nonnull 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 Consumer
public <V> Promise<V> map(@Nonnull 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 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 Consumer<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 Consumer
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 boolean awaitSuccess() throws 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.InterruptedException
- if the thread is interruped while awaiting completionRuntimeException
- if the promise is completed with an errorpublic boolean awaitSuccess(long timeout, TimeUnit unit) throws InterruptedException
Promise
.timeout
- the timeout valueunit
- the TimeUnit
of the timeout valueInterruptedException
- if the thread is interruped while awaiting completionpublic O await() throws 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
completedInterruptedException
- if the thread is interruped while awaiting completionRuntimeException
- if the promise is completed with an errorpublic O await(long timeout, TimeUnit unit) throws InterruptedException
Promise
.timeout
- the timeout valueunit
- the TimeUnit
of the timeout valuePromise
or null
if the timeout is reached and the Promise
has
not
completedInterruptedException
- 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
completedRuntimeException
- if the promise is completed with an errorpublic O poll(long timeout, 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 Supplier<O>
null
if it has not been completedRuntimeException
- if the promise was completed with an errorpublic 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 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(Throwable cause)
onError
in interface org.reactivestreams.Subscriber<O>
public void accept(O o)
Consumer
public StreamUtils.StreamVisitor debug()
public Action<?,?> findOldestStream()
protected void errorAccepted(Throwable error)
protected void valueAccepted(O value)
protected void completeAccepted()
public boolean isReactivePull(Dispatcher dispatcher, long producerCapacity)
NonBlocking
Dispatcher
.isReactivePull
in interface NonBlocking
public long getCapacity()
NonBlocking
Subscription
request needs. This is the maximum in-flight data allowed to transit to this elements.getCapacity
in interface NonBlocking
Copyright © 2017. All rights reserved.