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
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
680
Jakarta EE meets AI
ivargrimstad
0
870
Tauriでネイティブアプリを作りたい
tsucchinoko
0
380
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
490
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
2
130
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
280
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
360
Ethereum_.pdf
nekomatu
0
470
C++でシェーダを書く
fadis
6
4.2k
romajip: 日本の住所CSVデータを活用した英語住所変換ライブラリを作った話
sangunkang
0
1.1k
[Do iOS '24] Ship your app on a Friday...and enjoy your weekend!
polpielladev
0
120
rails new flags - `rails new` のフラグから Rails を構成するコンポーネントの変遷をザックリ眺める
snaka
0
1.8k
Featured
See All Featured
Automating Front-end Workflow
addyosmani
1366
200k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
Making Projects Easy
brettharned
115
5.9k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
A designer walks into a library…
pauljervisheath
204
24k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
730
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
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