Class ResourceObserver<T>

  • Type Parameters:
    T - the value type
    All Implemented Interfaces:
    Observer<T>, Disposable

    public abstract class ResourceObserver<T>
    extends java.lang.Object
    implements Observer<T>, Disposable
    An abstract Observer that allows asynchronous cancellation of its subscription and associated resources.

    All pre-implemented final methods are thread-safe.

    To release the associated resources, one has to call dispose() in onError() and onComplete() explicitly.

    Use add(Disposable) to associate resources (as Disposables) with this ResourceObserver that will be cleaned up when dispose() is called. Removing previously associated resources is not possible but one can create a CompositeDisposable, associate it with this ResourceObserver and then add/remove resources to/from the CompositeDisposable freely.

    Use the dispose() to dispose the sequence from within an onNext implementation.

    Like all other consumers, ResourceObserver can be subscribed only once. Any subsequent attempt to subscribe it to a new source will yield an IllegalStateException with message "It is not allowed to subscribe with a(n) <class name> multiple times.".

    Implementation of onStart(), Observer.onNext(Object), Observer.onError(Throwable) and Observer.onComplete() are not allowed to throw any unchecked exceptions. If for some reason this can't be avoided, use Observable.safeSubscribe(io.reactivex.rxjava3.core.Observer) instead of the standard subscribe() method.

    Example

    
     Disposable d =
         Observable.range(1, 5)
         .subscribeWith(new ResourceObserver<Integer>() {
             @Override public void onStart() {
                 add(Schedulers.single()
                     .scheduleDirect(() -> System.out.println("Time!"),
                         2, TimeUnit.SECONDS));
                 request(1);
             }
             @Override public void onNext(Integer t) {
                 if (t == 3) {
                     dispose();
                 }
                 System.out.println(t);
             }
             @Override public void onError(Throwable t) {
                 t.printStackTrace();
                 dispose();
             }
             @Override public void onComplete() {
                 System.out.println("Done!");
                 dispose();
             }
         });
     // ...
     d.dispose();
     
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(@NonNull Disposable resource)
      Adds a resource to this ResourceObserver.
      void dispose()
      Cancels the main disposable (if any) and disposes the resources associated with this ResourceObserver (if any).
      boolean isDisposed()
      Returns true if this ResourceObserver has been disposed/cancelled.
      protected void onStart()
      Called once the upstream sets a Disposable on this ResourceObserver.
      void onSubscribe​(Disposable d)
      Provides the Observer with the means of cancelling (disposing) the connection (channel) with the Observable in both synchronous (from within Observer.onNext(Object)) and asynchronous manner.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • upstream

        private final java.util.concurrent.atomic.AtomicReference<Disposable> upstream
        The active subscription.
    • Constructor Detail

      • ResourceObserver

        public ResourceObserver()
    • Method Detail

      • add

        public final void add​(@NonNull
                              @NonNull Disposable resource)
        Adds a resource to this ResourceObserver.
        Parameters:
        resource - the resource to add
        Throws:
        java.lang.NullPointerException - if resource is null
      • onStart

        protected void onStart()
        Called once the upstream sets a Disposable on this ResourceObserver.

        You can perform initialization at this moment. The default implementation does nothing.

      • dispose

        public final void dispose()
        Cancels the main disposable (if any) and disposes the resources associated with this ResourceObserver (if any).

        This method can be called before the upstream calls onSubscribe(Disposable) at which case the main Disposable will be immediately disposed.

        Specified by:
        dispose in interface Disposable
      • isDisposed

        public final boolean isDisposed()
        Returns true if this ResourceObserver has been disposed/cancelled.
        Specified by:
        isDisposed in interface Disposable
        Returns:
        true if this ResourceObserver has been disposed/cancelled