Interface StreamMessage<T>
- Type Parameters:
T
- the type of element signaled
- All Superinterfaces:
org.reactivestreams.Publisher<T>
- All Known Subinterfaces:
HttpRequest
,HttpRequestWriter
,HttpResponse
,HttpResponseWriter
- All Known Implementing Classes:
DefaultStreamMessage
,DeferredStreamMessage
,EmptyFixedStreamMessage
,FilteredHttpRequest
,FilteredHttpResponse
,FilteredStreamMessage
,OneElementFixedStreamMessage
,PublisherBasedStreamMessage
,RegularFixedStreamMessage
,StreamMessageWrapper
,TwoElementFixedStreamMessage
public interface StreamMessage<T>
extends org.reactivestreams.Publisher<T>
Publisher
, which allows
only one Subscriber
. Unlike a usual Publisher
, a StreamMessage
can stream itself
only once. It has the following additional operations on top of what the Reactive Streams API provides:
When is a StreamMessage
fully consumed?
A StreamMessage
is complete (or 'fully consumed') when:
- the
Subscriber
consumes all elements andSubscriber.onComplete()
is invoked, - an error occurred and
Subscriber.onError(Throwable)
is invoked, - the
Subscription
has been cancelled or abort()
has been requested.
When fully consumed, the CompletableFuture
returned by whenComplete()
will complete, which you may find useful because Subscriber
does not notify you when a stream is
cancelled.
Publication and Consumption of pooled HttpData
objects
StreamMessage
will discard the publication request of a pooled HttpData
silently and
release it automatically when the publication is attempted after the stream is closed.
For pooled HttpData
, StreamMessage
will convert them into its unpooled version that
never leak, so that the Subscriber
does not need to worry about leaks.
If a Subscriber
does not want a StreamMessage
to make a copy of a pooled HttpData
,
specify SubscriptionOption.WITH_POOLED_OBJECTS
when you subscribe. Note that the Subscriber
is responsible for releasing the objects given with Subscriber.onNext(Object)
.
Subscriber.onError(Throwable)
is invoked when any exception is raised except the
CancelledSubscriptionException
which is caused by Subscription.cancel()
. If you want your
Subscriber
get notified by Subscriber.onError(Throwable)
when Subscription.cancel()
is called, specify SubscriptionOption.NOTIFY_CANCELLATION
when you subscribe.
-
Method Summary
Modifier and Type Method Description void
abort()
Closes this stream withAbortedStreamException
and prevents further subscription.void
abort(Throwable cause)
Closes this stream with the specifiedThrowable
and prevents further subscription.default EventExecutor
defaultSubscriberExecutor()
Returns the defaultEventExecutor
which will be used when a user subscribes usingsubscribe(Subscriber)
,subscribe(Subscriber, SubscriptionOption...)
.default boolean
isComplete()
Returnstrue
if this stream is complete, either successfully or exceptionally, including cancellation and abortion.boolean
isEmpty()
Returnstrue
if this stream has been closed and did not publish any elements.boolean
isOpen()
Returnstrue
if this stream is not closed yet.static <T> StreamMessage<T>
of()
Creates a newStreamMessage
that will publish no objects, just a close event.static <T> StreamMessage<T>
of(T obj)
Creates a newStreamMessage
that will publish the singleobj
.static <T> StreamMessage<T>
of(T... objs)
Creates a newStreamMessage
that will publish the givenobjs
.static <T> StreamMessage<T>
of(T obj1, T obj2)
default void
subscribe(org.reactivestreams.Subscriber<? super T> subscriber)
Requests to start streaming data to the specifiedSubscriber
.default void
subscribe(org.reactivestreams.Subscriber<? super T> subscriber, SubscriptionOption... options)
Requests to start streaming data to the specifiedSubscriber
.void
subscribe(org.reactivestreams.Subscriber<? super T> subscriber, EventExecutor executor)
Requests to start streaming data to the specifiedSubscriber
.void
subscribe(org.reactivestreams.Subscriber<? super T> subscriber, EventExecutor executor, SubscriptionOption... options)
Requests to start streaming data to the specifiedSubscriber
.default StreamMessageDuplicator<T>
toDuplicator()
Returns a newStreamMessageDuplicator
that duplicates thisStreamMessage
into one or moreStreamMessage
s, which publish the same elements.default StreamMessageDuplicator<T>
toDuplicator(EventExecutor executor)
Returns a newStreamMessageDuplicator
that duplicates thisStreamMessage
into one or moreStreamMessage
s, which publish the same elements.CompletableFuture<Void>
whenComplete()
Returns aCompletableFuture
that completes when this stream is complete, either successfully or exceptionally, including cancellation and abortion.
-
Method Details
-
of
Creates a newStreamMessage
that will publish no objects, just a close event. -
of
Creates a newStreamMessage
that will publish the singleobj
. -
of
-
of
Creates a newStreamMessage
that will publish the givenobjs
. -
isOpen
boolean isOpen()Returnstrue
if this stream is not closed yet. Note that a stream may not be complete even if it's closed; a stream is complete when it's fully consumed by aSubscriber
. -
isEmpty
boolean isEmpty()Returnstrue
if this stream has been closed and did not publish any elements. Note that this method will not returntrue
when the stream is open even if it has not published anything so far, because it may publish something later. -
isComplete
default boolean isComplete()Returnstrue
if this stream is complete, either successfully or exceptionally, including cancellation and abortion.A
StreamMessage
is complete (or 'fully consumed') when:- the
Subscriber
consumes all elements andSubscriber.onComplete()
is invoked, - an error occurred and
Subscriber.onError(Throwable)
is invoked, - the
Subscription
has been cancelled or abort()
has been requested.
- the
-
whenComplete
CompletableFuture<Void> whenComplete()Returns aCompletableFuture
that completes when this stream is complete, either successfully or exceptionally, including cancellation and abortion.A
StreamMessage
is complete (or 'fully consumed') when:- the
Subscriber
consumes all elements andSubscriber.onComplete()
is invoked, - an error occurred and
Subscriber.onError(Throwable)
is invoked, - the
Subscription
has been cancelled or abort()
has been requested.
- the
-
subscribe
Requests to start streaming data to the specifiedSubscriber
. If there is a problem subscribing,Subscriber.onError(Throwable)
will be invoked with one of the following exceptions:IllegalStateException
if otherSubscriber
subscribed to this stream already.AbortedStreamException
if this stream has been aborted.CancelledSubscriptionException
if this stream has been cancelled andSubscriptionOption.NOTIFY_CANCELLATION
is specified when subscribed.- Other exceptions that occurred due to an error while retrieving the elements.
- Specified by:
subscribe
in interfaceorg.reactivestreams.Publisher<T>
-
subscribe
default void subscribe(org.reactivestreams.Subscriber<? super T> subscriber, SubscriptionOption... options)Requests to start streaming data to the specifiedSubscriber
. If there is a problem subscribing,Subscriber.onError(Throwable)
will be invoked with one of the following exceptions:IllegalStateException
if otherSubscriber
subscribed to this stream already.AbortedStreamException
if this stream has been aborted.CancelledSubscriptionException
if this stream has been cancelled andSubscriptionOption.NOTIFY_CANCELLATION
is specified when subscribed.- Other exceptions that occurred due to an error while retrieving the elements.
- Parameters:
options
-SubscriptionOption
s to subscribe with
-
subscribe
Requests to start streaming data to the specifiedSubscriber
. If there is a problem subscribing,Subscriber.onError(Throwable)
will be invoked with one of the following exceptions:IllegalStateException
if otherSubscriber
subscribed to this stream already.AbortedStreamException
if this stream has been aborted.CancelledSubscriptionException
if this stream has been cancelled andSubscriptionOption.NOTIFY_CANCELLATION
is specified when subscribed.- Other exceptions that occurred due to an error while retrieving the elements.
- Parameters:
executor
- the executor to subscribe
-
subscribe
void subscribe(org.reactivestreams.Subscriber<? super T> subscriber, EventExecutor executor, SubscriptionOption... options)Requests to start streaming data to the specifiedSubscriber
. If there is a problem subscribing,Subscriber.onError(Throwable)
will be invoked with one of the following exceptions:IllegalStateException
if otherSubscriber
subscribed to this stream already.AbortedStreamException
if this stream has been aborted.CancelledSubscriptionException
if this stream has been cancelled andSubscriptionOption.NOTIFY_CANCELLATION
is specified when subscribed.- Other exceptions that occurred due to an error while retrieving the elements.
- Parameters:
executor
- the executor to subscribeoptions
-SubscriptionOption
s to subscribe with
-
toDuplicator
Returns a newStreamMessageDuplicator
that duplicates thisStreamMessage
into one or moreStreamMessage
s, which publish the same elements. Note that you cannot subscribe to thisStreamMessage
anymore after you call this method. To subscribe, callStreamMessageDuplicator.duplicate()
from the returnedStreamMessageDuplicator
. -
toDuplicator
Returns a newStreamMessageDuplicator
that duplicates thisStreamMessage
into one or moreStreamMessage
s, which publish the same elements. Note that you cannot subscribe to thisStreamMessage
anymore after you call this method. To subscribe, callStreamMessageDuplicator.duplicate()
from the returnedStreamMessageDuplicator
.- Parameters:
executor
- the executor to duplicate
-
defaultSubscriberExecutor
Returns the defaultEventExecutor
which will be used when a user subscribes usingsubscribe(Subscriber)
,subscribe(Subscriber, SubscriptionOption...)
.Please note that if this method is called multiple times, the returned
EventExecutor
s can be different depending on thisStreamMessage
implementation. -
abort
void abort()Closes this stream withAbortedStreamException
and prevents further subscription. ASubscriber
that attempts to subscribe to an aborted stream will be notified with anAbortedStreamException
viaSubscriber.onError(Throwable)
. Calling this method on a closed or aborted stream has no effect. -
abort
-