Class SimplePublisher<T>

  • All Implemented Interfaces:
    org.reactivestreams.Publisher<T>

    public final class SimplePublisher<T>
    extends Object
    implements org.reactivestreams.Publisher<T>
    A Publisher to which callers can send(Object) messages, simplifying the process of implementing a publisher.

    Operations

    The SimplePublisher supports three simplified operations:

    1. send(Object) for sending messages
    2. complete() for indicating the successful end of messages
    3. error(Throwable) for indicating the unsuccessful end of messages
    Each of these operations returns a CompletableFuture for indicating when the message has been successfully sent.

    Callers are expected to invoke a series of send(Object)s followed by a single complete() or error(Throwable). See the documentation on each operation for more details.

    This publisher will store an unbounded number of messages. It is recommended that callers limit the number of in-flight send(Object) operations in order to bound the amount of memory used by this publisher.

    • Constructor Detail

      • SimplePublisher

        public SimplePublisher()
    • Method Detail

      • send

        public CompletableFuture<Void> send​(T value)
        Send a message using this publisher.

        Messages sent using this publisher will eventually be sent to a downstream subscriber, in the order they were written. When the message is sent to the subscriber, the returned future will be completed successfully.

        This method may be invoked concurrently when the order of messages is not important.

        In the time between when this method is invoked and the returned future is not completed, this publisher stores the request message in memory. Callers are recommended to limit the number of sends in progress at a time to bound the amount of memory used by this publisher.

        The returned future will be completed exceptionally if the downstream subscriber cancels the subscription, or if the send call was performed after a complete() or error(Throwable) call.

        Parameters:
        value - The message to send. Must not be null.
        Returns:
        A future that is completed when the message is sent to the subscriber.
      • complete

        public CompletableFuture<Void> complete()
        Indicate that no more send(Object) calls will be made, and that stream of messages is completed successfully.

        This can be called before any in-flight send calls are complete. Such messages will be processed before the stream is treated as complete. The returned future will be completed successfully when the complete is sent to the downstream subscriber.

        After this method is invoked, any future send(Object), complete() or error(Throwable) calls will be completed exceptionally and not be processed.

        The returned future will be completed exceptionally if the downstream subscriber cancels the subscription, or if the complete call was performed after a complete or error(Throwable) call.

        Returns:
        A future that is completed when the complete has been sent to the downstream subscriber.
      • error

        public CompletableFuture<Void> error​(Throwable error)
        Indicate that no more send(Object) calls will be made, and that streaming of messages has failed.

        This can be called before any in-flight send calls are complete. Such messages will be processed before the stream is treated as being in-error. The returned future will be completed successfully when the error is sent to the downstream subscriber.

        After this method is invoked, any future send(Object), complete() or #error(Throwable) calls will be completed exceptionally and not be processed.

        The returned future will be completed exceptionally if the downstream subscriber cancels the subscription, or if the complete call was performed after a complete() or error call.

        Parameters:
        error - The error to send.
        Returns:
        A future that is completed when the exception has been sent to the downstream subscriber.
      • subscribe

        public void subscribe​(org.reactivestreams.Subscriber<? super T> s)
        A method called by the downstream subscriber in order to subscribe to the publisher.
        Specified by:
        subscribe in interface org.reactivestreams.Publisher<T>