Slide 1

Slide 1 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Spring Mark Paluch, Pivotal Software, Inc. @mp911de

Slide 2

Slide 2 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What is Reactive Programming?

Slide 3

Slide 3 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Makes assumptions over resource usage ! Developers are in charge of resource usage efficiency 3 Imperative programming

Slide 4

Slide 4 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Container calls code on a Thread ! Thread is occupied ! Thread is released at the end of work 4 Imperative: Web applications

Slide 5

Slide 5 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Use Case: Synchronous fetch model 5 Service App Data store App Service

Slide 6

Slide 6 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Reactive is used to describe event-driven systems ! Reactive is used more for scalability and stability than for speed ! Reacts to resource availability 6 What is reactive?

Slide 7

Slide 7 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Use Case: Serve slow clients 7 Server

Slide 8

Slide 8 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Use Case: Serve slow clients 8 Server

Slide 9

Slide 9 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Use Case: Push message to client 9 Server Message Broker

Slide 10

Slide 10 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Live (continuous) database queries ! UI event handling (Android) ! Big Data ! Real time analytics ! HTTP/2 (flow control) 10 Other use cases

Slide 11

Slide 11 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Collaborative initiative to provide a standard for asynchronous stream processing with non-blocking back pressure. ! Co-designed by Twitter, Lightbend, Pivotal, Netflix and many others ! De-facto interop standard ! Java 9: Flow API 11 Reactive Streams

Slide 12

Slide 12 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Publisher and Subscriber 12 Subscriber Publisher Subscribe Data

Slide 13

Slide 13 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Publisher and Subscriber 13 Subscriber Publisher Publish as fast as possible

Slide 14

Slide 14 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 14 Publisher I/O thread push

Slide 15

Slide 15 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Back pressure control 15 Subscriber Publisher Demand

Slide 16

Slide 16 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Back pressure control 16 Subscriber Publisher Demand

Slide 17

Slide 17 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring WebFlux

Slide 18

Slide 18 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Spring’s reactive web framework ! End to end non-blocking and asynchronous execution 18 What is WebFlux?

Slide 19

Slide 19 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Publisher to Publisher ! JSON (Jackson) and XML (JAXB/Aalto) ! Server-Sent Events ! Zero-Copy transfer of resources 19 Reactive Object Mapping

Slide 20

Slide 20 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive WebControllers 20 @GetMapping("/") Flux people() { return personRepository.findAll(); } @GetMapping("/people/{name}") Mono person(@PathVariable String name) { return personRepository.findByName(name); } @PostMapping("/people") Mono addUser(@RequestBody Mono person) { return personRepository.save(person); }

Slide 21

Slide 21 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Non-blocking 21 @GetMapping("/people/{name}") Person person(@PathVariable String name) { return new Person(name); } @GetMapping("/people/{name}") Mono person(@PathVariable String name) { return personRepository.findByName(name); }

Slide 22

Slide 22 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Web Client 22 WebClient client = WebClient.create(); client.get() // .uri("/api/people?count={count}", 10) .retrieve() .bodyToFlux(String.class) .doOnNext(item -> …) .blockLast();

Slide 23

Slide 23 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Router Functions 23 RouterFunction> routes = route(GET("/"), request -> ServerResponse.ok().build()) .and(route(POST("/people/{name}") .and(accept(MediaType.APPLICATION_JSON)), request -> ServerResponse.accepted().build())) .and(resources("/static", new ClassPathResource(…))); HttpHandler handler = RouterFunctions.toHttpHandler(routes); HttpServer server = HttpServer.create(8080); server.startAndAwait(new ReactorHttpHandlerAdapter(handler));

Slide 24

Slide 24 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 24

Slide 25

Slide 25 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data

Slide 26

Slide 26 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Accessing Data today 26 Request Data access Remote data service I/O Wait This one is waiting

Slide 27

Slide 27 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Accessing Data today 27 Request Data access Remote data service Multiple calls Bulk fetch

Slide 28

Slide 28 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Keep resources busy ! Connection contention ! Usually synchronous/blocking ! Multiple requests ! Asynchronous isn’t always a good answer 28 Todays’ data access

Slide 29

Slide 29 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Project Reactor 3.0 ! Spring Framework 5.0 ! Spring Data 2.0 ! A reactive (asynchronous, ideally non-blocking) driver ! Optional: Spring Boot 2.0 29 Requirements for Reactive Spring Data

Slide 30

Slide 30 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Non-blocking I/O ! Threading-infrastructure ! Back pressure ! Integration with reactive libraries 30 Why reactive drivers?

Slide 31

Slide 31 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! MongoDB ! Apache Cassandra ! Redis ! Couchbase 31 Reactive Spring Data modules

Slide 32

Slide 32 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! JDBC is a blocking API ! JPA is a blocking API ! Sorry, no reactive JPA support 32 What about JPA

Slide 33

Slide 33 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Reactive Template API ! Reactive Repository support ! Reduced feature set 33 Reactive Spring Data

Slide 34

Slide 34 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 34 Imperative Template API T insert(T objectToSave) void insertAll(Collection<…> objects)

Slide 35

Slide 35 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 35 Reactive Template API Mono insert(T objectToSave) Mono insert(Mono objectToSave)

Slide 36

Slide 36 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 36 Reactive Repository API public interface ReactivePersonRepository extends ReactiveCrudRepository {
 
 Flux findByLastname(String lastname);
 
 @Query("{ 'firstname': ?0 }")
 Mono customQuery(String firstname); Flux findByLastname(Mono lastname); }

Slide 37

Slide 37 text

public interface ReactivePersonRepository extends ReactiveCrudRepository {
 
 Flux findByLastname(String lastname);
 
 @Query("{ 'firstname': ?0 }")
 Mono customQuery(String firstname); Flux findByLastname(Mono lastname); } Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 37 Reactive Repository API

Slide 38

Slide 38 text

public interface ReactivePersonRepository extends ReactiveCrudRepository {
 
 Flux findByLastname(String lastname);
 
 @Query("{ 'firstname': ?0 }")
 Mono customQuery(String firstname); Flux findByLastname(Mono lastname); } Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 38 Reactive Repository API

Slide 39

Slide 39 text

public interface ReactivePersonRepository extends ReactiveCrudRepository {
 
 Flux findByLastname(String lastname);
 
 @Query("{ 'firstname': ?0 }")
 Mono customQuery(String firstname); Flux findByLastname(Mono lastname); } Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 39 Reactive Repository API

Slide 40

Slide 40 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Questions? Mark Paluch • @mp911de https://github.com/mp911de/reactive-spring https://mp911.de/reactive-spring

Slide 41

Slide 41 text

Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Resources ! Code - Repository @ Github ! Reactor Rx Lite API – Repository @ Github ! Spring Data Examples – Repository @ Github ! Spring projects release calendar – Google Calendar 41 @mp911de