Blog Image

Reactive programming  

What is Reactive Programming?

Reactive Programming is a programming language with the asynchronous data stream.Once an event will raise it will react with a responsive and non-blocking manner that?s why it named it as reactive programming.

In a Reactive Programming context, ?Everything is a Stream and acts in a non-blocking manner when there is data in the stream.?

This Reactive programming in Java is introduced by Netflix organization with API (Rx-Java)

Why we need Reactive Programming?

When we have a traditional way to develop application why we should go for reactive programming this should be the common question for all

There is four pillar to move towards Reactive Programming


1. Responsive

2. Resilient

3. Elastic

4. Message Driven

Responsive:

Responsive means if we are raising any events on stream it will return a response in a fraction of the time as message processing in highly concurrent environments

Resilient:

Resilient means Application should be responsive at the time of failure

Normally we are integrating multiple modules and each depends on each other assume one module is failing so it should not impact all, it should be propagated

Elastic:

Elastic means our system should be handle N number of request, it should be well capable to load balance at any condition

Message Driven:

Message Driven means an asynchronous flow of execution, where we no need to wait for a response after sending the request to a server, once we send request it move to next business it shouldn?t depend on first one response, the first request response should be handled by a callback mechanism

Ideal Use Cases for Implementation of Reactive Programming

1.   A large number of transaction-processing services, as in the banking sector.

2.   Notification services of large online shopping applications, like Amazon.

3.   A share trading business where share prices change simultaneously.

What is Rx-Java?

RxJava is a Java VM implementation of ReactiveX (Reactive Extensions): a library for composing asynchronous and event-based programs by using observable DP.

RxJava tries to be very lightweight. It is implemented as a single JAR that is focused on just the Observable abstraction and related higher-order functions

In simple word, Rx-Java is an API which supports us for reactive programming

Observer Design principle:

The observer pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.

The object which is being watched is called the subject. The objects which are watching the state changes are called observers or listeners

For more visit: observable

Let?s do a small POC so that we will get to know about the design principle

  

  Entity class:

  

First, create one standalone maven project then add below dependency to achieve Reactive programming in java

Create a class and write business with Observable so that it will notify other once a task is done like below



Now let?s understand step by step

Initially, we are invoking getEmployee () method so control goes to it

Form getEmployee () method we are creating the Observable stream and checking each subscriber is subscribed or not

If subscribes then we are iterating that stream and we are calling the subscriber.onNext (Object obj) so that it will return to call back once each subscriber verified and execute logic at end of the loop we are calling subscriber.onCompleted() so that on completion of a task it will notify to all subscriber

A response to above code


Line number 37 indicating subscribe method arguments, subscribe () is an overloaded method


As per method signature

The first argument will take onNext() call which is a callback

The second argument will take care when we have an exception

The third argument will take care of on complete action

 As per above code, we don?t have any exception so second argument value not executed now let?s create an exception manually to check the complete flow of callback execution when a subscriber subscribes an event


Response:


This is all about the observable principle followed by Rx-Java to support Reactive programming.

About author

User Image
Basant

Well, This is Basant Having Experience in Java/J2ee development platform , currently working as a technology consultant in one of MNC and am passionate about Java technology completely lost in code . Exploring, Learning and Sharing.

3

-Comments

Be the first person to write a comment for this Blog
  • author image
    by:
      gopireddy
      3 Days ago

    thank you............................................................

  • author image
    by:
      Nadupuri
      14 Days ago

    thanks a lot bro and sharing this valuable content for us

  • author image
    by:
      Hrudanand J.
      15 Days ago

    Thanks a ton for sharing this and it will be helpful for us .

Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.