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

[London Java Community ] First steps with reactive Java programming

[London Java Community ] First steps with reactive Java programming

Have you ever heard of reactive programming? In this lecture we will cover the concepts of reactive programming, reactive streams specification, RxJava, Spring Webflux and mainly the Reactor library, including the use of Flux, Mono, Publisher, Subscriber and its operators, rsorcket and R2DBC and an example of the use of Spring webflux.


  1. None
  2. KAMILA SANTOS Backend Developer Microsoft MVP and community coordinator at

    DevsJavaGirl, WomakersCode e Perifacode

  4. REACTIVE PARADIGM It is a programming paradigm oriented to data

    / event flows, as well as their propagation asynchronously.
  5. REACTIVE PARADIGM Because it works asynchronously, it is highly recommended

    to handle large volumes of data that undergo major changes in real time.
  6. REACTIVE PARADIGM It is also widely used in social networks

    (front and back) because it works oriented to user actions that result in events.

  8. Message Driven Elastic Resilient Responsive

  9. Responsive The system responds in a timely manner, if possible

  10. RESILIENT The system remains responsive to failures

  11. Elastic The system remains responsive in the face of a

    variable workload.
  12. MESSAGE DRIVEN Reactive applications rely on asynchronous messaging to establish

    a boundary between components, ensuring flexible coupling, isolation and transparency
  13. RX JAVA

  14. RXJAVA It is an implementation for the Reactive Extensions JVM,

    which is an asynchronous and event-based library that works with observable sequences.
  15. RXJAVA It extends from the observer pattern -> each object

    called (subject), has a list of dependents (observers) that are automatically notified by the subject to each change of state through its operators and methods.
  16. RXJAVA Supports data sequences and events to compose your event

    strings in a declarative way.
  17. Map Transforms items emited by a Observable by applying a

    function to each item
  18. None
  19. Scan Apply a function to each item emitted by a

    Observable sequentially and emits each successive value.
  20. None
  21. Filter Emits only those items from an Observable that pass

    a predicate
  22. None

  24. REACTIVE STREAMS Initiative to provide a standard / guide /

    rules for asynchronous flow processing with NIO backpressure
  25. INTERFACE FLOW.PUBLISHER<T> methods to produce streams and other events

  26. None
  27. INTERFACE FLOW.SUBSCRIBER<T> methods for receiving streams and other events

  28. None
  29. INTERFACE FLOW.SUBSCRIPTION methods for linking Publisher and Subscriber.

  30. None
  31. INTERFACE FLOW.PROCESSOR<T,R> defines methods to do some advanced operations, like

    transforming items from publishers to subscribe
  32. None
  33. CLASS SUBMISSIONPUBLISHER<T> IMPLEMENTS FLOW.PUBLISHER<T> Flexible item producer, according to Reactive

  34. None
  35. REACTIVE STREAMS intended for runtime environments (JVM), as well as

    network protocols.https: //github.com/reactive-streams/reactive-streams-jvm
  36. REACTIVE STREAMS the main objective is to define the exchange

    of flow data through an asynchronous limit.
  37. REACTIVE STREAMS its scope is to find a minimal set

    of methods, interfaces and protocols that will describe the operations and entities necessary to have asynchronous data flows with NIO backpressure.
  38. Some Reactive Programming Concepts

  39. BACKPRESSURE Resistance or strength that opposes the desired data flow

    through the software. Feedback from the receiver to the producer that he is not supporting the load.
  40. STREAM Sequence of objects that supports various methods that can

    be operated to produce a result
  41. STREAM linked to a data source, a stream is capable

    of emitting three events:
  42. ONNEXT() represents some value, go to the next value in

    a list
  43. ONERROR() there was an error in execution

  44. ONCOMPLETE() in the case of finite events, it indicates that

  45. FLUX It can emit from 0 to N events, passing

    through OnNext (), OnComplete () and onError.
  46. None
  47. Mono It can emit from 0 to 1 events, passing

    through OnNext (), OnComplete () and onError.
  48. None
  49. OBSERVABLE an observable can pass messages asynchronously.

  50. SUBSCRIBER consumes the data received from the subscription

  51. PUBLISHER produces the data that will be consumed by the

  52. SUBSCRIPTION connection between Subscriber and Publisher

  53. None
  54. COLD OBSERVABLE the sequence of events is only executed if

    Observable has an associated subscriber
  55. HOT OBSERVABLE it broadcasts events regardless of whether there is

    an associated subscriber
  56. RSocket

  57. RSocket is a binary, point-to-point communication protocol intended for use

    in distributed applications
  58. RSocket It enables the following symmetric interaction models via async

    message passing over a single connection:
  59. RSocket 1) Request / Response (Stream of 1)

  60. RSocket 2) request/stream (finite stream of many)

  61. RSocket 3) fire-and-forget (no response)

  62. RSocket 4) channel (bi-directional streams)

  63. RSocket It supports the resumption of the session, that is,

    it allows the resumption of long-term flows in different transport connections.
  64. R2DBC

  65. R2DBC The relational Database Connectivity brings reactive programming APIs to

    relational databases
  66. R2DBC Based on the Reactive Streams specification

  67. R2DBC Provides an open specification.

  68. R2DBC Works with relational databases

  69. R2DBC Supports scalable solutions.


  71. SPRING WEBFLUX Spring WebFlux can be defined as a “parallel”

    version to the already known and widely used Spring MVC (servlet), having as main difference the support for reactive NIO streams and for supporting the concept of backpressure with the Netty server coming by default embedded in its architecture.
  72. SPRING WEBFLUX From version 5.0 of the Spring Framework we

    have a reactive part in addition to the Servlet structure that already existed, each module of these is optional, you can use the Servlet part, the reactive part or even both in your applications.

  74. SPRING WEBFLUX Spring Webflux was developed because we needed non-blocking

    applications that could work with a small number of threads simultaneously and that could be run with some hardware resources.
  75. SPRING WEBFLUX in Servlet 3.1, an NIO API was provided,

    but its use does not correspond to the rest of the API and to all the concepts behind Servlet, which has blocking contracts.
  76. SPRING WEBFLUX These factors were decisive for the development of

    a new API that was used independently of the execution time and in a non-blocking way, which was possible with the servers (Netty for example) that consolidated themselves in the asynchronous and non-blocking operation.
  77. SPRING WEBFLUX Another reason is that WebFlux makes it easier

    to understand and use concepts of functional / reactive programming. With the addition of Java 8 functional features and Flowable API / Reactive Streams in Java 9, which enabled Spring WebFlux to have functional endpoints and annotated controllers in applications.
  78. HOW DO REQUESTS WORK? This model, depending on the volume

    of requests and the way it was developed, can cause a slow application and even an Out Of Memory error (this type of error usually occurs when we keep objects for a long time or try to process a lot of data). once)
  79. None

  81. None
  82. None

  84. REACTOR It is a library based on Reactive Streams Specifications,

    it is completely non-blocking and interacts directly with the functional Java API (Stream, Duration and Completable Future), for composing elements using Flux and Mono in microservices architectures, offering backpressure mechanisms ready for TCP , UDP and HTTP (including web sockets).
  85. REACTOR Its operators and schedulers can support a large transfer

    volume (up to 10 million requests per second, according to Reactor documentation https://projectreactor.io/), it was also the first reactive library to implement some points suggested by reactive streams applications (https://github.com/reactor/reactive-streams-commons), which were later also implemented by RxJava2.
  86. REACTOR Its modules are abstract and interoperable reactive flows to

    facilitate their development, and can be used with: Spring FrameworksDrivers and clients (for example CloudFoundry Java Client https://github.com/cloudfoundry/cf-java-client)- in protocols / contracts like R2DBC (https://r2dbc.io/) and RSocket (https://rsocket.io/), for example.
  87. REACTOR CORE is the main part of this library, the

    other modules are dependent on it, it provides reactive types that implement a Publisher that provides Flux and Mono
  88. REACTOR NETTY is the server for our NIO application, used

    for developing highly scalable servers.
  89. Benefits

  90. Benefits non-blocking, which generates a certain performance gain.

  91. Benefits fewer threads, less used / spent memory

  92. Benefits follows the principles of the reactive manifest: responsive, resilient,

    elastic and message-driven applications.
  93. Problems

  94. Problems A different form of programming (declarative programming)

  95. None
  96. None
  97. None
  98. None
  99. None
  100. None
  101. References https://www.baeldung.com/rx-java https://github.com/reactive-streams/reactive-streams-jvm https://timurisachenko.com/experiment-3-reactive-spring-webflux-r2dbc-todo -rest-api/ https://www.baeldung.com/rsocket https://rsocket.io/ https://r2dbc.io/

  102. References https://tech.io/playgrounds/929/reactive-programming-with-reactor-3/transform https://www.callicoder.com/reactive-rest-apis-spring-webflux-reactive-mongo/ http://reactivex.io/languages.html https://projectreactor.io/ http://www.trieu.xyz/2019/04/netty-cookbook.html https://dzone.com/articles/build-a-simple-netty-application-with-and-without https://www.baeldung.com/netty

  103. References hps://tech.io/playgrounds/929/reactive-programming-with-reactor-3/transform https://www.callicoder.com/reactive-rest-apis-spring-webflux-reactive-mongo/ http://reactivex.io/languages.html https://projectreactor.io/ http://www.trieu.xyz/2019/04/netty-cookbook.html https://dzone.com/articles/build-a-simple-netty-application-with-and-without https://www.baeldung.com/netty

  104. Thanks Linkedin https://www.linkedin.com /in/kamila-santos-oliveira / Twitter @kamilah_santos