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.

More Decks by Kamila de fatima santos oliveira

Other Decks in Programming

Transcript

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

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

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

    (front and back) because it works oriented to user actions that result in events.
  4. MESSAGE DRIVEN Reactive applications rely on asynchronous messaging to establish

    a boundary between components, ensuring flexible coupling, isolation and transparency
  5. RXJAVA It is an implementation for the Reactive Extensions JVM,

    which is an asynchronous and event-based library that works with observable sequences.
  6. 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.
  7. Scan Apply a function to each item emitted by a

    Observable sequentially and emits each successive value.
  8. REACTIVE STREAMS Initiative to provide a standard / guide /

    rules for asynchronous flow processing with NIO backpressure
  9. REACTIVE STREAMS intended for runtime environments (JVM), as well as

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

    of flow data through an asynchronous limit.
  11. 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.
  12. 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.
  13. FLUX It can emit from 0 to N events, passing

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

    through OnNext (), OnComplete () and onError.
  15. COLD OBSERVABLE the sequence of events is only executed if

    Observable has an associated subscriber
  16. RSocket It supports the resumption of the session, that is,

    it allows the resumption of long-term flows in different transport connections.
  17. 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.
  18. 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.
  19. 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.
  20. 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.
  21. 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.
  22. 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.
  23. 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)
  24. 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).
  25. 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.
  26. 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.
  27. 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
  28. REACTOR NETTY is the server for our NIO application, used

    for developing highly scalable servers.