SlideShare a Scribd company logo
Modern app programming
with RxJava & Eclipse Vert.x
Who am I?
● Thomas Segismont
● tsegismont on GitHub, Twitter, Gmail, Freenode...
● Vert.x core team since August 2016
● At Red Hat since November 2012 (RHQ and Hawkular)
#RivieraDev @vertx_project
Expectations
● Goals
– Why is reactive programming important?
– What are RxJava and Vert.x?
– How can I combine them to build scalable, efficient and resilient
apps?
● Non goals
– In-depth study of RxJava or Vert.x
#RivieraDev @vertx_project
Back to the late 90s…
A hotel room booking engine
http://example.com
http://example.com
http://example.com
Such apps are mainstream today!
● Microservices
● API economy
#RivieraDev @vertx_project
Alexandre Duret-Lutz (CC BY-SA 2.0)
Event loop systems
Reactor pattern
Single
thread
New stuff?
● Browser (AJAX, #setTimeout)
● GUI
● Node.js
#RivieraDev @vertx_project
Event loop benefits
● Simple concurrency model
● Mechanical sympathy http://bit.ly/2cJMBsG
– Higher throughput
– Predictable latency
#RivieraDev @vertx_project
Vert.x
What is Vert.x
● A toolkit (lib)
● … to build polyglot (on the JVM)
– JS, Groovy, Ruby, Scala (3.4), Kotlin (3.4)
● … reactive systems
#RivieraDev @vertx_project
Demo
Damn simple verticle
Modern app programming with RxJava and Eclipse Vert.x
Multi-Reactor
#RivieraDev @vertx_project
Event Bus
● Nervous system of Vert.x
● Message passing style
● Different messaging paradigms:
– Point to point
– Publish/Subscribe
– Request/Reply
#RivieraDev @vertx_project
Running blocking code
● In the real world, most JVM libraries have blocking APIs
– JDBC
– io.File
– Your legacy libs
● With Vert.x you can interact with blocking parts easily
● You won’t throw away your code assets!
#RivieraDev @vertx_project
Modern app programming with RxJava and Eclipse Vert.x
Callback based (reactive imperative)
CALLBACK HELL !
RxJava
Data and events flows
● It is great at organizing transformation of data and coordination
of events
● It makes most sense when many sources of events are
involved (modern apps!)
#RivieraDev @vertx_project
Push based
Observable Observer
Subscription
● OnNext 0..∞
● Terminal event 0..1
– OnComplete
– OnError
#RivieraDev @vertx_project
Reactive pull backpressure
Observable Observer
Subscription
Request
#RivieraDev @vertx_project
Observable / Single / Completable
Zero One Many
Sync void T Iterable<T>
Async Completable Single<T> Observable<T>
#RivieraDev @vertx_project
Demo
Damn simple Rx Verticle
Music Store Demo
https://github.com/tsegismont/vertx-musicstore
http://example.com
http://example.com
http://example.com
https://coverartarchive.org/
Postgres
Couchbase
Vert.x RxJava
Vert.x
Web Client
HTTP
Vert.x
JDBC Client
Couchbase
RxJava driver
Event Bus Bridge
(WebSocket)
Vert.x
Local Cache
Observable#map
ReactiveX.io Creative Commons Attribution 3.0 License
Observable#map
private Single<JsonArray> findAlbums(SQLConnection sqlConnection, Long artistId) {
return sqlConnection.rxQueryStreamWithParams(findAlbumsByArtist, new JsonArray().add(artistId))
.flatMapObservable(SQLRowStream::toObservable)
.map(row -> new JsonObject().put("id", row.getLong(0)).put("title", row.getString(1)))
.collect(JsonArray::new, JsonArray::add)
.toSingle();
}
#RivieraDev @vertx_project
Observable#flatMap
ReactiveX.io Creative Commons Attribution 3.0 License
Single#flatMap
public void handle(RoutingContext rc) {
dbClient.rxGetConnection().flatMap(sqlConnection -> {
return findGenres(sqlConnection).doAfterTerminate(sqlConnection::close);
}).flatMap(genres -> {
rc.put("genres", genres);
return templateEngine.rxRender(rc, "templates/index");
}).subscribe(rc.response()::end, rc::fail);
}
#RivieraDev @vertx_project
Single#zip
ReactiveX.io Creative Commons Attribution 3.0 License
Single#zip
Single<JsonObject> as = findAlbum(sqlConnection, albumId);
Single<JsonArray> ts = findTracks(sqlConnection, albumId);
return Single.zip(as, ts, (album, tracks) -> {
Map<String, Object> data = new HashMap<>(2);
data.put("album", album);
data.put("tracks", tracks);
return data;
}).doAfterTerminate(sqlConnection::close);
#RivieraDev @vertx_project
Observable#observeOn
ReactiveX.io Creative Commons Attribution 3.0 License
Observable#observeOn
#RivieraDev @vertx_project
albumCommentsBucket.query(Query.parametrized(findRecentCommentsByAlbum, params))
.observeOn(RxHelper.scheduler(rc.vertx()))
.flatMap(AsyncQueryResult::rows)
.limit(5)
.collect(JsonArray::new, (jsonArray, row) -> jsonArray.add(new JsonObject(row.value().toMap())))
.toSingle()
.flatMap(data -> {
rc.put("comments", data);
return templateEngine.rxRender(rc, "templates/partials/album_comments");
}).subscribe(rc.response()::end, rc::fail);
#RivieraDev @vertx_project
http://red.ht/2pgMEoA
Thank you!
http://vertx.io
Come get your sticker!
#RivieraDev @vertx_project

More Related Content

Modern app programming with RxJava and Eclipse Vert.x