$30 off During Our Annual Pro Sale. View Details »

Reactive Spring (JUG CH)

Mark Paluch
November 08, 2017

Reactive Spring (JUG CH)

These slides explain reactive programming concepts, using Project Reactor and Spring to build asynchronous, non-blocking applications.

Mark Paluch

November 08, 2017
Tweet

More Decks by Mark Paluch

Other Decks in Programming

Transcript

  1. 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

    View Slide

  2. 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?

    View Slide

  3. 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

    View Slide

  4. 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

    View Slide

  5. 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

    View Slide

  6. 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?

    View Slide

  7. 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

    View Slide

  8. 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

    View Slide

  9. 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

    View Slide

  10. 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

    View Slide

  11. 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

    View Slide

  12. 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

    View Slide

  13. 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

    View Slide

  14. 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

    View Slide

  15. 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

    View Slide

  16. 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

    View Slide

  17. 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

    View Slide

  18. 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?

    View Slide

  19. 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

    View Slide

  20. 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);
    }

    View Slide

  21. 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);
    }

    View Slide

  22. 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();

    View Slide

  23. 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));

    View Slide

  24. 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

    View Slide

  25. 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

    View Slide

  26. 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

    View Slide

  27. 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

    View Slide

  28. 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

    View Slide

  29. 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

    View Slide

  30. 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?

    View Slide

  31. 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

    View Slide

  32. 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

    View Slide

  33. 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

    View Slide

  34. 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)

    View Slide

  35. 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)

    View Slide

  36. 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);
    }

    View Slide

  37. 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

    View Slide

  38. 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

    View Slide

  39. 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

    View Slide

  40. 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

    View Slide

  41. 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

    View Slide