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...
Expectations
● Goals
– Why is reactive programming important?
– What are RxJava and Vert.x?
– How can I combine them to bu...
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
– Predictab...
Vert.x
What is Vert.x
● A toolkit (lib)
● … to build polyglot (on the JVM)
– JS, Groovy, Ruby, Scala (3.4), Kotlin (3.4)
● … reac...
Demo
Damn simple verticle
Multi-Reactor
#RivieraDev @vertx_project
Event Bus
● Nervous system of Vert.x
● Message passing style
● Different messaging paradigms:
– Point to point
– Publish/S...
Running blocking code
● In the real world, most JVM libraries have blocking APIs
– JDBC
– io.File
– Your legacy libs
● Wit...
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 ...
Push based
Observable Observer
Subscription
● OnNext 0..∞
● Terminal event 0..1
– OnComplete
– OnError
#RivieraDev @vertx_...
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>
#Riviera...
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
Ver...
Observable#map
ReactiveX.io Creative Commons Attribution 3.0 License
Observable#map
private Single<JsonArray> findAlbums(SQLConnection sqlConnection, Long artistId) {
return sqlConnection.rxQ...
Observable#flatMap
ReactiveX.io Creative Commons Attribution 3.0 License
Single#flatMap
public void handle(RoutingContext rc) {
dbClient.rxGetConnection().flatMap(sqlConnection -> {
return findGe...
Single#zip
ReactiveX.io Creative Commons Attribution 3.0 License
Single#zip
Single<JsonObject> as = findAlbum(sqlConnection, albumId);
Single<JsonArray> ts = findTracks(sqlConnection, alb...
Observable#observeOn
ReactiveX.io Creative Commons Attribution 3.0 License
Observable#observeOn
#RivieraDev @vertx_project
albumCommentsBucket.query(Query.parametrized(findRecentCommentsByAlbum, pa...
#RivieraDev @vertx_project
http://red.ht/2pgMEoA
Thank you!
http://vertx.io
Come get your sticker!
#RivieraDev @vertx_project
Modern app programming with RxJava and Eclipse Vert.x
Modern app programming with RxJava and Eclipse Vert.x
Prochain SlideShare
Chargement dans…5
×

Modern app programming with RxJava and Eclipse Vert.x

732 vues

Publié le

With the advent of mobile web and IoT (Internet of Things), today's applications need to handle a lot of concurrent requests while staying responsive and easy to scale. Infrastructure plays a role in achieving these goals, but changing your programming style can help too.

In this presentation, we will make the case for reactive programming, and introduce RxJava in particular. Then we will guide you through the process of building a real-life web application, based on Vert.x 3 and RxJava, communicating with databases, external services and modern datastores. You will learn how Vert.x integrates with RxJava, and why its simplicity makes it a perfect runtime for reactive applications.

Publié dans : Logiciels
0 commentaire
2 j’aime
Statistiques
Remarques
  • Soyez le premier à commenter

Aucun téléchargement
Vues
Nombre de vues
732
Sur SlideShare
0
Issues des intégrations
0
Intégrations
2
Actions
Partages
0
Téléchargements
17
Commentaires
0
J’aime
2
Intégrations 0
Aucune incorporation

Aucune remarque pour cette diapositive

Modern app programming with RxJava and Eclipse Vert.x

  1. 1. Modern app programming with RxJava & Eclipse Vert.x
  2. 2. 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
  3. 3. 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
  4. 4. Back to the late 90s… A hotel room booking engine
  5. 5. http://example.com http://example.com http://example.com
  6. 6. Such apps are mainstream today! ● Microservices ● API economy #RivieraDev @vertx_project
  7. 7. Alexandre Duret-Lutz (CC BY-SA 2.0)
  8. 8. Event loop systems
  9. 9. Reactor pattern Single thread
  10. 10. New stuff? ● Browser (AJAX, #setTimeout) ● GUI ● Node.js #RivieraDev @vertx_project
  11. 11. Event loop benefits ● Simple concurrency model ● Mechanical sympathy http://bit.ly/2cJMBsG – Higher throughput – Predictable latency #RivieraDev @vertx_project
  12. 12. Vert.x
  13. 13. 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
  14. 14. Demo Damn simple verticle
  15. 15. Multi-Reactor #RivieraDev @vertx_project
  16. 16. Event Bus ● Nervous system of Vert.x ● Message passing style ● Different messaging paradigms: – Point to point – Publish/Subscribe – Request/Reply #RivieraDev @vertx_project
  17. 17. 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
  18. 18. Callback based (reactive imperative)
  19. 19. CALLBACK HELL !
  20. 20. RxJava
  21. 21. 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
  22. 22. Push based Observable Observer Subscription ● OnNext 0..∞ ● Terminal event 0..1 – OnComplete – OnError #RivieraDev @vertx_project
  23. 23. Reactive pull backpressure Observable Observer Subscription Request #RivieraDev @vertx_project
  24. 24. Observable / Single / Completable Zero One Many Sync void T Iterable<T> Async Completable Single<T> Observable<T> #RivieraDev @vertx_project
  25. 25. Demo Damn simple Rx Verticle
  26. 26. Music Store Demo https://github.com/tsegismont/vertx-musicstore
  27. 27. 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
  28. 28. Observable#map ReactiveX.io Creative Commons Attribution 3.0 License
  29. 29. 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
  30. 30. Observable#flatMap ReactiveX.io Creative Commons Attribution 3.0 License
  31. 31. 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
  32. 32. Single#zip ReactiveX.io Creative Commons Attribution 3.0 License
  33. 33. 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
  34. 34. Observable#observeOn ReactiveX.io Creative Commons Attribution 3.0 License
  35. 35. 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);
  36. 36. #RivieraDev @vertx_project http://red.ht/2pgMEoA
  37. 37. Thank you! http://vertx.io Come get your sticker! #RivieraDev @vertx_project

×