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 Details

    • DefaultStreamMessage

      public DefaultStreamMessage()
      Creates a new instance.
  • Method Details

    • isOpen

      public final boolean isOpen()
      Description copied from interface: StreamMessage
      Returns true 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.
    • isEmpty

      public final boolean isEmpty()
      Description copied from interface: StreamMessage
      Returns true 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.
    • subscribe0

      protected void subscribe0(EventExecutor executor, SubscriptionOption[] options)
      Invoked when a subscriber subscribes.
    • onRequest

      protected void onRequest(long n)
      Invoked whenever a new demand is requested.
      Parameters:
      n - Newly requested demand
    • abort

      public final void abort()
      Description copied from interface: StreamMessage
      Closes this stream with AbortedStreamException 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.
    • abort

      public final void abort(Throwable cause)
      Description copied from interface: StreamMessage
      Closes this stream with the specified Throwable and prevents further subscription. A Subscriber that attempts to subscribe to an aborted stream will be notified with the specified Throwable via Subscriber.onError(Throwable). Calling this method on a closed or aborted stream has no effect.
    • 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 the StreamMessage successfully. Subscriber.onComplete() will be invoked to signal that the Subscriber has consumed the stream completely.
    • close

      public final void close(Throwable cause)
      Description copied from interface: StreamWriter
      Closes the StreamMessage exceptionally. Subscriber.onError(Throwable) will be invoked to signal that the Subscriber did not consume the stream completely.
    • tryClose

      public final boolean tryClose(Throwable cause)
      Tries to close the stream with the specified cause.
      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 the StreamMessage. The written object will be transferred to the Subscriber.
      Specified by:
      tryWrite in interface StreamWriter<T>
      Returns:
      true if the specified object has been scheduled for publication. false if the stream has been closed already.
      See Also:
    • whenConsumed

      public final CompletableFuture<Void> whenConsumed()
      Description copied from interface: StreamWriter
      Returns a CompletableFuture which is completed when all elements written so far have been consumed by the Subscriber.
      Specified by:
      whenConsumed in interface StreamWriter<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 specified Subscriber. If there is a problem subscribing, Subscriber.onError(Throwable) will be invoked with one of the following exceptions:
      Specified by:
      subscribe in interface StreamMessage<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 specified Subscriber. If there is a problem subscribing, Subscriber.onError(Throwable) will be invoked with one of the following exceptions:
      Specified by:
      subscribe in interface StreamMessage<T>
      executor - the executor to subscribe
      options - SubscriptionOptions to subscribe with
    • whenComplete

      public final CompletableFuture<Void> whenComplete()
      Description copied from interface: StreamMessage
      Returns a CompletableFuture 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 and Subscriber.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 interface StreamMessage<T>
    • onRemoval

      protected void onRemoval(T obj)
      Invoked after an element is removed from the StreamMessage and before Subscriber.onNext(Object) is invoked.
      Parameters:
      obj - the removed element