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
200
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
54
Advance Android
blazsolar
0
74
Other Decks in Programming
See All in Programming
ファインディの テックブログ爆誕までの軌跡
starfish719
2
1.1k
Honoのおもしろいミドルウェアをみてみよう
yusukebe
1
200
SpringBoot3.4の構造化ログ #kanjava
irof
2
980
ISUCON14公式反省会LT: 社内ISUCONの話
astj
PRO
0
190
2024年のWebフロントエンドのふりかえりと2025年
sakito
1
240
sappoRo.R #12 初心者セッション
kosugitti
0
240
動作確認やテストで漏れがちな観点3選
starfish719
6
1k
“あなた” の開発を支援する AI エージェント Bedrock Engineer / introducing-bedrock-engineer
gawa
11
1.9k
AWS Organizations で実現する、 マルチ AWS アカウントのルートユーザー管理からの脱却
atpons
0
130
How mixi2 Uses TiDB for SNS Scalability and Performance
kanmo
35
14k
Honoとフロントエンドの 型安全性について
yodaka
5
330
Kubernetes History Inspector(KHI)を触ってみた
bells17
0
220
Featured
See All Featured
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.7k
Optimising Largest Contentful Paint
csswizardry
34
3.1k
GitHub's CSS Performance
jonrohan
1030
460k
Adopting Sorbet at Scale
ufuk
74
9.2k
Being A Developer After 40
akosma
89
590k
A better future with KSS
kneath
238
17k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.6k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
YesSQL, Process and Tooling at Scale
rocio
171
14k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
193
16k
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