Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Reactive Extensions for JVM
Search
Blaž Šolar
December 21, 2015
Programming
0
210
Reactive Extensions for JVM
Blaž Šolar
December 21, 2015
Tweet
Share
More Decks by Blaž Šolar
See All by Blaž Šolar
Secrets from Google I/O
blazsolar
0
55
Advance Android
blazsolar
0
75
Other Decks in Programming
See All in Programming
大規模アプリのDIフレームワーク刷新戦略 ~過去最大規模の並行開発を止めずにアプリ全体に導入するまで~
mot_techtalk
1
430
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
250
Server Side Kotlin Meetup vol.16: 内部動作を理解して ハイパフォーマンスなサーバサイド Kotlin アプリケーションを書こう
ternbusty
3
180
登壇は dynamic! な営みである / speech is dynamic
da1chi
0
300
dynamic!
moro
10
7.4k
CSC305 Lecture 04
javiergs
PRO
0
270
CSC509 Lecture 03
javiergs
PRO
0
330
Serena MCPのすすめ
wadakatu
4
980
私はどうやって技術力を上げたのか
yusukebe
43
18k
Android16 Migration Stories ~Building a Pattern for Android OS upgrades~
reoandroider
0
100
CI_CD「健康診断」のススメ。現場でのボトルネック特定から、健康診断を通じた組織的な改善手法
teamlab
PRO
0
210
Go言語はstack overflowの夢を見るか?
logica0419
0
180
Featured
See All Featured
How to Ace a Technical Interview
jacobian
280
24k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
30
2.9k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
114
20k
[RailsConf 2023] Rails as a piece of cake
palkan
57
5.9k
GraphQLとの向き合い方2022年版
quramy
49
14k
Build The Right Thing And Hit Your Dates
maggiecrowley
37
2.9k
The Language of Interfaces
destraynor
162
25k
Become a Pro
speakerdeck
PRO
29
5.5k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Learning to Love Humans: Emotional Interface Design
aarron
274
41k
Building Applications with DynamoDB
mza
96
6.7k
Gamification - CAS2011
davidbonilla
81
5.5k
Transcript
Reactive extensions for JVM
“A library for composing asynchronous and event-based programs using observable
sequences for the JVM” - RxJava
“ReactiveX is a combination of the best ideas from the
Observer pattern, the Iterator pattern, and functional programming” - ReactiveX
Reactive
Problem
Limited method signature public Data getData(); What if the implementation
needs to change from synchronous to asynchronous How should the client execute this method without blocking? Spawn a thread?
Let’s do it async public void getData(Callback<T> c); public Future<T>
getData(); public Future<List<Future<T>>> getData();
Observable push onNext(T) onError(Exception) onCompleted() Iterable vs. Observable Iterable pull
T next() throws Exception returns
Observable push onNext(T) onError(Exception) onCompleted() Iterable pull T next() throws
Exception returns getDataFromLocalMemory() .skip(10) .take(5) .map({ s -> s + "_transformed" }) .forEach({ println "onNext => " + it }) getDataFromNetwork() .skip(10) .take(5) .map({ s -> s + "_transformed" }) .subscribe({ println "onNext => " + it })
How to deal with data Single Multiple Sync T getData();
Iterable<T> getData(); Async Future<T> getData(); Observable<T> getData();
How to deal with data Single Multiple Sync T getData();
Iterable<T> getData(); Async Future<T> getData(); Observable<T> getData(); String s = getData(); if(s.equals(x)) { // do something } else { // do something else }
How to deal with data Single Multiple Sync T getData();
Iterable<T> getData(); Async Future<T> getData(); Observable<T> getData(); Iterable<String> values = getData(); for(String s : values) { if(s.equals(x)) { // do sometinhg } else { // do something else } }
How to deal with data Single Multiple Sync T getData();
Iterable<T> getData(); Async Future<T> getData(); Observable<T> getData(); Future<String> s = getData(); if(s.get().equals(x)) { // do sometinhg } else { // do something else }
How to deal with data Single Multiple Sync T getData();
Iterable<T> getData(); Async Future<T> getData(); Observable<T> getData(); CompletableFuture<String> s = getData(); s.thenAply((v) -> { if(v.equals(x)) { // do something } else { // do something else } })
How to deal with data Single Multiple Sync T getData();
Iterable<T> getData(); Async Future<T> getData(); Observable<T> getData(); Future<String> s = getData(); s.map({ s -> if(s.equals(x)) { // do something } else { // do something else } })
How to deal with data Observable<String> s = getData(); s.map({
s -> if(s.equals(x)) { // do something } else { // do something else } }) Single Multiple Sync T getData(); Iterable<T> getData(); Async Future<T> getData(); Observable<T> getData();
Code examples
Synchronous Observable with single value Observable.create({ observer -> try {
Location location = /** get last location */ observer.onNext(location); observer.onCompleted(); } catch(Exception e) { observer.onError(e); } })
Asynchronous Observable with single value Observable.create({ observer -> executor.execute(new Runnable()
{ def void run() { try { Location location = /** get location */ observer.onNext(location); observer.onCompleted(); } catch(Exception e) { observer.onError(e); } } }) })
Synchronous Observable with multiple values Observable.create({ observer -> try {
for(l in locations) { observer.onNext(l); } observer.onCompleted(); } catch(Exception e) { observer.onError(e); } })
Asynchronous Observable with multiple values Observable.create({ observer -> executor.execute(new Runnable()
{ def void run() { try { for(l in location) { location = /** resolve location */ observer.onNext(location); } observer.onCompleted(); } catch(Exception e) { observer.onError(e); } } }) })
Asynchronous Observer getLocations().subscribe( {location -> prinln "Location: " + location.name
}, { error -> println "Error" }, { println "Completed" } )
Composable functions
Composable functions Transform: map, flatMap, reduce, scan, ... Filter: take,
skip, sample, takeWhile, filter, .... Combine: count, merge, zip, combineLatest, multicast, publish, cache Concurrency: observeOn, subscribeOn Error Handling: onErrorReturn, onErrorResume, ...
Synchronous Observable with single value Observable.create({ observer -> try {
Location location = /** get last location */ observer.onNext(location); observer.onCompleted(); } catch(Exception e) { observer.onError(e); } })
Combining via Merge
Combining via Merge Observable<Data> a = getDataA(); Observable<Data> b =
getDataB(); Observable.merge(a, b) .subscribe( { element -> println "data: " + element})
Combining via Zip
Combining via Merge Observable<Data> a = getDataA(); Observable<String> b =
getDataB(); Observable.zip(a, b, {x, y, -> [x, y]}) .subscribe( { pair -> println "a: " + pair[0] +"b: " + pair[1]})
Error handling Observable<Data> a = getDataA(); Observable<String> b = getDataB();
Observable.zip(a, b, {x, y, -> [x, y]}) .subscribe( { pair -> println "a: " + pair[0] +"b: " + pair[1]}, { exception -> println "Exception " + exception.getMessage})
Error handling
Error handling Observable<Data> a = getDataA(); Observable<String> b = getDataB()
.onErrorResumeNext(getFallbackForB()); Observable.zip(a, b, {x, y, -> [x, y]}) .subscribe( { pair -> println "a: " + pair[0] +"b: " + pair[1]}, { exception -> println "Exception " + exception.getMessage})
Error handling
Thanks!
[email protected]
@solarb