reactive java - reactive programming + rxjava
TRANSCRIPT
ReactiveJava = Reactive Programming using RxJava
Hello!
I AM Hiten Pratap SinghI am here because I love to dig in to experience new things. You can find me at:◦ [email protected]◦ http://github.com/hitenpratap/◦ http://hprog99.wordpress.com/
I have divided this presentation into two parts
ReactiveProgramming RxJava
Reactive ProgrammingWhat’s Reactive Programming? What’s the big deal about it?
“In computing, reactive programming is a programming paradigm oriented around data flows and the propagation of change.
Reactive Programming
Reactive programming is programming with asynchronous data streams.◦ Event buses or your typical click events are really
an asynchronous event stream◦ Streams are cheap and ubiquitous, anything can be
a stream: variables, user inputs, properties etc.◦ For example, imagine your Twitter feed would be
a data stream in the same fashion that click events are.
Reactive Programming
On top of that, you are given an amazing toolbox of functions to combine, create and filter any of those streams.◦ A stream can be used as an input to another one.◦ You can merge two streams.◦ You can filter a stream to get another one that has
only those events you are interested in.◦ You can map data values from one stream to
another new one.
A stream is a sequence of ongoing events ordered in time. It can emit three different things: a value (of some type), an error, or a "completed" signal. Consider that the "completed" takes place, for instance, when the current window or view containing that button is closed.
We capture these emitted events only asynchronously, by defining a function that will execute when a value is emitted, another function when an error is emitted, and another function when 'completed' is emitted.The "listening" to the stream is called subscribing. The functions we are defining are observers. The stream is the subject (or "observable") being observed. This is precisely the Observer Design Pattern.
Reactive Programming
Why should I consider adopting RP? ◦ RP raises the level of abstraction of your code.◦ Code in RP will likely be more concise.◦ Apps nowadays have an abundancy of real-time
events of every kind that enable a highly interactive experience to the user. ◦ We need tools for properly dealing with that, and
Reactive Programming is an answer.
Reactive Programming
Key Takeaways◦ RP is a specification for dealing with
asynchronous streams of data.◦ Reactive provides tools for transforming and
combining streams and for managing flow-control◦ Resembles Java Streams API but the resemblance
is purely superficial
Thinking in RP withExample
What’s ReactiveX?
◦ A library for composing async and event based program by using observable sequences.
◦ Created by Microsoft initially for .Net platform by Erik Meijer
◦ Extends observer pattern-* Supports sequence of data and/or events* Add operators that allow you to compose sequences together declaratively
▫ Abstracts away concerns around* Threading and Thread Safety* Concurrent data structures and non blocking I/O
▫ RxJava is a port of Reactive Extensions created by Netflix.
What’s ReactiveX?
RxJavaA library to use mighty powers of Reactive Programming in Java!
Maven
Getting Started
Gradle
You can also include its jar file without having to use any of build system as well.
Reactive Components/Building Blocks
ObserverObservable Subject
Observable
Observable
In ReactiveX an observer subscribes to an Observable. Then that observer reacts to whatever item or sequence of items the Observable emits. This pattern facilitates concurrent operations because it does not need to block while waiting for the Observable to emit objects, but instead it creates a sentry in the form of an observer that stands ready to react appropriately at whatever future time the Observable does so.
◦ Emits zero or more values◦ Life Cycles
* Notifies Observer using onNext(T)* Completes with either onError() or onCompleted()
Operators
Operators By Categories
Creating Observables
Transforming Observables
Filtering Observables
Combining Observables
Error handling Operators
Observable Utility
Operators
Operators By Categories(Contd.)
Conditional and Boolean
Operators
Mathematical and
Aggregate Operators
Backpressure Operators
ReactiveX provides great flexibility over operators by letting us chaining them together or even creating new ones.
Transforming Observable
Transforming Observable << Map
Transforming Observable << Flatmap
Filtering Observable
Filtering Observable << Take
Filtering Observable << Filter
Filtering Observable << Distinct
Filtering Observable << First
Combining Observable
Combining Observable << Merge
Combining Observable << Zip
SchedulersIf you want to introduce multithreading into your cascade of Observable operators, you can do so by instructing those operators (or particular Observables) to operate on particular Schedulers.
Observable Utility Operators
Observable Utility Operators << SubscribeOn
Observable Utility Operators << ObserveOn
Single
Single
◦ RxJava (and its derivatives like RxGroovy & RxScala) has developed an Observable variant called “Single.”
◦ A Single is something like an Observable, but instead of emitting a series of values — anywhere from none at all to an infinite number — it always either emits one value or an error notification.
◦ For this reason, instead of subscribing to a Single with the three methods you use to respond to notifications from an Observable (onNext, onError, and onCompleted), you only use two methods to subscribe:
Subjects
Subject
A Subject is a sort of bridge or proxy that is available in some implementations of ReactiveX that acts both as an observer and as an Observable. Because it is an observer, it can subscribe to one or more Observables, and because it is an Observable, it can pass through the items it observes by reemitting them, and it can also emit new items.
Because a Subject subscribes to an Observable, it will trigger that Observable to begin emitting items (if that Observable is “cold” — that is, if it waits for a subscription before it begins to emit items). This can have the effect of making the resulting Subject a “hot” Observable variant of the original “cold” Observable.
Varieties of Subject
◦ AsyncSubject◦ BehaviorSubject◦ PublishSubject◦ ReplaySubject
Subject << AsyncSubject
Subject << BehaviorSubject
Subject << PublishSubject
Subject << ReplaySubject
Backpressure
Backpressure
In RxJava it is not difficult to get into a situation in which an Observable is emitting items more rapidly than an operator or subscriber can consume them. This presents the problem of what to do with such a growing backlog of unconsumed items.
For example, imagine using the zip operator to zip together two infinite Observables, one of which emits items twice as frequently as the other. A naive implementation of the zip operator would have to maintain an ever-expanding buffer of items emitted by the faster Observable to eventually combine with items emitted by the slower one. This could cause RxJava to seize an unwieldy amount of system resources.
Hot and ColdObservable
Hot Observable
A hot Observable begins generating items to emit immediately when it is created. Subscribers typically begin observing the sequence of items emitted by a hot Observable from somewhere in the middle of the sequence, beginning with the first item emitted by the Observable subsequent to the establishment of the subscription.
Such an Observable emits items at its own pace, and it is up to its observers to keep up.
Examples of items emitted by a hot Observable might include mouse & keyboard events, system events, or stock prices.
Cold Observable
A cold Observable emits a particular sequence of items, but can begin emitting this sequence when its Observer finds it to be convenient, and at whatever rate the Observer desires, without disrupting the integrity of the sequence.
For example if you convert a static Iterable into an Observable, that Observable will emit the same sequence of items no matter when it is later subscribed to or how frequently those items are observed.
Examples of items emitted by a cold Observable might include the results of a database query, file retrieval, or web request.
Conclusion
Pros
◦ Makes Async code easy to develop◦ Lots of resources to get started with◦ Strong use of functional type programming◦ Netflix used RxNetty and Reactive Programming to
considerable performance advantage over Tomcat◦ Web Service clients like Jersey and Retrofit support RxJava.
Cons
◦ Learning curve and mind set change to use functional reactive programming
◦ Appears like more code and more complex code initially –Requires time for it to grow on you.
◦ If on Java 7 anonymous class would make this a non-starter.
Thanks!
ANY QUESTIONS?You can find me at:[email protected]://github.com/hitenpratap/http://hprog99.wordpress.com/