Class DefaultStreamMessage<T>
java.lang.Object
com.linecorp.armeria.common.stream.DefaultStreamMessage<T>
- Type Parameters:
T
- the type of element signaled
- All Implemented Interfaces:
StreamMessage<T>
,StreamWriter<T>
,org.reactivestreams.Publisher<T>
@UnstableApi public class DefaultStreamMessage<T> extends Object
A
StreamMessage
which buffers the elements to be signaled into a Queue
.
This class implements the StreamWriter
interface as well. A written element will be buffered
into the Queue
until a Subscriber
consumes it. Use StreamWriter.whenConsumed()
to control the rate of production so that the Queue
does not grow up infinitely.
void stream(DefaultStreamMessage<Integer> pub, int start, int end) {
// Write 100 integers at most.
int actualEnd = (int) Math.min(end, start + 100L);
int i;
for (i = start; i < actualEnd; i++) {
pub.write(i);
}
if (i == end) {
// Wrote the last element.
return;
}
pub.whenConsumed().thenRun(() -> stream(pub, i, end));
}
final DefaultStreamMessage<Integer> myPub = new DefaultStreamMessage<>();
stream(myPub, 0, Integer.MAX_VALUE);
-
Constructor Summary
Constructors Constructor Description DefaultStreamMessage()
Creates a new instance. -
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.void
close()
Closes theStreamMessage
successfully.void
close(Throwable cause)
Closes theStreamMessage
exceptionally.CompletableFuture<List<T>>
collect(EventExecutor executor, SubscriptionOption... options)
Collects the elements published by thisStreamMessage
with the specifiedEventExecutor
andSubscriptionOption
s.long
demand()
Returns the current demand of this stream.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.protected void
onRemoval(T obj)
Invoked after an element is removed from theStreamMessage
and beforeSubscriber.onNext(Object)
is invoked.protected void
onRequest(long n)
Invoked whenever a new demand is requested.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
.protected void
subscribe0(EventExecutor executor, SubscriptionOption[] options)
Invoked when a subscriber subscribes.protected boolean
tryClose(Throwable cause)
Tries to close the stream with the specifiedcause
.boolean
tryWrite(T obj)
Writes the specified object to theStreamMessage
.CompletableFuture<Void>
whenComplete()
Returns aCompletableFuture
that completes when this stream is complete, either successfully or exceptionally, including cancellation and abortion.CompletableFuture<Void>
whenConsumed()
Returns aCompletableFuture
which is completed when all elements written so far have been consumed by theSubscriber
.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.linecorp.armeria.common.stream.StreamMessage
collect, collect, defaultSubscriberExecutor, filter, isComplete, map, subscribe, subscribe, subscribe, subscribe, toDuplicator, toDuplicator, whenComplete
Methods inherited from interface com.linecorp.armeria.common.stream.StreamWriter
tryWrite, write, write
-
Constructor Details
-
DefaultStreamMessage
public DefaultStreamMessage()Creates a new instance.
-
-
Method Details
-
isOpen
public final boolean isOpen()Description copied from interface:StreamMessage
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
public final boolean isEmpty()Description copied from interface:StreamMessage
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. -
subscribe0
Invoked when a subscriber subscribes. -
collect
Description copied from interface:StreamMessage
Collects the elements published by thisStreamMessage
with the specifiedEventExecutor
andSubscriptionOption
s. The returnedCompletableFuture
will be notified when the elements are fully consumed.Note that if this
StreamMessage
was subscribed by otherSubscriber
already, the returnedCompletableFuture
will be completed with anIllegalStateException
. -
onRequest
protected void onRequest(long n)Invoked whenever a new demand is requested. -
abort
public final void abort()Description copied from interface:StreamMessage
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
Description copied from interface:StreamMessage
-
demand
public final long demand()Description copied from interface:StreamMessage
Returns the current demand of this stream. -
close
public final void close()Description copied from interface:StreamWriter
Closes theStreamMessage
successfully.Subscriber.onComplete()
will be invoked to signal that theSubscriber
has consumed the stream completely. -
close
Description copied from interface:StreamWriter
Closes theStreamMessage
exceptionally.Subscriber.onError(Throwable)
will be invoked to signal that theSubscriber
did not consume the stream completely. -
tryClose
Tries to close the stream with the specifiedcause
.- Returns:
true
if the stream has been closed by this method call.false
if the stream has been closed already by other party.
-
tryWrite
Description copied from interface:StreamWriter
Writes the specified object to theStreamMessage
. The written object will be transferred to theSubscriber
.- Specified by:
tryWrite
in interfaceStreamWriter<T>
- Returns:
true
if the specified object has been scheduled for publication.false
if the stream has been closed already.- See Also:
- Life cycle of reference-counted objects
-
whenConsumed
Description copied from interface:StreamWriter
Returns aCompletableFuture
which is completed when all elements written so far have been consumed by theSubscriber
.- Specified by:
whenConsumed
in interfaceStreamWriter<T>
- Returns:
- the future which completes successfully when all elements written so far have been consumed,
or exceptionally when the
StreamMessage
has been closed unexpectedly.
-
subscribe
public final void subscribe(org.reactivestreams.Subscriber<? super T> subscriber, EventExecutor executor)Description copied from interface:StreamMessage
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 interfaceStreamMessage<T>
executor
- the executor to subscribe
-
subscribe
public final void subscribe(org.reactivestreams.Subscriber<? super T> subscriber, EventExecutor executor, SubscriptionOption... options)Description copied from interface:StreamMessage
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 interfaceStreamMessage<T>
executor
- the executor to subscribeoptions
-SubscriptionOption
s to subscribe with
-
whenComplete
Description copied from interface:StreamMessage
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 StreamMessage.abort()
has been requested.
- Specified by:
whenComplete
in interfaceStreamMessage<T>
- the
-
onRemoval
Invoked after an element is removed from theStreamMessage
and beforeSubscriber.onNext(Object)
is invoked.- Parameters:
obj
- the removed element
-