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
OSS開発者という働き方
andpad
5
1.7k
旅行プランAIエージェント開発の裏側
ippo012
2
890
Ruby Parser progress report 2025
yui_knk
1
440
はじめてのMaterial3 Expressive
ym223
2
260
請來的 AI Agent 同事們在寫程式時,怎麼用 pytest 去除各種幻想與盲點
keitheis
0
120
奥深くて厄介な「改行」と仲良くなる20分
oguemon
1
520
詳解!defer panic recover のしくみ / Understanding defer, panic, and recover
convto
0
240
rage against annotate_predecessor
junk0612
0
170
Processing Gem ベースの、2D レトロゲームエンジンの開発
tokujiros
2
120
テストコードはもう書かない:JetBrains AI Assistantに委ねる非同期処理のテスト自動設計・生成
makun
0
250
Introducing ReActionView: A new ActionView-compatible ERB Engine @ Rails World 2025, Amsterdam
marcoroth
0
670
Tool Catalog Agent for Bedrock AgentCore Gateway
licux
6
2.4k
Featured
See All Featured
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.5k
Facilitating Awesome Meetings
lara
55
6.5k
Build your cross-platform service in a week with App Engine
jlugia
231
18k
Gamification - CAS2011
davidbonilla
81
5.4k
A designer walks into a library…
pauljervisheath
207
24k
Context Engineering - Making Every Token Count
addyosmani
1
37
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
What's in a price? How to price your products and services
michaelherold
246
12k
Git: the NoSQL Database
bkeepers
PRO
431
66k
Docker and Python
trallard
45
3.6k
The Cult of Friendly URLs
andyhume
79
6.6k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.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