sexta-feira, 3 de junho de 2016

Reactive Extensions for Java(RxJava) - Overview of some concepts behind the RxJava

RxJava is the Java implementation of ReactiveX and is a library for compose asynchronous and event-based programs using observable sequence for the JVM

RxJava implements of the Reactor pattern, and the observer pattern that could be consider combination between Interator Pattern and Functional Programing, in other words are lot of functions to create, combine and filters streams.

According defined on official documentation: 
"It extends the observer pattern to support sequences of data and/or events and adds operators that allow you to compose sequences together declaratively while abstracting away concerns about things like low-level threading, synchronization, thread-safety, concurrent data structures, and non-blocking I/O." 

The main target of Rx is whole platform JVM not just a language. Until this moment, has supports to Java, Groovy, Scala, JavaScript, Ruby.

One of the ideas of Rx is provide a DSL for creating flows out of asynchronous sources using collections of operators for filtering, selecting transforming and combining that flow in a lazy manner. Encapsulates data sequences in Observable. Provide composable operations on them.

These flows are called Observables, collections of events with push semantics, opposed to pull used in iterator.

Non-blocking is not faster, non blocking could be consider about utilizing threads better, if thread utilization is not a issue, it will perform about the same.
About the Observable and Observer we can consider:
  • Observable -> source of data stream (SENDER)
  • Observer -> listener for emitted values (RECEIVER)
  • The Observer Subscribes (listens) to the Observable.
  • Observers react to whatever item or sequence of items the Observable emits.
  • Many observers can subscribe to the same observable.

Observable and Observer Pattern differences:
  • Observer:
    • Allows for concurrent operations
    • The observer does not need to block while waiting for the observable to emit values.
    • Observer waits to receive values when the observable is ready to emit them.
    • Based on push rather than pull.
  • Observable:
    • Composable and easily chained together or combined.
    • Flexible and can be used to emit network results, Sequences, infinite streams.
    • Free from callback hell.
    • Easy to transform one asynchronous stream into another.

Sample Code:

Observable.just("Hello, world!")
    .map(s -> s.hashCode())
    .map(i -> Integer.toString(i))
    .subscribe(s -> System.out.println(s));
Comparison between Iterable and Observable Architecture:
 Iterable Architecture - How it works:
  • Call a method
  • Wait for result
  • Store the return value from that method in a variable.
  • Use that variable and its new value to do something useful.

 Observable Architecture - How it works:
  • Define an Observer that specifics what to do with each emitted value.
  • Call a method that returns an Observable.
  • Subscribe the Observer to the Observable.
  • Tell the Observable that it has a subscriber waiting to receive values when they're available.

The subscribe method connects an Observer to an Observable, the subscribe no need to block the thread and the values will come to your Observer when they are ready.
What should we think about when we use Rx:
  • Treat all data as immutable
  • Since we treat data as a stream, we can return one or more results for each subscription
  • We can transform and combine data on any thread and subscribe to update for related values/collections

My Sandbox with RxJava

Available RX Functions

Reactive Manifesto

Erik Meijer: Rx in 15 Minutes - Rx is Here!!!!!

101 Rx Samples


Nenhum comentário:

Postar um comentário