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

RxJava - Getting Started

David
September 09, 2014

RxJava - Getting Started

Les applications sont de plus en plus interconnectées. Une architecture type WOA (Web Oriented Archiecture) et l’utilisation des micros-services nécessitent de faire de plus en plus d’appel à différents services web. Comment composer un résultat à partir de ces différents services, sans avoir à ce soucier de l’ordre dans lequel les serveurs vont répondre ?

RxJava offre une manière élégante de faire de l’asynchrone et de la composition au sein de son application. Nous vous exposerons les concepts de Rx (Reactive eXtension) avant de vous montrer une mise en application.

David Wursteisen - Brice Dutheil / Human Talks Paris / 9 septembre 2014

Video link of this presentation : https://parleys.com/play/5496fd23e4b0c9badd302bd7/chapter0/about

David

September 09, 2014
Tweet

More Decks by David

Other Decks in Programming

Transcript

  1. RxJava
    Getting Started
    Brice Dutheil / David Wursteisen

    View full-size slide

  2. Brice Dutheil
    @BriceDutheil
    Indep, Devoxx France,
    Hackergarten Paris
    Qui sommes nous ?
    David Wursteisen
    @dwursteisen

    View full-size slide

  3. Il était une fois...

    View full-size slide

  4. Il était une fois...

    View full-size slide

  5. Systèmes distribués

    View full-size slide

  6. Systèmes distribués

    View full-size slide

  7. Systèmes distribués

    View full-size slide

  8. Systèmes distribués

    View full-size slide

  9. Systèmes distribués
    The Internet

    View full-size slide

  10. Systèmes distribués

    View full-size slide

  11. Systèmes distribués

    View full-size slide

  12. Observable
    Observable.from(1, 2, 3, 4)

    View full-size slide

  13. Observable
    Observable.from(1, 2, 3, 4)
    Observable.from(asyncTask.getFuture())

    View full-size slide

  14. Observable
    Observable.from(1, 2, 3, 4)
    Observable.from(asyncTask.getFuture())
    Observable.range(1, 4400)

    View full-size slide

  15. Observable
    Observable.from(1, 2, 3, 4)
    Observable.from(asyncTask.getFuture())
    Observable.range(1, 4400)
    Observable.timer(0, 5, TimeUnit.SECONDS)

    View full-size slide

  16. Observable
    Observable.from(1, 2, 3, 4)
    Observable.from(asyncTask.getFuture())
    Observable.range(1, 4400)
    Observable.timer(0, 5, TimeUnit.SECONDS)
    Observable.create(...)

    View full-size slide

  17. Observable
    Observable.from(1, 2, 3, 4)
    Observable.from(asyncTask.getFuture())
    Observable.range(1, 4400)
    Observable.timer(0, 5, TimeUnit.SECONDS)
    Observable.create(...)
    Observable.from(anIterable)
    Observable.just(73)
    Observable.error(new Exception())
    Observable.empty()
    ...

    View full-size slide

  18. Observable
    Observable
    Observable
    Observable

    View full-size slide

  19. Subscriber
    Observable.range(1, 4400)

    View full-size slide

  20. Subscriber
    Observable.range(1, 4400)
    .subscribe(
    onNext
    )

    View full-size slide

  21. Subscriber
    Observable.range(1, 4400)
    .subscribe(
    System.out::println
    )

    View full-size slide

  22. Subscriber
    Observable.range(1, 4400)
    .subscribe(
    System.out::println,
    onError
    )

    View full-size slide

  23. Subscriber
    Observable.range(1, 4400)
    .subscribe(
    System.out::println,
    System.err::println
    )

    View full-size slide

  24. Subscriber
    Observable.range(1, 4400)
    .subscribe(
    System.out::println,
    System.err::println,
    onCompleted
    )

    View full-size slide

  25. Subscriber
    Observable.range(1, 4400)
    .subscribe(
    System.out::println,
    System.err::println,
    () -> System.out.println(“finished”)
    )

    View full-size slide

  26. Diagrammes Marble

    View full-size slide

  27. Diagrammes Marble

    View full-size slide

  28. Diagrammes Marble

    View full-size slide

  29. Diagrammes Marble

    View full-size slide

  30. Diagrammes Marble

    View full-size slide

  31. Diagrammes Marble

    View full-size slide

  32. Diagrammes Marble

    View full-size slide

  33. Diagrammes Marble

    View full-size slide

  34. Opérateurs
    Observable.from(1, 2, 3, 4)

    View full-size slide

  35. Opérateurs
    Observable.from(1, 2, 3, 4)
    .filter(predicate)

    View full-size slide

  36. Opérateurs
    Observable.from(1, 2, 3, 4)
    .filter((i) -> (i % 2) == 0)

    View full-size slide

  37. Opérateurs
    Observable.from(1, 2, 3, 4)
    .filter((i) -> (i % 2) == 0)
    .subscribe(System.out::println)

    View full-size slide

  38. Opérateurs
    Observable.from(1, 2, 3, 4)
    .filter((i) -> (i % 2) == 0)
    .subscribe(System.out::println)

    View full-size slide

  39. Opérateurs
    Observable.from(1, 2, 3, 4)
    .filter((i) -> (i % 2) == 0)
    .subscribe(System.out::println)
    2

    View full-size slide

  40. Opérateurs
    Observable.from(1, 2, 3, 4)
    .filter((i) -> (i % 2) == 0)
    .subscribe(System.out::println)
    2
    4

    View full-size slide

  41. Opérateurs
    Observable.from(1, 2, 3, 4)
    .take(2)
    .subscribe(System.out::println);

    View full-size slide

  42. Opérateurs
    Observable.from(1, 2, 3, 4)
    .take(2)
    .subscribe(System.out::println);
    1
    2

    View full-size slide

  43. Opérateurs
    Observable.from(1, 2, 3, 4)
    .map((i) -> i * 10)
    .subscribe(System.out::println);

    View full-size slide

  44. Opérateurs
    Observable.from(1, 2, 3, 4)
    .map((i) -> i * 10)
    .subscribe(System.out::println);
    10
    20
    30
    40

    View full-size slide

  45. Composition
    couleurs
    formes

    View full-size slide

  46. Programmation synchrone
    observableReadingSynchronously(“strings.txt”)
    .take(10)
    .delay(1, SECONDS)
    .map(parse())
    .map(n -> “=> ” + n))
    .subscribe(to_the_view())

    View full-size slide

  47. Programmation synchrone
    main

    View full-size slide

  48. Programmation synchrone
    observableReadingSynchronously(“strings.txt”)
    .take(10)
    .delay(1, SECONDS)
    .map(parse())
    .map(n -> “=> ” + n))
    .subscribe(to_the_view())

    View full-size slide

  49. Programmation asynchrone
    Computation
    IO
    UI
    main
    source

    View full-size slide

  50. Programmation asynchrone
    Computation
    IO
    UI
    main
    source
    ...
    .subscribeOn(Schedulers.io())

    View full-size slide

  51. Programmation asynchrone
    Computation
    IO
    UI
    main
    source
    .take(...)
    .subscribeOn(Schedulers.io())

    View full-size slide

  52. Programmation asynchrone
    Computation
    IO
    UI
    main
    .delay(..., Schedulers.computation())

    View full-size slide

  53. Programmation asynchrone
    Computation
    IO
    UI
    main
    .delay(..., Schedulers.computation())
    .map(...)

    View full-size slide

  54. Programmation asynchrone
    Computation
    IO
    UI
    main
    .observeOn(Schedulers.from(...))
    .map(...)

    View full-size slide

  55. Programmation asynchrone
    Computation
    IO
    UI
    main
    .observeOn(Schedulers.from(...))
    .map(...)
    .subscribe(...)

    View full-size slide

  56. Programmation asynchrone
    observableReadingSynchronously(“strings.txt”)
    .subscribeOn(scheduler)
    .take(10)
    .delay(1, SECONDS, scheduler)
    .map(parse())
    .observeOn(scheduler)
    .map(n -> “=> ” + n))
    .subscribe(to_the_view())

    View full-size slide

  57. Programmation asynchrone
    observableReadingSynchronously(“strings.txt”)
    .subscribeOn(Schedulers.io())
    .take(10)
    .delay(1, SECONDS, Schedulers.computation())
    .map(parse())
    .observeOn(Schedulers.from(uiExecutor()))
    .map(n -> “=> ” + n))
    .subscribe(to_the_view())

    View full-size slide

  58. Aller plus loin
    Séquence
    finie & infinie
    Unsubscribe
    BackPressure
    Reactive Streams
    Hot & Cold
    observable

    View full-size slide

  59. Aller plus loin
    Séquence
    finie & infinie
    Unsubscribe
    BackPressure
    Reactive Streams
    Hot & Cold
    observable

    View full-size slide

  60. Aller plus loin
    Séquence
    finie & infinie
    Unsubscribe
    BackPressure
    Reactive Streams
    Hot & Cold
    observable

    View full-size slide

  61. Aller plus loin
    Séquence
    finie & infinie
    Unsubscribe
    BackPressure
    Reactive Streams
    Hot & Cold
    observable

    View full-size slide

  62. Aller plus loin
    Séquence
    finie & infinie
    Unsubscribe
    BackPressure
    Reactive Streams
    Hot & Cold
    observable

    View full-size slide

  63. Aller plus loin
    Séquence
    finie & infinie
    Unsubscribe
    BackPressure
    Reactive Streams
    Hot & Cold
    observable

    View full-size slide