reactive java - reactive programming + rxjava

56
ReactiveJava = Reactive Programming using RxJava

Upload: nexthoughts-technologies

Post on 15-Apr-2017

189 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Reactive java - Reactive Programming + RxJava

ReactiveJava = Reactive Programming using RxJava

Page 2: Reactive java - Reactive Programming + 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/

Page 3: Reactive java - Reactive Programming + RxJava

I have divided this presentation into two parts

ReactiveProgramming RxJava

Page 4: Reactive java - Reactive Programming + RxJava

Reactive ProgrammingWhat’s Reactive Programming? What’s the big deal about it?

Page 5: Reactive java - Reactive Programming + RxJava
Page 6: Reactive java - Reactive Programming + RxJava

“In computing, reactive programming is a programming paradigm oriented around data flows and the propagation of change.

Page 7: Reactive java - Reactive Programming + RxJava

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.

Page 8: Reactive java - Reactive Programming + RxJava

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.

Page 9: Reactive java - Reactive Programming + RxJava

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.

Page 10: Reactive java - Reactive Programming + RxJava

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.

Page 11: Reactive java - Reactive Programming + RxJava

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

Page 12: Reactive java - Reactive Programming + RxJava

Thinking in RP withExample

Page 13: Reactive java - Reactive Programming + RxJava

What’s ReactiveX?

Page 14: Reactive java - Reactive Programming + RxJava

◦ 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?

Page 15: Reactive java - Reactive Programming + RxJava

RxJavaA library to use mighty powers of Reactive Programming in Java!

Page 16: Reactive java - Reactive Programming + RxJava

Maven

Getting Started

Gradle

You can also include its jar file without having to use any of build system as well.

Page 17: Reactive java - Reactive Programming + RxJava

Reactive Components/Building Blocks

ObserverObservable Subject

Page 18: Reactive java - Reactive Programming + RxJava

Observable

Page 19: Reactive java - Reactive Programming + RxJava

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()

Page 20: Reactive java - Reactive Programming + RxJava
Page 21: Reactive java - Reactive Programming + RxJava

Operators

Page 22: Reactive java - Reactive Programming + RxJava

Operators By Categories

Creating Observables

Transforming Observables

Filtering Observables

Combining Observables

Error handling Operators

Observable Utility

Operators

Page 23: Reactive java - Reactive Programming + RxJava

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.

Page 24: Reactive java - Reactive Programming + RxJava

Transforming Observable

Page 25: Reactive java - Reactive Programming + RxJava

Transforming Observable << Map

Page 26: Reactive java - Reactive Programming + RxJava

Transforming Observable << Flatmap

Page 27: Reactive java - Reactive Programming + RxJava

Filtering Observable

Page 28: Reactive java - Reactive Programming + RxJava

Filtering Observable << Take

Page 29: Reactive java - Reactive Programming + RxJava

Filtering Observable << Filter

Page 30: Reactive java - Reactive Programming + RxJava

Filtering Observable << Distinct

Page 31: Reactive java - Reactive Programming + RxJava

Filtering Observable << First

Page 32: Reactive java - Reactive Programming + RxJava

Combining Observable

Page 33: Reactive java - Reactive Programming + RxJava

Combining Observable << Merge

Page 34: Reactive java - Reactive Programming + RxJava

Combining Observable << Zip

Page 35: Reactive java - Reactive Programming + RxJava

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.

Page 36: Reactive java - Reactive Programming + RxJava

Observable Utility Operators

Page 37: Reactive java - Reactive Programming + RxJava

Observable Utility Operators << SubscribeOn

Page 38: Reactive java - Reactive Programming + RxJava

Observable Utility Operators << ObserveOn

Page 39: Reactive java - Reactive Programming + RxJava

Single

Page 40: Reactive java - Reactive Programming + RxJava

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:

Page 41: Reactive java - Reactive Programming + RxJava

Subjects

Page 42: Reactive java - Reactive Programming + RxJava

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.

Page 43: Reactive java - Reactive Programming + RxJava

Varieties of Subject

◦ AsyncSubject◦ BehaviorSubject◦ PublishSubject◦ ReplaySubject

Page 44: Reactive java - Reactive Programming + RxJava

Subject << AsyncSubject

Page 45: Reactive java - Reactive Programming + RxJava

Subject << BehaviorSubject

Page 46: Reactive java - Reactive Programming + RxJava

Subject << PublishSubject

Page 47: Reactive java - Reactive Programming + RxJava

Subject << ReplaySubject

Page 48: Reactive java - Reactive Programming + RxJava

Backpressure

Page 49: Reactive java - Reactive Programming + RxJava

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.

Page 50: Reactive java - Reactive Programming + RxJava

Hot and ColdObservable

Page 51: Reactive java - Reactive Programming + RxJava

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.

Page 52: Reactive java - Reactive Programming + RxJava

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.

Page 53: Reactive java - Reactive Programming + RxJava

Conclusion

Page 54: Reactive java - Reactive Programming + RxJava

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.

Page 55: Reactive java - Reactive Programming + 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.

Page 56: Reactive java - Reactive Programming + RxJava

Thanks!

ANY QUESTIONS?You can find me at:[email protected]://github.com/hitenpratap/http://hprog99.wordpress.com/