RxJava - UnicastSubject



UnicastSubject queues up events until a single Observer subscribes to it, replays those events to it until the Observer picks up and then subject switches to relaying events live to this single Observer until this UnicastSubject terminates or the Observer disposes of.

Class Declaration

Following is the declaration for io.reactivex.rxjava3.subjects.UnicastSubject<T> class −

public final class UnicastSubject<T>
   extends Subject<T>

Example - Usage of UnicastSubject

ObservableTester.java

package com.tutorialspoint;

import io.reactivex.rxjava3.subjects.UnicastSubject;

public class ObservableTester  {
   public static void main(String[] args) {   
      final StringBuilder result1 = new StringBuilder();
      final StringBuilder result2 = new StringBuilder();         

      UnicastSubject<String> subject =  UnicastSubject.create(); 
      subject.subscribe(value -> result1.append(value) ); 
      subject.onNext("a"); 
      subject.onNext("b"); 
      subject.onNext("c"); 
      // error will be thrown as only single observer is allowed.
      subject.subscribe(value -> result2.append(value)); 
      subject.onNext("d"); 
      subject.onComplete();

      //Output will be d being the last item emitted
      System.out.println(result1);
      //Output will be d being the last item emitted     
      System.out.println(result2);
   }
}

Output

Compile and Run the code to verify the following output −

io.reactivex.rxjava3.exceptions.OnErrorNotImplementedException: The exception was not handled due to missing onError handler in the subscribe() method call. Further reading: https://github.com/ReactiveX/RxJava/wiki/Error-Handling | java.lang.IllegalStateException: Only a single observer allowed.
	at io.reactivex.rxjava3.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:718)
	at io.reactivex.rxjava3.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:715)
	at io.reactivex.rxjava3.internal.observers.LambdaObserver.onError(LambdaObserver.java:77)
	at io.reactivex.rxjava3.internal.disposables.EmptyDisposable.error(EmptyDisposable.java:63)
	at io.reactivex.rxjava3.subjects.UnicastSubject.subscribeActual(UnicastSubject.java:294)
	at io.reactivex.rxjava3.core.Observable.subscribe(Observable.java:13263)
	at io.reactivex.rxjava3.core.Observable.subscribe(Observable.java:13208)
	at io.reactivex.rxjava3.core.Observable.subscribe(Observable.java:13146)
	at com.tutorialspoint.ObservableTester.main(ObservableTester.java:16)
Caused by: java.lang.IllegalStateException: Only a single observer allowed.
	... 5 more
Exception in thread "main" io.reactivex.rxjava3.exceptions.OnErrorNotImplementedException: The exception was not handled due to missing onError handler in the subscribe() method call. Further reading: https://github.com/ReactiveX/RxJava/wiki/Error-Handling | java.lang.IllegalStateException: Only a single observer allowed.
	at io.reactivex.rxjava3.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:718)
	at io.reactivex.rxjava3.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:715)
	at io.reactivex.rxjava3.internal.observers.LambdaObserver.onError(LambdaObserver.java:77)
	at io.reactivex.rxjava3.internal.disposables.EmptyDisposable.error(EmptyDisposable.java:63)
	at io.reactivex.rxjava3.subjects.UnicastSubject.subscribeActual(UnicastSubject.java:294)
	at io.reactivex.rxjava3.core.Observable.subscribe(Observable.java:13263)
	at io.reactivex.rxjava3.core.Observable.subscribe(Observable.java:13208)
	at io.reactivex.rxjava3.core.Observable.subscribe(Observable.java:13146)
	at com.tutorialspoint.ObservableTester.main(ObservableTester.java:16)
Caused by: java.lang.IllegalStateException: Only a single observer allowed.
	... 5 more
abcd

Example - Usage of UnicastSubject events

ObservableTester.java

package com.tutorialspoint;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.subjects.UnicastSubject;

public class ObservableTester  {
   public static void main(String[] args) {   
      UnicastSubject<String> subject = UnicastSubject.create(); 

      subject.subscribe(new Observer<Object>() {

         @Override
         public void onComplete() {
            System.out.println("Observer: Completed");
         }

         @Override
         public void onError(@NonNull Throwable error) {
            System.out.println("Observer: Error" + error.getMessage());
         }

         @Override
         public void onNext(@NonNull Object item) {
            System.out.println("Observer received: " + item);
         }

         @Override
         public void onSubscribe(@NonNull Disposable arg0) {
            System.out.println("Observer: Subscribed");
         }
      });

      subject.onNext("A"); 
      subject.onNext("B");

      subject.onComplete();
   }
}

Output

Compile and Run the code to verify the following output −

Observer: Subscribed
Observer received: A
Observer received: B
Observer: Completed
Advertisements