Reactive Spring Workshop @Javaland

Reactive Spring Workshop @Javaland

Slides introducing reactive programming with Spring and Reactor at the Javaland "Reactive Spring" workshop, given March 2017.

Fda20bf9d9c85c4390ca7237beba45a2?s=128

Simon Baslé

March 28, 2017
Tweet

Transcript

  1. Reactive Spring Workshop

  2. Reactive Programming 101 what does it bring to the table?

  3. WHY?

  4. WHY? because blocking is evil

  5. sync/blocking main thread processing resumes I/O ! app does nothing

  6. sync/blocking main thread processing resumes I/O BAD ! app does

    nothing
  7. async & blocking main thread wait & join ! new

    threads, costly ! complex
  8. async & blocking main thread wait & join ! new

    threads, costly ! complex BAD
  9. async & nonblocking “event loop” in non-blocking processin g chunks

    no more threads than needed
  10. how do you achieve that without losing your mind ?

  11. Reactive Programming

  12. Composing asynchronous & event-based sequences, using non-blocking operators “ ”

  13. without sacrifice

  14. without sacrifice Callbacks ? Futures ? easy to block hard

    to compose callback hell ! not readable
  15. Pull? Push!

  16. Pull? Push!

  17. vs Iterable - Iterator Publisher - Subscriber

  18. Data in Flux

  19. Publisher Subscriber push events produces consumes feedback

  20. interfaces from Reactive Streams spec Publisher Subscriber feedback consumes push

    events produces
  21. Publisher Subscriber push events produces consumes feedback Subscriber<T> onNext(T) onComplete();

    onError(Throwable);
  22. Publisher Subscriber produces consumes feedback 0..N elements + 0..1 (complete

    | error)
  23. Publisher Subscriber push events produces consumes feedback backpressure

  24. Publisher Subscriber push events produces consumes feedback can I have

    an API though?
  25. Publisher Subscriber push events produces consumes feedback

  26. Reactor 3 types and operators

  27. Flux<T> for 0..N elements

  28. None
  29. Mono<T> for at most 1 element

  30. None
  31. Reactive Streams all the way

  32. focus on Java 8

  33. focus on Java 8 Duration, CompletableFuture, Streams

  34. an Rx-inspired API with a vocabulary of operators similar to

    RxJava...
  35. an Rx-inspired API ...but not exactly the same

  36. Flux/Mono generator operator operator operator nothing happens until you subscribe

  37. Flux/Mono generator Subscriber operator operator operator nothing happens until you

    subscribe
  38. Flux/Mono generator Subscriber operator operator operator per Subscription state Sub

    Sub Sub
  39. Flux/Mono generator Subscriber operator operator operator data flows Sub Sub

    Sub
  40. “elements of functional programming”

  41. threading contexts

  42. Reactor is agnostic

  43. however it facilitates switching

  44. Schedulers

  45. Schedulers elastic, parallel, single, timer...

  46. publishOn switch rest of the flux on a thread

  47. subscribeOn make the subscription and request happen on a particular

    thread
  48. Flux/Mono generator operator subscribeOn operator publishOn operator operator Subscriber Sub

    Sub Sub Sub Sub Sub
  49. Flux/Mono generator operator subscribe On operator publish On operator operator

    Subscriber Sub Sub Sub Sub Sub Sub
  50. Flux/Mono generator operator subscribeOn operator publishOn operator operator Subscriber Sub

    Sub Sub Sub Sub Sub
  51. Flux/Mono generator operator subscribeOn operator publishOn operator operator Subscriber Sub

    Sub Sub Sub Sub Sub
  52. Flux/Mono generator operator subscribeOn operator publishOn operator operator Subscriber Sub

    Sub Sub Sub Sub Sub
  53. Flux/Mono generator operator subscribeOn operator publishOn operator operator Subscriber Sub

    Sub Sub Sub Sub Sub
  54. CODING TIME!

  55. None
  56. Bonus Slides operator step-by-step examples, backpressure

  57. map, filter, buffer

  58. Flux.range Subscriber map filter buffer

  59. None
  60. None
  61. None
  62. Flux.range(5, 3) .map(i -> i + 3) .filter(i -> i

    % 2 == 0) .buffer(3)
  63. Flux.range Subscriber map filter buffer

  64. Flux.range Subscriber map filter buffer

  65. Flux.range Subscriber map filter buffer

  66. Flux.range Subscriber map filter buffer

  67. Flux.range Subscriber map filter buffer

  68. Flux.range Subscriber map filter buffer

  69. Flux.range Subscriber map filter buffer

  70. Flux.range(5, 3) .map(i -> i + 3) .filter(i -> i

    % 2 == 0) .buffer(3) 5, 6, 7 | 8, 9, 10 | 8, 10 | [8,10]|
  71. retry

  72. Flux.from Subscriber map filter retry Publisher from HTTP reactive client

  73. None
  74. Flux.from Subscriber map filter retry Publisher from HTTP reactive client

  75. Flux.from Subscriber map filter retry Publisher from HTTP reactive client

    resubscribe
  76. Trickier: flatMap async sub-processes

  77. None
  78. flatMap(user -> tweetStream(user))

  79. flatMap(user -> tweetStream(user))

  80. flatMap(user -> tweetStream(user))

  81. flatMap(user -> tweetStream(user))

  82. None
  83. & much more...

  84. backpressure the "volume control"

  85. Publisher Subscriber subscribe

  86. Publisher Subscriber push data as fast as possible

  87. Publisher Subscriber subscribe with small request (eg. 1)

  88. Publisher Subscriber 1 onNext

  89. Publisher Subscriber request more (eg. 2)

  90. Publisher Subscriber 2 onNext

  91. Publisher Subscriber backpressure

  92. other ways of dealing with backpressure eg. drop, buffer...

  93. Credits ! Robot Devil: copyright FOX ! Volume Knob: CC0

    (via Pixabay) ! Camel Shape: CC0 (via Pixabay) ! Dromedary Shape: CC-By-SA USPN,Whidou (via Wikimedia) ! Thread Balls: CC0 (via Pixabay) ! Coding Time: derived from KEMUDA Computer Lab (CC-By-SA Andy.aug, via Wikimedia) ! Sound Table: CC0 (via Pexels) ! Dam: CC-By-SA Matthew Hatton (via geograph.org.uk) ! logos: Pivotal, Spring, Twitter and Github logo copyright their respective companies.