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

Going Reactive with Spring Data (øredev 2017)

Going Reactive with Spring Data (øredev 2017)

Introduction to reactive streams, project Reactor and Springframework 5 for building end to end reactive applications.

Christoph Strobl

November 09, 2017
Tweet

More Decks by Christoph Strobl

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/ Going with Data. Christoph Strobl
 Pivotal Software Inc.
 @stroblchristoph
  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/ In a nutshell, reactive programming is about
 non-blocking, event-driven applications
 that scale with a small number of threads
 with back pressure as a key ingredient
 that aims to ensure producers to not overwhelm consumers. (Rossen Stoyanchev)
  3. The next 50 Minutes 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/ - A bit of recent history. - Project Reactor / Spring Data Kay / Spring Framework 5. - Some Code.
  4. A bit of recent history. 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/ 2.0 GA 5.0 M1 Jul 2016 Kay M1 Sept 2016 Nov 2016 3.0 GA Sept 2015 Kay GA Sept 28 5.0 GA Oct 2nd Sept 25 3.1 GA Sept 21 9 Nov 9th
  5. Imperative Applications 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/
  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/ @RestController
 static class PersonController {
 
 PersonRepository repository;
 
 
 
 
 
 
 } @GetMapping(name="/", produces=MediaType.APPLICATION_JSON_VALUE)
 List<Person> listPersons(String name) {
 return repository.findAllByName(name);
 }

  7. Total Control 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/ {…}
  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/ image: Fire at the Bamber Family Home (FAL v.1.3 License)
  9. Batching 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/ {…}
  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/ image: Fire at the Bamber Family Home (FAL v.1.3 License)
  11. Async 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/ {…} Dispatcher Thread: Worker A Thread: Worker B Thread: Worker C
  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/ image: Fire at the Bamber Family Home (FAL v.1.3 License)
  13. Reactive 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/ {…} Subscriber Publisher Flow
  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/ image: Fire at the Bamber Family Home (FAL v.1.3 License)
  15. Iterator.next() Future.get() Subscriber.onNext(t) Remember 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/ vs
  16. Reactor 3.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/
  17. Reactive Streams 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/ Provider of a potentially unbounded number of sequenced elements. Publisher<T> Subscriber<T> A processing stage - actually both a Subscriber and a Publisher. Processor<T,R> One-to-one lifecycle of a Subscriber subscribing to a Publisher. Subscription
  18. Flux<Person> findAllByLastname(String lastname) Mono<Person> findByEmail(String email) 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/ Mono<T> (0…1) Flux<T> (0…n) #jürgenized
  19. Reactive Applications with Spring 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 Framework
 5 Project Reactor
 3.1 Spring Data
 Kay
  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/ Apply familiar concepts
 to a new way of expressing functionality.
 With minimal fluff and surprise!
  21. Spring WebFlux 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/ @RestController
 static class PersonController {
 
 PersonRepository repository;
 
 
 
 
 
 
 } @GetMapping(name="/", produces=MediaType.APPLICATION_STREAM_JSON_VALUE)
 Flux<Person> fluxPersons(String name) {
 return repository.findAllByName(name);
 }

  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/
  23. Spring WebFlux 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/ @RestController
 static class PersonController {
 
 PersonRepository repository;
 
 
 
 
 
 
 } @GetMapping(name="/", produces=MediaType.APPLICATION_STREAM_JSON_VALUE)
 Flux<Person> fluxPersons(String name) {
 return repository.findAllByName(name);
 }

  24. Spring Data Reactive 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/ interface Repo extends ReactiveCrudRepository<Person, String> { } Flux<Person> findAllByLastname(String lastname); Flux<Person> findAllByLastname(Mono<String> lastname); Flux<Person> customQuery(String lastname); @Query("{lastname : ?0}"); Flux<Person> findAllByLastname(String ln, Pageable page);
  25. Reactive Repository 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/ <S extends T> Mono<S> save(S entity); <S extends T> Mono<S> save(Mono<S> entity); <S extends T> Flux<S> saveAll(Iterable<S> entities); <S extends T> Flux<S> saveAll(Publisher<S> entities); Mono<Boolean> existsById(Mono<ID> id); Flux<T> findAll(); Mono<Long> count(); Mono<Void> deleteById(ID id);
  26. Reactive Template (MongoDB) 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/ /**
 * Query for a {@link Flux} of objects of type T from the specified collection.
 *
 * @param entityClass the parametrized type of the returned {@link Flux}.
 * @param collectionName name of the collection to retrieve the objects from
 * @return the converted collection
 */
 <T> Flux<T> findAll(Class<T> entityClass, String collectionName); /**
 * Map the results of an ad-hoc query on the collection for the entity class to a 
 * single instance of an object of the
 * specified type. *
 * @param query the query class that specifies the criteria 
 * @param entityClass the parametrized type of the returned {@link Mono}.
 * @return the converted object
 */
 <T> Mono<T> findOne(Query query, Class<T> entityClass);
  27. Reactive Streams MongoDB Java Driver 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/ /** * Counts the number of documents in the collection according to the given options. * * @param filter the query filter * @return a publisher with a single element indicating the number of documents */ Publisher<Long> count(Bson filter); /** * Finds all documents in the collection. * * @param filter the query filter * @return the fluent find interface */ FindPublisher<TDocument> find(Bson filter);
  28. Publisher<T> Subscriber<T> Processor<T,R> Subscription .subscribe(Subscriber) .onSubscribe(Subscription) .request(long) .onNext(T) Reactive Streams

    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/ 1 2 3 4 5
  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/ Time to Code… https://github.com/christophstrobl/going-reactive-with-spring-data
  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/ Reactive is about efficient resource usage. Even if backed with familiar concepts and framework support, 
 it is no free lunch. The price is complexity.
  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/ Spring Framework
 5 Project Reactor
 3.1 Spring Data
 Kay Thank you! Questions ?