Factors affecting the Java Benchmarking.

Reactive programming and ReactiveX [RxJava]

[ My programming notes ]

During past few years i have been working mostly on CRUD applications. Most of us are familiar with frameworks and application which enables us to implement good quality CRUD applications. Few decades ago Engineers used to work on CRUD applications most of the time. We were introduced to programming during a time where companies implement different types of applications. But still most of the time, we got the opportunity only to work on CRUD application which manipulates data.

If we are to create an application which act as an processing unit, a CRUD application model like MVC + DB won't be the suitable method to follow.  Few examples for applications which act as processing units are,
  1. Applications to monitor certain topics in twitter.

  2. Monitor stock prices based on several conditions.

  3. Monitor current weather information and send notifications to users via the mobile app.

  4. Observe faces and recognize culprits in real time.


According to the Reactive Manifesto, Reactive systems are Responsive, Resilient, Elastic and Message Driven. https://www.reactivemanifesto.org/

Responsive : Responsive systems focus on providing rapid and consistent response times. Having an upper bound for the response time makes the application consistent

Resilient : You application should not break, in any case. System should stay responsive in the face of failures. Have to handle your errors properly and focus not only on the happy paths.

Elastic : System should be able to handle various loads (High or Low). Engineers should have a design for the system which uses memory and other resources efficiently. Devops help to achieve this and services like AWS and Google cloud enable us to handle these loads easily and cost efficiently.

Message Driven : Using asynchronous message-passing to enable loose coupling among modules. Rest APIs, Message queues and etc.

Go to https://www.reactivemanifesto.org/ website and read more in-detail.

We know and have tried to accommodate above mentioned qualities in our systems. Reactive is about bringing these things together.

Multi-treading and callbacks are not the techniques we are looking for when implementing reactive programs.
Why not threading ?

Threading is a facility to allow multiple activities to coexist within a single process. There are two ways to create a thread,
  • Extend Thread class

  • Implement Runnable
These both methods use Runnable  [link][Below is a Runnable example]. Runnable is an interface which has a single void method called run() and it doesn't accept any parameters and does not return any values. This is suitable in cases where we don't expect anything from thread execution. Example : Logging ( It is a cross cutting concern and business logic doesn't depend on it). Runnable is not the choice when it comes to Reactive Programming.

Why not callable ?

We can execute an asynchronous task in a separate thread using callable. Callable object is passed to the java ExecutorService to be executed asynchronously. call() method in Callable interface is used to execute the task and Java Future is being used to return back the result. java Future get() method waits for the java callable task to be completed. It blocks the program and we don't want that when we are implementing Reactive Programs.

For single level asynchronous execution [link]  it is easy to use Java Future, but when there are nested asynchronous executions, it gets complicated.

For conditional asynchronous execution also it is difficult to implement with Java Future and can get very complicated. [link]

Future.get() prematurely block the program and removes the purpose of asynchronous  execution. [Check below example]

ReactiveX

ReactiveX is a library for composing asynchronous and event-based programs by using observable sequences. Handling threading, synchronization, thread-safety, concurrent data structures, and non-blocking I/O when implementing a system has always been difficult. ReactiveX allows us to handle sequences of data/events declaratively without worrying about these difficult topics.  ReactiveX operates on discrete values that are emitted over time.

Observables


ReactiveX uses observer patterns. ReactiveX allows us to handle streams of asynchronous events like the way we normally work on different types of collections (Arrays, linkedLists etc.). It makes the code clean and readable by avoiding web callbacks, Multi-threading and etc. ReactiveX Observables supports sequences of values or even infinite streams.

Observable pushes data to a stream and client can process data. And it has methods to handle retrieve data [onNext(T)], discover error [onError(Exception)] and complete [onCompleted()].


Observable can be implemented using thread-pools, event loops, non-blocking I/O, actors (such as from Akka), or any other implementation.



Observable is like a producer which pushes values to the consumer whenever values are available. This approach is more flexible, because values can arrive synchronously or asynchronously.

Follow this link to see examples https://github.com/h-hub/Java_Practice/tree/master/src/main/java/javaPractice/rxJava

References:
  • http://tutorials.jenkov.com/java-concurrency/creating-and-starting-threads.html

  • https://dzone.com/articles/deal-with-disadvantagesnbspof-multithreading

  • https://www.journaldev.com/1090/java-callable-future-example

  • http://reactivex.io/intro.html

  • https://www.ibm.com/developerworks/java/tutorials/j-threads/j-threads.html

  • https://www.baeldung.com/java-runnable-callable

  • https://en.wikipedia.org/wiki/Observer_pattern

  • https://youtu.be/weWSYIUdX6c

Comments