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

Spring 5.0 Meets Reactive Programming

Spring 5.0 Meets Reactive Programming

Talk about new Spring 5.0 features and Reactive Programming

Transcript

  1. Spring 5.0 Meets Reactive Programming

  2. Hello! Cláudio E. de Oliveira Architect and Software Developer at

    @sensedia 2
  3. “ Launched at February S5BEEB50 - 50% OFF 24th April

    3
  4. Agenda ⊷ Why Spring Boot ⊷ Reactive Programming ⊷ Project

    Reactor ⊷ Spring Boot 2 ⊷ New Features ⊷ Spring Cloud Projects 4
  5. Spring Boot Why Spring Boot became a huge success??? 5

  6. ⊷ Stand-alone applications ⊷ Starter POMs - Production Friendly ⊷

    Automatically configurations (a.k.a JPA, Brokers) ⊷ No code generation and NO XML anymore ⊷ Production ready features metrics and monitoring Spring Boot Main Features 6
  7. A lot of sub-projects Spring Data 7 Spring AMQP Spring

    Framework Spring Cloud GCP Spring Cloud Spring Cloud Data Flow Spring Security Spring Integration Spring Messaging
  8. Reactive Programming The JVM Scenario 8

  9. 9 JVM Players

  10. Project Reactor Reactive library implementation for Reactive Streams Specification 10

  11. Motivation 11 “Reactor is a fourth-generation Reactive library for building

    non-blocking applications on the JVM based on the Reactive Streams Specification” https://projectreactor.io/
  12. Why not RXjava?? 12

  13. Let’s review some concepts Reactive Core Reactor is a fully

    non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API, Completable Future, Stream and Duration. Non Blocking IO Suited for Microservices Architecture, Reactor offers backpressure-ready network engines for HTTP (including Websockets), TCP and UDP. Reactive Encoding/Decoding is fully supported. 13
  14. 14 Reactor Types MONO [0|1] FLUX [N]

  15. 15 Reactor Types public Mono<Flight> flight(String id) { return this.flightRepository.findById(id);

    } public Flux<Flight> flights() { return this.flightRepository.findAll(); } Samples
  16. Spring Boot 2 What is coming??? 16

  17. 17

  18. 18 ⊷ Simpler Code, more readable ⊷ Focus on Business

    logic ⊷ Stream processing implies memory efficient ⊷ Flow API Java 9
  19. 19

  20. 20 Event-Loop

  21. 21

  22. 22

  23. Simple Reactive Controller // omitted imports @Bean public RouterFunction<ServerResponse> routes()

    { return route(GET("/api/goodbye"), serverRequest -> ok().body(fromPublisher(goodbye(),String.class))) .andRoute(GET("/api/nap"), request -> ok().body(fromPublisher(nap(),String.class))); } 23
  24. Reactive WebClient // omitted imports public Mono<Flight> flight(String id) {

    return discoveryService.serviceAddressFor(this.flightService).next().flatMap( address -> this.webClient.mutate() .baseUrl(address + "/" + this.flightServiceApiPath + "/" + id).build().get().exchange() .flatMap(clientResponse -> clientResponse.bodyToMono(Flight.class))); } 24
  25. 25 Spring Data Reactive

  26. 26 Reactive

  27. Kotlin Support 27

  28. Simple REST Controller // omitted imports @RestController @RequestMapping("/api/temperature") class TemperatureResource(val

    temperatureService: TemperatureService) { @GetMapping fun all() = this.temperatureService.all() @PostMapping fun register(request: TemperatureRequest) = this.temperatureService.register(request) } 28
  29. Repository // omitted imports interface TemperatureRepository:ReactiveCrudRepository<Temperature,String>{ @Tailable fun findByDeviceId(deviceId:String):Flux<Temperature> }

    29
  30. Continuous Queries Makes queries on DB and get notification when

    data changes. @Tailable for MongoDB 30
  31. SSE Server-Sent Events 31

  32. SSE REST Controller // omitted imports @RestController @RequestMapping("/api/device") class DeviceResource(val

    temperatureService: TemperatureService) { @GetMapping(path = ["/{id}/real-time"],produces = [MediaType.APPLICATION_STREAM_JSON_VALUE]) fun byDeviceRealTime(@PathVariable("id")deviceId:String) = this.temperatureService.byDevice(deviceId) } 32
  33. 33 Demo SSE

  34. Java 9 34

  35. Full Support for Java 9 Module System 35

  36. Others changes Actuator Endpoints Reactive Thymeleaf jUnit 5 Support 36

  37. Spring Boot Actuator 37

  38. 38 Actuator

  39. 39 How About Spring Cloud Projects???

  40. https://github.com/claudioed/kotlin-mongodb-demo 40 https://github.com/claudioed/goodbye https://github.com/PacktPublishing/Spring-5.0-By-Example GitHub

  41. @claudioed 41 https://www.linkedin.com/in/claudioedoliveira/ claudioed.oliveira@gmail.com Contacts