Build a Reactive code


Just imagine the time of you waiting for the bus at bus stop or waiting for the pizza sitting at your favourite pizza shop.

Waiting yes, Waiting makes you feel the worst right. So how bad it is of an application waiting for the reply from an another application.

Well, The only way we find for an application to do multiple things at a time is threading, yes threading works well but we don’t even know why our application getting hanged up suddenly.

Yes,If you are using threading you might have faced the problem that ‘it was working fine till now but suddenly stopped working, you don’t even know why and what had happened to that now’. In most of the cases, It is because improper flow of threading.

One more problem with threading is Exception. Let me give you an example, when we are dealing with multiple threads, the question is how do you deal with exceptions? if you were to handle an exception in threads and imagine you have 1000’s of threads running and at some point one time 164 threads failed. You know, if you want to know how that feel. Its like running a day care center and half the children immediately starts crying at the same time, what do you do? you just have to call for help, its like I don’t know whats going on everybody is crying right now. So, we as a developers need to concentrate not only on application flow but also on multitasking flow which might finally screw up the product.

Well, to help to get ride of these problems we have something called Reactive programming.

Reactive programming is a really nice step forward from functional programming, let me explain why i say that. when it comes to functional programming we really care about two things.

  1. Immutability
  2. Higher-order functions

well when it comes to immutability we wanna be able to avoid constantly mutating stuff, so for example:

import java.util.*;
public class Sample{
public static void main(String args[]){
List<String> symbols;
symbols = Arrays.asList("GOOG","AAPL","MSFT","INTC");
List<String> symbolsInLowerCase = new ArrayList<>();
for(String symbol:symbols){

this is imperative programming,well what if we could write this code in a functional way:


It removes all the accidental complexity that we would normally deal with in our code and also time and effort in the long run itself. So, this would give us an idea about how we could do it.

The other thing we talk about when it comes to functional programming is, we talk about as Higher-order functions.

Higher order functions are those where we can pass the function to the another function or we could even create function and return function from the function.

In functional programming we can enjoy function pipeline and lazy evaluation. Reactive programming is a beautiful next step to the functional programming, I feel that reactive programming starts where functional programming leaves us.

we can approach Reactive programming using different libraries one of them is ‘ReactiveX’ yes it is reactive and it is available for 10+ languages. RxJava is ReactiveX java library. Lets us see how we can use it.

RxJava has Observables, it is nothing but data flow/data stream, think of observable as a little tank you can open and water begins to flow through it. Think of observable as a little radio that you can turn on and start listening the stream.

Even Iterator can do sequential streaming, but the question is whats the difference between Observable and an Iterator, well the very first difference is in the case of Iterator, they pull the data one at a time. But Observables are different, here they push the data when the data is available.

will be continued…