Wednesday, December 23, 2015

Reactive Programming - Introduction

What is Reactive Programming?

If a program propagates all the changes that modify its data to all the interested parties (users, other programs, components, and subparts), then this program can be called reactive.

Simple Example:

int a = 4;
int b = 5;

int c = a + b; System.out.println(c); // 9
a = 6; System.out.println(c); // 9 again,

but if 'c' was tracking the changes of 'a' and 'b', // it would've been 6 + 5 = 11; Therefore this is not reactive.

Why should we be reactive? 


In the past it was normal for websites to have a slower response time, but in the current day, we have to deal with high response to the user.
eg:  "likes" to some content can be reflected in real time to other connected users, and so forth.  In most cases, these applications communicate with a large number of remote services as well. 

How to be reactive ?


Modular/dynamic - because modules can go offline and come online without breaking or halting the entire system - Being modular means the system should be   event-driven.  We can divide the system into multiple micro-services/components/modules that are going to communicate with each other using notifications
Scalable: This way, we are going to be able to handle a huge amount of data or large numbers of user requests.  - to react to load without falling apart.
NOTE:  You can build a message-driven, resilient, scalable, and responsive application without using a reactive library or language.

Reactive Programming With Java?


(https://github.com/ReactiveX/RxJava), an open source Java implementation of the reactive programming paradigm. Writing code using RxJava requires a different kind of thinking, but it will give you the power to create complex logic using simple pieces of well-structured code.

Example:

List<String> list = Arrays.asList("One", "Two", "Three", "Four", "Five");

//RXJava Observable

Observable<String> observable = Observable.from(list);
  
// Subscribe to the Observable. It will PUSH it's values to the Subscriber, and it //will be printed.

observable.subscribe(new Action1<String>() {

   public void call(String element) {
    System.out.println(element);
   }
  }, new Action1<Throwable>() {
   public void call(Throwable t) {
    System.err.println(t); 
   }
  }, new Action0() {
   public void call() {
    System.out.println("We've finnished!"); 
   }

  });


Here, we can subscribe to the Observable instance. By subscribing, we tell RxJava that we are interested in this Observable instance and want to receive notifications from it. We subscribe using an anonymous class implementing the Action1 interface, by defining a single method—call(T). This method will be called by the Observable instance every time it has a value, ready to be pushed. Always creating new Action1 instances may seem too verbose, but Java 8 solves this verbosity.

So, every string from the source list will be pushed through to the call() method, and it will be printed


This is just an introduction:
Please find here for more info.
https://github.com/meddle0x53/learning-rxjava