Globant
Published in

Globant

Reactive Programming : Project-Reactor / Rx-Java / Java 9 Reactive Flow

What is Reactive Programming ?
Reactive Programming is a programming paradigm associated with non-blocking, asynchronous and event driven processing to data streams. It facilitates automatic propagation of changed data flow.

Why Reactive ?

Need of reactive programming

above example shows the need of reactive programming that you would want your application to be reactive if the data is changing rapidly and you don’t want explicit update calls to every piece who is interested in the data values.

Business use-cases for Reactive Programming:
- Stock Market application, where stock prices continuously vary.
- Cab service applications like Uber/Ola, where multiple metrics like cab locations, pickup locations etc keep on changing.

Different implementation options for JAVA :

  1. Project Reactor
  2. Rx-Java
  3. Java 9 Reactive Flow

Newbies to the reactive programming will always have the question which one to be used ?
We will find it out in this article.

4 Pillars of Reactive Programming:

Before going into different ways for implementation, let’s go through the basics of reactive programming, the 4 pillars are the 4 interfaces which each of the 3 libraries listed above use directly or indirectly.

  • Interface Publisher
    has 1 method :
    - subscribe(Subscriber<? super T> subscriber)
  • Interface Subscriber
    has 4 methods :
    - onSubscribe(Subscription subscription)
    - onNext(T item)
    - onError(Throwable throwable)
    - onComplete()
  • Interface Subscription
    has 2 methods :
    - request(Long n)
    - cancel()
  • Interface Processor
    extends Subscriber<T> , Publisher<T>

Basic workflow/sequence in a reactive programming paradigm :

Sample workflow for Reactive Paradigm

A subscriber calls subscribe method of publisher to subscribe, publisher returns a subscription to subscriber. Subscriber can request for data to publisher via onRequest and then publisher emits events 1 to N times via onNext calls and finally emits an onComplete event to tell subscriber about completion of events.

Lets see how each library use these concepts:

Project Reactor

Project Reactor provides Spring-WebFlux to work with reactive programming in a spring boot application. It provides two data types:
Flux : Publisher that can publish 0 or N elements.
Mono : Publisher that can publish 0 or 1 elements.

You will need to add explicit dependency in your maven project

Example:

Example Code

This example shows how a flux publisher emits 4 events when subscribed, and can also be modified using operators provided by library. The log below shows the flow we saw in above workflow between a subscriber and publisher which starts with subscribe() and ends with the onComplete() call.

Output

Rx-Java

It is a Java based extension of ReactiveX, where ReactiveX aims to provide reactive programming concepts to different programming languages.

Rx-Java’s key components :
Observables: Represents any object that can get data from datasource and whose state may be of interest in a way others can register the interest, in short we can call them publisher.

Observer: Any object that wishes to be notified when state of another object changes, in short we can assume it as a subscriber.

Observer subscribes to Observables and then Observables sends items to Observer one at a time.

Observables can be created in one of the following ways:
- Observable : emits 0 to N items, it is not back-pressure enabled
- Flowable : emits 0 to N items and is back-pressure enabled, so we can use Observable when we have fewer items or rate of publish is expected to be low else Flowable should be used. I won’t talk about back-pressure in this article you can read about it
here.
- Completable : it doesn’t emit any item.
- Single : emits only 1 item or error.
- Maybe : emits either no item or 1 item.

You can create an Observable just like Flux from just, while it has other options to create which we won’t be covering here like fromIterable, fromCallable etc.

You will need to add explicit dependency in your maven project

Example:

Same example as we used for Project Reactor above, instead of Flux we will create the publisher from Observable and then subscribe to it to print all values.

Example code
Output

Rx-Java don’t provide logging feature for calls happening as we get from Project Reactor but the flow is same as we saw in workflow diagram above.

Java 9 Reactive Flow

Java 9 has introduced Reactive Streams under java.util.concurrent.Flow package. Flow API contains four interrelated core interfaces: java.util.concurrent.Publisher, java.util.concurrent.Subscriber, java.util.concurrent.Subscription, and java.util.concurrent.Processor, which are exactly same as we have discussed above in 4 Pillars of Reactive Programming.

As it is shipped along with Java 9 you don’t need any extra dependency in your project.

Example:

Here we will create a publisher which will emit the input list of strings, also we will create a subscriber which will subscribe to the publisher and process the emitted events.

Example code
Output

If you have noticed we used Flow.Subscription to control the back-pressure here as once we process the item we request the next one.

Comparison:

Comparison

Note: All three libraries have much more than what is listed in this document, this article only aims to give a feel of what each library has in its box for reactive programming and kickstart to start exploring them.

Conclusion:

All three libraries have their own pros and cons in terms of APIs and streaming paradigm. It all depends on the requirement and choice of tool that needs to perform the action with integration. API also contains very little difference. Hope this will save your time in choosing the right library for your specific requirement.

References:

https://github.com/ReactiveX/RxJava
https://projectreactor.io/
https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/Flow.html

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store