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>>
drainAll(EventExecutor executor)
Subscribes to thisStreamMessage
and retrieves all elements from it.CompletableFuture<List<T>>
drainAll(EventExecutor executor, SubscriptionOption... options)
Subscribes to thisStreamMessage
and retrieves all elements from it.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.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 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
defaultSubscriberExecutor, drainAll, drainAll, drainAll, drainAll, isComplete, 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 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 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. -
abort
public 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
-
close
public 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
public boolean tryWrite(T obj)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
-
drainAll
Description copied from interface:StreamMessage
Subscribes to thisStreamMessage
and retrieves all elements from it. The returnedCompletableFuture
may be completed exceptionally with the following exceptions:IllegalStateException
if otherSubscriber
subscribed to this stream already.AbortedStreamException
if this stream has been aborted.- Other exceptions that occurred due to an error while retrieving the elements.
- Specified by:
drainAll
in interfaceStreamMessage<T>
- Parameters:
executor
- the executor to retrieve all elements- Returns:
- the
CompletableFuture
which will be completed with the list of the elements retrieved.
-
drainAll
public final CompletableFuture<List<T>> drainAll(EventExecutor executor, SubscriptionOption... options)Description copied from interface:StreamMessage
Subscribes to thisStreamMessage
and retrieves all elements from it. The returnedCompletableFuture
may be completed exceptionally with the following exceptions:IllegalStateException
if otherSubscriber
subscribed to this stream already.AbortedStreamException
if this stream has been aborted.- Other exceptions that occurred due to an error while retrieving the elements.
- Specified by:
drainAll
in interfaceStreamMessage<T>
- Parameters:
executor
- the executor to retrieve all elementsoptions
-SubscriptionOption
s to subscribe with. Note thatSubscriptionOption.NOTIFY_CANCELLATION
is ineffective because there's no cancelling while draining all elements.- Returns:
- the
CompletableFuture
which will be completed with the list of the elements retrieved.
-
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
protected void onRemoval(T obj)Invoked after an element is removed from theStreamMessage
and beforeSubscriber.onNext(Object)
is invoked.- Parameters:
obj
- the removed element
-