Class DeferredStreamMessage<T>
- java.lang.Object
-
- com.linecorp.armeria.common.stream.DeferredStreamMessage<T>
-
- Type Parameters:
T
- the type of element signaled
- All Implemented Interfaces:
StreamMessage<T>
,org.reactivestreams.Publisher<T>
- Direct Known Subclasses:
DeferredHttpResponse
public class DeferredStreamMessage<T> extends Object
AStreamMessage
whose stream is published later by anotherStreamMessage
. It is useful when yourStreamMessage
will not be instantiated early.
-
-
Constructor Summary
Constructors Constructor Description DeferredStreamMessage()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
abort()
Closes this stream withAbortedStreamException
and prevents further subscription.void
close()
Closes the deferred stream without setting a delegate.void
close(Throwable cause)
Closes the deferred stream without setting a delegate.CompletableFuture<Void>
completionFuture()
Returns aCompletableFuture
that completes when this stream is complete, either successfully or exceptionally, including cancellation and abortion.protected EventExecutor
defaultSubscriberExecutor()
Returns the defaultEventExecutor
which will be used when a user subscribes usingStreamMessage.subscribe(Subscriber)
orStreamMessage.subscribe(Subscriber, SubscriptionOption...)
.protected void
delegate(StreamMessage<T> delegate)
Sets the delegateStreamMessage
which will actually publish the stream.CompletableFuture<List<T>>
drainAll()
Subscribes to thisStreamMessage
and retrieves all elements from it.CompletableFuture<List<T>>
drainAll(boolean withPooledObjects)
Subscribes to thisStreamMessage
and retrieves all elements from it.CompletableFuture<List<T>>
drainAll(SubscriptionOption... options)
Subscribes to thisStreamMessage
and retrieves all elements from it.CompletableFuture<List<T>>
drainAll(EventExecutor executor)
Subscribes to thisStreamMessage
and retrieves all elements from it.CompletableFuture<List<T>>
drainAll(EventExecutor executor, boolean withPooledObjects)
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)
Requests to start streaming data to the specifiedSubscriber
.void
subscribe(org.reactivestreams.Subscriber<? super T> subscriber, boolean withPooledObjects)
Requests to start streaming data to the specifiedSubscriber
.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, boolean withPooledObjects)
Requests to start streaming data, invoking the specifiedSubscriber
from the specifiedExecutor
.void
subscribe(org.reactivestreams.Subscriber<? super T> subscriber, EventExecutor executor, SubscriptionOption... options)
Requests to start streaming data to the specifiedSubscriber
.-
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
closeFuture, isComplete
-
-
-
-
Method Detail
-
delegate
protected void delegate(StreamMessage<T> delegate)
Sets the delegateStreamMessage
which will actually publish the stream.- Throws:
IllegalStateException
- if the delegate has been set already or ifclose()
orclose(Throwable)
was called already.
-
close
public void close()
Closes the deferred stream without setting a delegate.- Throws:
IllegalStateException
- if the delegate has been set already or ifclose()
orclose(Throwable)
was called already.
-
close
public void close(Throwable cause)
Closes the deferred stream without setting a delegate.- Throws:
IllegalStateException
- if the delegate has been set already or ifclose()
orclose(Throwable)
was called already.
-
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.
-
subscribe
public final void subscribe(org.reactivestreams.Subscriber<? super T> subscriber)
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 interfaceorg.reactivestreams.Publisher<T>
- Specified by:
subscribe
in interfaceStreamMessage<T>
-
subscribe
public final void subscribe(org.reactivestreams.Subscriber<? super T> subscriber, boolean withPooledObjects)
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>
-
subscribe
public final void subscribe(org.reactivestreams.Subscriber<? super T> subscriber, 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>
options
-SubscriptionOption
s to subscribe with
-
subscribe
public final void subscribe(org.reactivestreams.Subscriber<? super T> subscriber, EventExecutor executor, boolean withPooledObjects)
Description copied from interface:StreamMessage
Requests to start streaming data, invoking the specifiedSubscriber
from the specifiedExecutor
. 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)
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
-
defaultSubscriberExecutor
protected EventExecutor defaultSubscriberExecutor()
Returns the defaultEventExecutor
which will be used when a user subscribes usingStreamMessage.subscribe(Subscriber)
orStreamMessage.subscribe(Subscriber, SubscriptionOption...)
.
-
drainAll
public final CompletableFuture<List<T>> 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>
- Returns:
- the
CompletableFuture
which will be completed with the list of the elements retrieved.
-
drainAll
public final CompletableFuture<List<T>> drainAll(boolean withPooledObjects)
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>
- Returns:
- the
CompletableFuture
which will be completed with the list of the elements retrieved.
-
drainAll
public final CompletableFuture<List<T>> drainAll(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:
options
-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.
-
drainAll
public final CompletableFuture<List<T>> drainAll(EventExecutor executor, boolean withPooledObjects)
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)
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.
-
completionFuture
public final CompletableFuture<Void> completionFuture()
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:
completionFuture
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
-
-