Spring 5.0
Meets Reactive
Programming
Hello!Cláudio E. de Oliveira
Software Engineer and
Architect
At @sensedia
@claudioed
2
“ Launched TODAY
3
Agenda
⊷ What is Spring Boot
⊷ Reactive Programming
⊷ Project Reactor
⊷ Spring Boot 2
⊷ New Features
⊷ Spring Cloud Projects
4
Spring
Boot
Why Spring Boot became a huge
success???
5
⊷ Stand-alone applications
⊷ Starter POMs
⊷ 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
A lot of sub-projects
Spring Data
7
Spring AMQP
Spring
Framework
Spring
BatchSpring
Cloud
Spring Cloud Data Flow
Spring SecuritySpring Integration
Spring
Messaging
Reactive
Programmin
g
The JVM Scenario
8
9
JVM
Players
Project
Reactor
Reactive library implementation for Reactive
Streams Specification
10
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/
Why not RXjava??
12
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
Reactor Types
MONO
[0|1]
FLUX
[N]
15
Reactor Types
public Mono<Flight> flight(String id) {
return this.flightRepository.findById(id);
}
public Flux<Flight> flights() {
return this.flightRepository.findAll();
}
Samples
Spring Boot 2
What is coming???
16
17
18
⊷ Simpler Code, more
readable
⊷ Focus on Business logic
⊷ Stream processing implies
memory efficient
⊷ Flow API Java 9
19
20
Event-Loop
21
22
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
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
Spring Data Reactive
26
Reactive
Kotlin Support
27
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
Repository
// omitted imports
interface TemperatureRepository:ReactiveCrudRepository<Temperature,String>{
@Tailable
fun findByDeviceId(deviceId:String):Flux<Temperature>
}
29
Continuous Queries
Makes queries on DB and get
notification when data
changes.
@Tailable for MongoDB
30
SSEServer-Sent Events
31
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
Demo SSE
Java 9
34
Full Support for
Java 9 Module
System
35
Others changes
Actuator Endpoints
Reactive Thymeleaf
jUnit 5 Support
36
Spring Boot Actuator
37
38
Actuator
39
Spring Cloud
Projects
https://github.com/claudioed/kotlin-mongodb-demo
40
https://github.com/claudioed/goodbye
https://github.com/PacktPublishing/Spring-5.0-By-Example
GitHub
41

Spring 5.0 meets reactive programming

Editor's Notes

  • #7 Stand-Alone application makes the devops adoption easier than before Starter pom makes the configurations automatically using the application.yaml The infrastructure elements can be configured by code Spring Actuator metrics, monitoring logs and much more
  • #15 Mono represents 0 or 1 elements Flux (N) elements The basis is the stream, data emission
  • #16 Mono represents 0 or 1 elements Flux (N) elements The basis is the stream, data emission
  • #18 Reactive programming is oriented to data flow and change propagation. (asynchronous) In plain terms reactive programming is about non-blocking applications that are asynchronous and event-driven and require a small number of threads to scale vertically (i.e. within the JVM) rather than horizontally (i.e. through clustering).
  • #19 Abstracts away from low-level threading, synchronization, and concurrency issues. The model can be applied almost everywhere to solve almost any kind of problem.
  • #21 Popularized by nodejs There is a event queue to process http request
  • #22 Annotation-based with @Controller and the other annotations supported also with Spring MVC Functional, Java 8 lambda style routing and handling
  • #27 kotlin-monfo
  • #31 Capped collections are fixed-size collections that support high-throughput operations that insert and retrieve documents based on insertion order. Capped collections work in a way similar to circular buffers: once a collection fills its allocated space, it makes room for new documents by overwriting the oldest documents in the collection.
  • #32 Low latency delivery via a single, long-lived connection Efficient browser message parsing with no unbounded buffers Automatic tracking of last seen message and auto reconnect Client message notifications as DOM events
  • #34 kotlin-monfo
  • #38 Protected by Default Reactive Endpoint (Flux or Mono) Path was changed Pay attention at configurations because they are changed
  • #39 kotlin-monfo
  • #40 Not supported in Spring Boot 2 The features not address the problems with spring cloud tries to solve