Class PublishProcessor<T>

  • Type Parameters:
    T - the value type multicasted to Subscribers.
    All Implemented Interfaces:
    FlowableSubscriber<T>, org.reactivestreams.Processor<T,​T>, org.reactivestreams.Publisher<T>, org.reactivestreams.Subscriber<T>

    public final class PublishProcessor<@NonNull T>
    extends FlowableProcessor<T>
    Processor that multicasts all subsequently observed items to its current Subscribers.

    This processor does not have a public constructor by design; a new empty instance of this PublishProcessor can be created via the create() method.

    Since a PublishProcessor is a Reactive Streams Processor type, nulls are not allowed (Rule 2.13) as parameters to onNext(Object) and onError(Throwable). Such calls will result in a NullPointerException being thrown and the processor's state is not changed.

    PublishProcessor is a Flowable as well as a FlowableProcessor, however, it does not coordinate backpressure between different subscribers and between an upstream source and a subscriber. If an upstream item is received via onNext(Object), if a subscriber is not ready to receive an item, that subscriber is terminated via a MissingBackpressureException. To avoid this case, use offer(Object) and retry sometime later if it returned false. The PublishProcessor's Subscriber-side consumes items in an unbounded manner.

    For a multicasting processor type that also coordinates between the downstream Subscribers and the upstream source as well, consider using MulticastProcessor.

    When this PublishProcessor is terminated via onError(Throwable) or onComplete(), late Subscribers only receive the respective terminal event.

    Unlike a BehaviorProcessor, a PublishProcessor doesn't retain/cache items, therefore, a new Subscriber won't receive any past items.

    Even though PublishProcessor implements the Subscriber interface, calling onSubscribe is not required (Rule 2.12) if the processor is used as a standalone source. However, calling onSubscribe after the PublishProcessor reached its terminal state will result in the given Subscription being canceled immediately.

    Calling onNext(Object), offer(Object), onError(Throwable) and onComplete() is required to be serialized (called from the same thread or called non-overlappingly from different threads through external means of serialization). The FlowableProcessor.toSerialized() method available to all FlowableProcessors provides such serialization and also protects against reentrance (i.e., when a downstream Subscriber consuming this processor also wants to call onNext(Object) on this processor recursively). Note that serializing over offer(Object) is not supported through toSerialized() because it is a method available on the PublishProcessor and BehaviorProcessor classes only.

    This PublishProcessor supports the standard state-peeking methods hasComplete(), hasThrowable(), getThrowable() and hasSubscribers().

    Backpressure:
    The processor does not coordinate backpressure for its subscribers and implements a weaker onSubscribe which calls requests Long.MAX_VALUE from the incoming Subscriptions. This makes it possible to subscribe the PublishProcessor to multiple sources (note on serialization though) unlike the standard Subscriber contract. Child subscribers, however, are not overflown but receive an IllegalStateException in case their requested amount is zero.
    Scheduler:
    PublishProcessor does not operate by default on a particular Scheduler and the Subscribers get notified on the thread the respective onXXX methods were invoked.
    Error handling:
    When the onError(Throwable) is called, the PublishProcessor enters into a terminal state and emits the same Throwable instance to the last set of Subscribers. During this emission, if one or more Subscribers cancel their respective Subscriptions, the Throwable is delivered to the global error handler via RxJavaPlugins.onError(Throwable) (multiple times if multiple Subscribers cancel at once). If there were no Subscribers subscribed to this PublishProcessor when the onError() was called, the global error handler is not invoked.
    Example usage:
     
    
      PublishProcessor<Object> processor = PublishProcessor.create();
      // subscriber1 will receive all onNext and onComplete events
      processor.subscribe(subscriber1);
      processor.onNext("one");
      processor.onNext("two");
      // subscriber2 will only receive "three" and onComplete
      processor.subscribe(subscriber2);
      processor.onNext("three");
      processor.onComplete();
    
       
    See Also:
    MulticastProcessor
    • Constructor Detail

      • PublishProcessor

        PublishProcessor()
        Constructs a PublishProcessor.
        Since:
        2.0
    • Method Detail

      • subscribeActual

        protected void subscribeActual​(@NonNull
                                       @NonNull org.reactivestreams.Subscriber<? super @NonNull T> t)
        Description copied from class: Flowable
        Operator implementations (both source and intermediate) should implement this method that performs the necessary business logic and handles the incoming Subscribers.

        There is no need to call any of the plugin hooks on the current Flowable instance or the Subscriber; all hooks and basic safeguards have been applied by Flowable.subscribe(Subscriber) before this method gets called.

        Specified by:
        subscribeActual in class Flowable<T>
        Parameters:
        t - the incoming Subscriber, never null
      • add

        boolean add​(PublishProcessor.PublishSubscription<@NonNull T> ps)
        Tries to add the given subscriber to the subscribers array atomically or returns false if this processor has terminated.
        Parameters:
        ps - the subscriber to add
        Returns:
        true if successful, false if this processor has terminated
      • onSubscribe

        public void onSubscribe​(@NonNull
                                @NonNull org.reactivestreams.Subscription s)
        Description copied from interface: FlowableSubscriber
        Implementors of this method should make sure everything that needs to be visible in Subscriber.onNext(Object) is established before calling Subscription.request(long). In practice this means no initialization should happen after the request() call and additional behavior is thread safe in respect to onNext.
      • onComplete

        public void onComplete()
      • offer

        @CheckReturnValue
        public boolean offer​(@NonNull
                             @NonNull T t)
        Tries to emit the item to all currently subscribed Subscribers if all of them has requested some value, returns false otherwise.

        This method should be called in a sequential manner just like the onXXX methods of this PublishProcessor.

        History: 2.0.8 - experimental

        Parameters:
        t - the item to emit, not null
        Returns:
        true if the item was emitted to all Subscribers
        Throws:
        java.lang.NullPointerException - if t is null
        Since:
        2.2
      • getThrowable

        @Nullable
        @CheckReturnValue
        public @Nullable java.lang.Throwable getThrowable()
        Description copied from class: FlowableProcessor
        Returns the error that caused the FlowableProcessor to terminate or null if the FlowableProcessor hasn't terminated yet.

        The method is thread-safe.

        Specified by:
        getThrowable in class FlowableProcessor<T>
        Returns:
        the error that caused the FlowableProcessor to terminate or null if the FlowableProcessor hasn't terminated yet