What is RxJava?


Why RxJava? 

Let's start in fashion - Life will be easy with RxJava.

RxJava is an art and endless possibilities await those who can master it.

Yes, that's true.


So, let's see technically what is RxJava?

RxJava is a Java VM implementation of Reactive Extensions.

Question arises in mind, What is Reactive Extensions?

From the official doc, Reactive Extensions(ReactiveX) is as a library for composing asynchronous and event-based programs by using observable sequences.

Now we have to learn these three terms - asynchronous, event-based and observable sequences.

  1. Asynchronous: Different parts of the program run simultaneously.
  2. Event-Based: The program executes the code based on the event generated.
  3. Observable Sequences: We will see it later for better understanding.
  4. The Reactive Extension is further elaborated: It extends the observer pattern to support sequences of data and/or events and adds operators that allow you to compose sequences together declaratively while abstracting away concerns about things like low-level threading, synchronization, thread-safety, concurrent data structures, and non-blocking I/O.

Here, observer pattern is the key to understand the architecture, which is a software design pattern, where Subject maintains its Observers and notifies them automatically of any state change, usually by calling one of their methods.

RxJava is an implementation of Reactive Stream Specification

How does this Reactive Stream implement?


There are two main components of RxJava:

  1. Observable: it emits the values.
  2. Observer: it gets the values.

Observer get the emitted values from Observable by subscribing on it.


All components are as follows:

  1. Flowable, Observable, Single, and Completable - does some work and emit values.
  2. Subsciption - work is going on or completed or is used to cancel.
  3. Operators - Modify Data
  4. Schedulers - Where the work should be done, which thread like main thread, etc.
  5. Subscriber/Disposable - where the response will be sent after work has been completed.

Show me the code to understand it in a better way.


    // Observable just is a factory method that emmits the values.
    private Observable<String> getObservable() {
        return Observable.just("Cricket", "Football");
    }


    private Observer<String> getObserver() {
        return new Observer<String>() {

            @Override
            public void onSubscribe(Disposable d) {
                
            }

            @Override
            public void onNext(String value) {
                System.out.println(value);
            }

            @Override
            public void onError(Throwable e) {

            }

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


Now, we have to connect both Observable and Observer with a Subscription. Only then can it actually do anything:


    private void doSomeWork() {
        getObservable()
                // Run on a background thread
                .subscribeOn(Schedulers.io())
                // Be notified on the main thread
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(getObserver());
    }

This will print the following

Cricket

Football

onComplete

This means the Observable emits two strings, one by one, which are observed by Observer.

So, here it goes to onNext two times and then after emitting all the values it goes to onComplete


The complete RxJava Examples can be found here: Link