Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Practical RxJava workshop v1

Practical RxJava workshop v1

A workshop to discover RxJava by migrating an application from classical blocking code to nonblocking reactive code. Skeleton for the workshop is on http://github.com/simonbasle/PracticalRx (just use it to practice the workshop).

Simon Baslé

April 16, 2015
Tweet

More Decks by Simon Baslé

Other Decks in Programming

Transcript

  1. Futures<T>? Callbacks? too easy to block (get) complex beyond 1

    level of composition no composition have you heared of Callback Hell?
  2. DocumentService.find("userId", new Callback<List<Document>>() { public void onSuccess(List<Document> result) { final

    List<String> jsonList = new ArrayList<String>(10); int taken = 0; for (Document doc : result) { if (taken >= 10) break; if (!doc.isStarred()) continue; taken++; final CountDownLatch rendezVous = new CountDownLatch(3); final JsonObject jsonBuffer = new JsonObject(); jsonBuffer.appendInt("id", doc.getId()); jsonBuffer.append("text", doc.getText()); CommentService.findForDoc(doc, new Callback<List<Comment>>() { public void onSuccess(List<Comment> comments) { final JsonObject commentArray = JsonObject.createArray(); CountDownLatch userLatch = new CountDownLatch(comments.size()); for (Comment c : comments) { JsonObject cj = new JsonObject(); cj.append("content", c.getText()); cj.append("date", c.getDate()); UserService.find(c.getUserId(), new Callback<User>() { public void onSuccess(User user) { cj.append("author", user.getName()); cj.append("nickname", user.getLogin()); Futures<T>? Callbacks? too easy to block (get) complex beyond 1 level of composition no composition have you heared of Callback Hell?
  3. Doge Mining Pool Client (UI?) Rest Controller Rest Controller Rest

    Controller Rest Controller Service Service Service External API External API External API DB L E G A C Y
  4. Doge Mining Pool Client (UI?) Rest Controller Rest Controller Rest

    Controller Rest Controller Service Service Service External API External API External API DB
  5. Doge Mining Pool Client (UI?) Rest Controller Rest Controller Rest

    Controller Rest Controller Service Service Service External API External API External API DB
  6. Migrate Services • UserService ◦ findAll for now naively adapted

    (Observable.from) ◦ compose on findAll for getUser / getUserByLogin • SearchService
  7. Useful Operators • filter • take • flatMap to asynchronously

    retrieve additional data needed for filter
  8. Migrate Services StatService.getAllStats • for each User ◦ retrieve his

    hashrate ◦ retrieve how many coins he mined ◦ combine both and make a UserStat
  9. The Problem StatService.lastBlockFoundBy ◦ fireproof implementation (choose a random User)

    ◦ but what if… we wanted to do it without knowing the number of users in advance?
  10. The Plan Generate an index randomly (within a broadly guessed

    limit) Retry up to 4 times if out of bounds Last resort: use a default User (eg. “Banned User”)
  11. Migrate Services ExchangeRateService ◦ calls two external APIs: doge-to-dollar rate

    and currency-to-currency exchange rate ◦ two chained REST calls ◦ combination of the two gives doge-to-anyCurrency
  12. Useful Operators • Observable.create ◦ wrap the REST call on

    each subscription ◦ catch RestClientException and make them into DogePoolException (especially for timeouts)
  13. Useful Operators • zipWith ◦ similar to zip but called

    from stream A instead of statically ◦ combine both rates to get the final one
  14. "this free exchange rate API crashes too often, make it

    so we switch to an alternative non-free API when it’s the case" - the Product Owner
  15. "this free exchange rate API crashes too often, make it

    so we switch to an alternative non-free API when it’s the case" - the Product Owner
  16. "oh yeah and if you could go on and track

    costs of these calls it would be great" - the Product Owner, pouring himself a coffee Much Surprise Such Requirements wow
  17. The Plan • AdminService ◦ add method+variable to track cost

    for this month • ExchangeRateService ◦ rate retrieval method similar to current one ◦ use endpoint from exchange.nonfree.api.baseUrl • switch thanks to OnErrorResumeNext • use side effects ◦ log when we switch & track the cost
  18. Asynchronous Response • prepare a DeferredResult<T> ◦ that will be

    returned without blocking • subscribe on stream ◦ onNext: inject T via setResult ◦ onError: create a DogePoolException and inject it via setErrorResult
  19. This could be Generalized • using ReturnValueHandler and a simple

    adapter of Observable to DeferredResult • each time an Observable is returned, it’ll be converted into this adapter ◦ WebAsyncUtils.getAsyncManager(...). startDeferredResultProcessing(...)
  20. Migrate Services • AdminController (simple) • IndexController (trickier) ◦ use

    zip, flatMap, single… to detect bad users • UserProfileController (idem)
  21. ➔ RxJava Logo and Marble Diagrams - ReactiveX Documentation http://reactivex.io

    ➔ Cat Attack - Static416 on Flickr ➔ Stormtroopers Series - J.D. Hancock http://photos.jdhancock.com/series/stormtroopers.html ➔ Blackhole with Corona - NASA on Wikimedia ➔ Broken Keyboard - Santeri Viinamäki on Wikimedia ➔ Gobot Toys- Tom Prankerd on Wikimedia ➔ Coffee Filter - Unsplash on Pixabay ➔ Abacus - Succo on Pixabay ➔ Still from Office Space - Copyright, 20th Century Fox By By-Nc By Cc0 By By Cc0 Cc0 © Credits (1/2)
  22. ➔ Hand Reaching - Kinseykick on Pixabay ➔ Sidecar Motocross

    - Jean-Daniel Echenard on Flickr ➔ Twin Falls - Blese on Flickr ➔ Tumbeast Gnawing on Servers - Matthew Inman on Wikimedia ➔ Trap - Anticiv on Flickr ➔ Kane Cleaning Supplies - Collinanderson on Flickr ➔ Chain - Stomchak on Wikimedia ➔ Takeaway - Edimburgh Blog on Flickr ➔ The End Sands - Elektro-Plan on Pixabay Cc0 By-Nd By-Nc By By-Sa By By By Cc0 Credits (2/2)