Class BufferedEmittingPublisher<T>

  • Type Parameters:
    T - type of emitted item
    All Implemented Interfaces:
    Flow.Publisher<T>

    public class BufferedEmittingPublisher<T>
    extends Object
    implements Flow.Publisher<T>
    Emitting publisher for manual publishing with built-in buffer for handling backpressure.

    This publisher allows only a single subscriber.

    • Constructor Detail

      • BufferedEmittingPublisher

        protected BufferedEmittingPublisher()
    • Method Detail

      • onRequest

        public void onRequest​(BiConsumer<Long,​Long> requestCallback)
        Callback executed when request signal from downstream arrive.
        • param n the requested count.
        • param result the current total cumulative requested count, ranges between [0, Long.MAX_VALUE] where the max indicates that this publisher is unbounded.
        Parameters:
        requestCallback - to be executed
      • onEmit

        public void onEmit​(Consumer<T> onEmitCallback)
        Callback executed right after onNext is actually sent.
        • param i sent item
        Parameters:
        onEmitCallback - to be executed
      • emit

        public int emit​(T item)
        Emit item to the stream, if there is no immediate demand from downstream, buffer item for sending when demand is signaled.
        Parameters:
        item - to be emitted
        Returns:
        actual size of the buffer, value should be used as informative and can change asynchronously
        Throws:
        IllegalStateException - if cancelled, completed of failed
      • fail

        public void fail​(Throwable throwable)
        Send onError signal downstream, regardless of the buffer content. Nothing else can be sent downstream after calling fail. emit(Object) throws IllegalStateException after calling fail.
        Parameters:
        throwable - Throwable to be sent downstream as onError signal.
      • complete

        public void complete()
        Drain the buffer, in case of not sufficient demands wait for more requests, then send onComplete signal to downstream. emit(Object) throws IllegalStateException after calling complete.
      • completeNow

        public void completeNow()
        Send onComplete signal downstream immediately, regardless of the buffer content. Nothing else can be sent downstream after calling completeNow(). emit(Object) throws IllegalStateException after calling completeNow.
      • clearBuffer

        public void clearBuffer​(Consumer<T> consumer)
        Clear whole buffer, invoke consumer for each item before discarding it.
        Parameters:
        consumer - to be invoked for each item
      • isUnbounded

        public boolean isUnbounded()
        Check if downstream requested unbounded number of items eg. Long.MAX_VALUE.
        Returns:
        true if so
      • hasRequests

        public boolean hasRequests()
        Check if demand is higher than 0. Returned value should be used as informative and can change asynchronously.
        Returns:
        true if demand is higher than 0
      • isCompleted

        public boolean isCompleted()
        Check if publisher sent onComplete signal downstream. Returns true right after calling completeNow() but after calling complete() returns false until whole buffer has been drained.
        Returns:
        true if so
      • isCancelled

        public boolean isCancelled()
        Check if publisher is in terminal state CANCELLED.
        Returns:
        true if so
      • bufferSize

        public int bufferSize()
        Estimated size of the buffer. Returned value should be used as informative and can change asynchronously.
        Returns:
        estimated size of the buffer