T - the type of element signaled@UnstableApi public class DefaultStreamMessage<T> extends Object
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 and Description |
|---|
DefaultStreamMessage()
Creates a new instance.
|
| Modifier and Type | Method and Description |
|---|---|
void |
abort()
Closes this stream with
AbortedStreamException and prevents further subscription. |
void |
abort(Throwable cause)
Closes this stream with the specified
Throwable and prevents further subscription. |
void |
close() |
void |
close(Throwable cause) |
CompletableFuture<List<T>> |
drainAll(io.netty.util.concurrent.EventExecutor executor)
Subscribes to this
StreamMessage and retrieves all elements from it. |
CompletableFuture<List<T>> |
drainAll(io.netty.util.concurrent.EventExecutor executor,
SubscriptionOption... options)
Subscribes to this
StreamMessage and retrieves all elements from it. |
boolean |
isEmpty()
Returns
true if this stream has been closed and did not publish any elements. |
boolean |
isOpen()
Returns
true if this stream is not closed yet. |
protected void |
onRemoval(T obj)
Invoked after an element is removed from the
StreamMessage and before
Subscriber.onNext(Object) is invoked. |
void |
subscribe(org.reactivestreams.Subscriber<? super T> subscriber,
io.netty.util.concurrent.EventExecutor executor)
Requests to start streaming data to the specified
Subscriber. |
void |
subscribe(org.reactivestreams.Subscriber<? super T> subscriber,
io.netty.util.concurrent.EventExecutor executor,
SubscriptionOption... options)
Requests to start streaming data to the specified
Subscriber. |
protected boolean |
tryClose(Throwable cause)
Tries to close the stream with the specified
cause. |
boolean |
tryWrite(T obj)
Writes the specified object to the
StreamMessage. |
CompletableFuture<Void> |
whenComplete()
Returns a
CompletableFuture that completes when this stream is complete,
either successfully or exceptionally, including cancellation and abortion. |
CompletableFuture<Void> |
whenConsumed()
Returns a
CompletableFuture which is completed when all elements written so far have been
consumed by the Subscriber. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitcloseFuture, completionFuture, defaultSubscriberExecutor, drainAll, drainAll, drainAll, drainAll, isComplete, of, of, of, of, subscribe, subscribe, subscribe, subscribe, toDuplicator, toDuplicator, whenCompletepublic boolean isOpen()
StreamMessagetrue 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 a Subscriber.public boolean isEmpty()
StreamMessagetrue if this stream has been closed and did not publish any elements.
Note that this method will not return true when the stream is open even if it has not
published anything so far, because it may publish something later.public void abort()
StreamMessageAbortedStreamException and prevents further subscription.
A Subscriber that attempts to subscribe to an aborted stream will be notified with
an AbortedStreamException via Subscriber.onError(Throwable). Calling this method
on a closed or aborted stream has no effect.public void abort(Throwable cause)
StreamMessagepublic void close()
public void close(Throwable cause)
protected final boolean tryClose(Throwable cause)
cause.true if the stream has been closed by this method call.
false if the stream has been closed already by other party.public boolean tryWrite(T obj)
StreamWriterStreamMessage. The written object will be transferred to the
Subscriber.true if the specified object has been scheduled for publication. false if the
stream has been closed already.public CompletableFuture<Void> whenConsumed()
StreamWriterCompletableFuture which is completed when all elements written so far have been
consumed by the Subscriber.StreamMessage has been closed unexpectedly.public final void subscribe(org.reactivestreams.Subscriber<? super T> subscriber,
io.netty.util.concurrent.EventExecutor executor)
StreamMessageSubscriber. If there is a problem subscribing,
Subscriber.onError(Throwable) will be invoked with one of the following exceptions:
IllegalStateException if other Subscriber subscribed to this stream already.AbortedStreamException if this stream has been aborted.CancelledSubscriptionException if this stream has been
cancelled and SubscriptionOption.NOTIFY_CANCELLATION is
specified when subscribed.subscribe in interface StreamMessage<T>executor - the executor to subscribepublic final void subscribe(org.reactivestreams.Subscriber<? super T> subscriber,
io.netty.util.concurrent.EventExecutor executor,
SubscriptionOption... options)
StreamMessageSubscriber. If there is a problem subscribing,
Subscriber.onError(Throwable) will be invoked with one of the following exceptions:
IllegalStateException if other Subscriber subscribed to this stream already.AbortedStreamException if this stream has been aborted.CancelledSubscriptionException if this stream has been
cancelled and SubscriptionOption.NOTIFY_CANCELLATION is
specified when subscribed.subscribe in interface StreamMessage<T>executor - the executor to subscribeoptions - SubscriptionOptions to subscribe withpublic final CompletableFuture<List<T>> drainAll(io.netty.util.concurrent.EventExecutor executor)
StreamMessageStreamMessage and retrieves all elements from it.
The returned CompletableFuture may be completed exceptionally with the following exceptions:
IllegalStateException if other Subscriber subscribed to this stream already.AbortedStreamException if this stream has been aborted.drainAll in interface StreamMessage<T>executor - the executor to retrieve all elementsCompletableFuture which will be completed with the list of the elements retrieved.public final CompletableFuture<List<T>> drainAll(io.netty.util.concurrent.EventExecutor executor, SubscriptionOption... options)
StreamMessageStreamMessage and retrieves all elements from it.
The returned CompletableFuture may be completed exceptionally with the following exceptions:
IllegalStateException if other Subscriber subscribed to this stream already.AbortedStreamException if this stream has been aborted.drainAll in interface StreamMessage<T>executor - the executor to retrieve all elementsoptions - SubscriptionOptions to subscribe with. Note that
SubscriptionOption.NOTIFY_CANCELLATION is ineffective because there's no
cancelling while draining all elements.CompletableFuture which will be completed with the list of the elements retrieved.public final CompletableFuture<Void> whenComplete()
StreamMessageCompletableFuture that completes when this stream is complete,
either successfully or exceptionally, including cancellation and abortion.
A StreamMessage is complete
(or 'fully consumed') when:
Subscriber consumes all elements and Subscriber.onComplete() is invoked,Subscriber.onError(Throwable) is invoked,Subscription has been cancelled orStreamMessage.abort() has been requested.whenComplete in interface StreamMessage<T>protected void onRemoval(T obj)
StreamMessage and before
Subscriber.onNext(Object) is invoked.obj - the removed elementCopyright © 2020 LeanCloud. All rights reserved.