RxJava - Getting Started

F31c7fbcbb0766d0632d96fd7e74b649?s=47 Brice Dutheil
September 09, 2014

RxJava - Getting Started

Nowadays applications are getting increasingly interconnected. Web Oriented Architecture or Micro-Service Architecture for example show there's a growing trend that integrating multiple different web services at different scale.

How to compose or aggregate all of these interactions in a usable result ; but especially how to implement it in a meaningful and readable code, with loose coupling and without temporal coupling ?

RxJava offers an elegant way to write synchronous and asynchronous code with enhanced composition capabilities. In this lightning talk we will present basic concepts of Rx (Reactive eXtensions).

David Wursteisen - Brice Dutheil / Human Talks Paris / September 9th 2014
Video of this presentation is on parleys in French :
https://www.parleys.com/tutorial/rxjava-getting-started-1

F31c7fbcbb0766d0632d96fd7e74b649?s=128

Brice Dutheil

September 09, 2014
Tweet

Transcript

  1. RxJava Getting Started Brice Dutheil / David Wursteisen

  2. Brice Dutheil @BriceDutheil Indep, Devoxx France, Hackergarten Paris Qui sommes

    nous ? David Wursteisen @dwursteisen
  3. Il était une fois...

  4. Il était une fois...

  5. Systèmes distribués

  6. Systèmes distribués

  7. Systèmes distribués

  8. Systèmes distribués

  9. Systèmes distribués The Internet

  10. Systèmes distribués

  11. Systèmes distribués

  12. RxJava

  13. Obervable

  14. Obervable

  15. Obervable

  16. Obervable

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

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

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

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

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

    TimeUnit.SECONDS) Observable.create(...)
  22. 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() ...
  23. Observable Observable<Message> Observable<Result> Observable<Interaction>

  24. Subscriber Observable.range(1, 4400)

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

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

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

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

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

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

  31. Diagrammes Marble

  32. Diagrammes Marble

  33. Diagrammes Marble

  34. Diagrammes Marble

  35. Diagrammes Marble

  36. Diagrammes Marble

  37. Diagrammes Marble

  38. Diagrammes Marble

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

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

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

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

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

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

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

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

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

  48. Opérateurs Observable.from(1, 2, 3, 4) .map((i) -> i * 10)

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

    .subscribe(System.out::println); 10 20 30 40
  50. Composition

  51. Composition

  52. Composition

  53. Composition couleurs formes

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

    ” + n)) .subscribe(to_the_view())
  55. Programmation synchrone main

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

    ” + n)) .subscribe(to_the_view())
  57. Programmation asynchrone Computation IO UI main source

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

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

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

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

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

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

  64. Programmation asynchrone observableReadingSynchronously(“strings.txt”) .subscribeOn(scheduler) .take(10) .delay(1, SECONDS, scheduler) .map(parse()) .observeOn(scheduler)

    .map(n -> “=> ” + n)) .subscribe(to_the_view())
  65. 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())
  66. Aller plus loin Séquence finie & infinie Unsubscribe BackPressure Reactive

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

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

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

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

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

    Streams Hot & Cold observable
  72. Questions ?