Thinkshifts Group

The Essence Of Reactive Programming In Java

Reactive programming (RP) is not something new and cutting-edge when it comes to application development. Chances are, you’ve already heard about it. The term was initially introduced to the IT world in the 1960-s and ever since much has been said and written in its regard. Unfortunately, as it often happens, the new concept quickly fueled a set of misinterpretations around itself, and still continues doing so today. The Reactive Manifesto of 2014 that introduced ‘reactive systems’ and their four ‘sacred principles’ messed everything up even more. So, let’s try to clarify what is what, and understand why and where we need reactive programming in Java application development (if we really do).

#1 What is reactive programming in a few words?

Reactive programming is a programming paradigm that deals with asynchronous data streams (sequences of events) and the specific propagation of change, which means it implements modifications to the execution environment (context) in a certain order.

#2 What is this ‘specific propagation of change’?

Here’s a real-life example. Say, it’s Friday and John wants to spend this evening with his friend Bob, scarfing pizza and watching one of the Star Wars episodes. Let’s outline the options he has.

1. John finishes his work. Then goes and orders the pizza, waits till it’s done. Then picks up his friend. And finally (with Bob and pizza) makes it home and gets down to the movie. It will be the sync approach and it will be way too long, so that probably John will have wanted to call the thing off by that time.

2. John orders his pizza online, phones Bob, invites him to come. He heads home, has his pizza delivered and starts watching the movie (and eating the pizza) without waiting for Bob to show up. That is what can happen with the async approach.

3. John orders pizza, phones Bob, invites him to come, heads home, and gets his pizza delivered. But this time, he waits until Bob comes and only after that he turns the movie on. This is what the reactive approach is about. You wait till all async actions (changes) are completed and then proceed with further actions.

#3 Are reactive programming and reactive systems the same thing?

No, they are not. Though often used interchangeably, the terms are not exactly synonymous and reflect different things.

Reactive systems represent the next level of ‘reactivity’. This level implies specific design and architectural decisions that allow building resilient, flexible, and responsive applications.

You don’t have to use reactive programming in reactive systems, but it’s a good idea to do so, as the combination brings even more benefits to your application, as make them even more loosely coupled, allow more efficient use of resources, make them more responsive and ensure lower latency.

#4 Why do we need ‘reactivity’ in Java?

When it comes to huge volumes of data or multi-userness, we often need asynchronous processing to make our systems fast and responsive. In Java, a representative of old object-oriented programming, asynchronicity can become really troublesome and make the code hard to understand and maintain. So, reactive programming is especially beneficial for this ‘purely’ object-oriented environment as it simplifies dealing with asynchronous flows.

#5 How do I go reactive in Java?

With its latest releases (starting with Java 8), Java itself has made some attempts to introduce built-in reactivity, yet these attempts are not very popular with developers to date. But there’re some live and regularly updated third-party implementations for reactive programming in Java that help to save the day and thus are particularly loved and cherished by Java developers.

RxJava was the first Reactive Extension API specific for the Java platform. It works with Java 6 and provides an opportunity to write asynchronous, event-based programs for both Java and Android Java, which is very convenient.

Spring Reactor is another framework for Java from Spring developers. It is quite similar to RxJava but has simpler abstraction. The framework has managed to win popularity due to the possibility to leverage benefits of Java 8.

#6 What do I get with RP in real life?

Increased performance – due to the possibility to handle huge volumes of data in a quick and stable way.

Improved UX – due to the possibility to keep the application more responsive to its user.

Simplified modifications and updates – due to more readable and easier to predict code.

#7 Do I have to make it all reactive?

Going reactive provides an elegant solution when it comes to specific types of high-load or multi-user applications:

Improved UX – due to the possibility to keep the application more responsive to its user.

- Social networks, chats - Games
- Audio and video apps
- And to the following components of any application type:
- Server code that serves highly interactive UI elements
- Proxy servers, load balancers
- Artificial intelligence, machine learning
- Real-time data streaming