RxJava Basics with example | Create, Subscribe, Synchronous, Async, Parallel, Backpressure, Non-Blocking | Good for beginners

In this article we will go through very basic & simple examples of RxJava 2 to understand different ways in which publisher & subscriber interact to perform desired operations.

Here is the Maven dependency for io.reactivex.rxjava2 – rxjava

RxJava Synchronous

This is example where publishing & subscribing is synchronous. Publisher & subscriber both run on same thread.

  • create() – Creates Observable i.e. publisher i.e. emitter.
  • subscribe() – Subscribes to Observable i.e. publisher i.e. emitter & starts receiving emitted values.



Subscriber takes 100ms to complete processing. You can see that, when subscriber takes 100 ms to process, publisher is blocked during that time. Next value will be published only when subscriber processing is finished.



RxJava Asynchronous

In this example, we will put publisher i.e. observable & subscriber on different threads & make it asynchronous.

  • create() – Creates Observable i.e. publisher i.e. emitter.
  • subscribeOn() – Informs Observable to put subscriber in different thread that current thread i.e. main thread.
  • observeOn() – Informs Observable to put publisher i.e. observable in different thread that current thread i.e. main thread.
  • subscribe() – Subscribes to Observable i.e. publisher i.e. emitter & starts receiving emitted values.

You can see in output that thread names for publisher & subscriber are different. Also publisher is not waiting for subscriber to finish processing.



RxJava Parallel processing

If there is some processing that needs to be done on large emitted data set, then processing can be put on parallel operation & then after completion, it can be merged back as shown in below example.

  • create() – Creates Flowable i.e. publisher i.e. emitter.
  • parallel() & runOn() –  Converts Flowable to ParallelFlowable & Informs it to put further processing in different thread that current thread i.e. main thread.
  • map() – This does the processing of calculating squares of all numbers & map it to ParallelFlowable.
  • sequential() – Merges parallel operation results & converts ParallelFlowable back to Flowable.
  • blockingSubscribe() – Subscribes to Flowable & starts receiving emitted values in the same thread i.e. main thread.



RxJava Async with Backpressure

During asynchronous processing, if subscriber is consuming data very slow than publisher, this situation is called as backpressure. RxJava provides a way to handle the Backpressure gracefully.

Observable – In case of Observable, there is unbounded buffer i.e. infinite buffer. So all the data published/emitted is stored & made sure that subscriber receives that. But if published data is very very huge, then it might cause OutOfMemory error eventually. Observable does not provide graceful ways to handle this backpressure.

Flowable – Flowable provides graful ways to handle backpressure. In next section, we will look at different strategies offered by Flowable. Flowable also has default buffer of 128 values.

RxJava Basics with example | Backpressure – DROP, ERROR, LATEST, MISSING | Good for beginners

 

 



Leave a Reply

Your email address will not be published. Required fields are marked *