Sie sind auf Seite 1von 51

Reactive Programming with RxJava

Shareif.com
About me

Romain Cambier
@r_cambier github.com/cambierr
romain@shareif.com

CS Engineer from Mons (172), BE


Founder & Owner of Shareif.com
Founder of TTN Belgium
Intro
Today's apps: Facebook

get profile

get friends

.
get news

get news
www.facebook.com

get chats
get msgs Facebook

...
messenger
get msgs
get ads

Facebook ads
Today's apps: General

Application/Website

File Storage API Database ...


Today's apps: General

Your app

time spent waiting

Your db
Today's apps: My Super E-Shop

10 k 100 k, 10 / user 10 k, 10 / order


Today's apps: My Super E-Shop

How much money have the first 1000


users spent in my shop (per user) ?
Today's apps: My Super E-Shop
Today's apps: My Super E-Shop
RxJava
RxJava is:

Reactive Extension for the JVM:

A library for composing asynchronous and event-based


programs using observable sequences
Reminder: Observable DP
Reminder: Observable DP
Observable Observer

registerObserver(me)
This means : Derpina, keep me in touch !

Something happens to Derpina

notify(data)
This means : Derp, I'm pregnant
Derp just died..
Reactive means :

RESPONSIVE
(react to users input)

RESILIENT
(react to errors and failures)

ELASTIC
(react to load)

MESSAGE-DRIVEN
(react to events and message)
RX Observable
=
Chained Observables
RX Observable
Observable Observable Observable

observe(me)
observe(me)

...
observe(me)

onNext(data)

onNext(data)
onNext(data)
Steam? Everything is aIf you
stream!
try hard enough

New student create student

Assign a class Give him housing

Give him
Create a password
login & password
Steam? Everything is aIf you
stream!
try hard enough

So this means:

When you have a new student, create it in your db.


Then, give gim a student housing.
After that, assign him a class.
You should then create a password.
And finally, give him its login & pass.
Just remember this:

Reactive programming is about


dealing with event stream
RX Observable DP
Observable Subscriber

subscribe(me)

onStart()

onNext(data)
onCompleted()

onError(exception)
RxJava Operators
Empty
Just
From
Error
Range
Interval
Subscriber
Map
FlatMap
ToList
Filter
GroupBy
Take / TakeLast
Skip / SkipLast
Count
Distinct
IgnoreElements
First / Last / Single
Create
Multi-threading made easy
(No, really)
Playing with Threads

Schedulers :

IO: For all I/O operations


Computation: For computational work

NewThread: A new thread for each job

Trampoline: Wait for your thread to be available

Immediate: Do it, now.


SubscribeOn
ObserveOn
Playing with threads

This is single-thread code :

This takes 100,000 ms to run


Playing with threads

This is multi-thread code :

This takes 1,000 ms to run


DIY time!
RX is also:

RxJava RxJs Rx.NET UniRx


RxScala RxClojure RxCpp Rx.rb
RxPY RxGroovy RxJRuby RxKotlin
RxSwift RxPHP RxQuasar RxRust
RxRoboVM RxGo

RxNetty RxAndroid RxCocoa RxApacheHttp


RxJavaFileUtils RxJavaFX RxSwing
Meta

Images are from : http://reactivex.io

RX javadoc : http://goo.gl/Cu4zcD

Database used : http://couchbase.com

Docker : http://docker.com

DIY sources : https://goo.gl/9bnhNy


Shareif.com

Das könnte Ihnen auch gefallen