Slide 1

Slide 1 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor Core 3.0 A lite Rx API for the JVM Sébastien Deleuze - Stéphane Maldini @sdeleuze - @smaldlini

Slide 2

Slide 2 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Sébastien Deleuze • Live in Lyon • Remote worker @ Pivotal • Spring Framework and Reactor committer • Works on Spring Framework 5
 upcoming Reactive support • Co-worker @ La Cordée • Mix-IT staff member • @sdeleuze on Twitter 2

Slide 3

Slide 3 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Stéphane Maldini • Survive in London • Social Engineering @ • Project Reactor lead • Reactive Streams & 
 Reactive Streams Commons contributor • Works on Spring Framework 5
 upcoming Reactive support • @smaldini on Twitter 3 +

Slide 4

Slide 4 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Why going reactive? • More for scalability and stability than for speed • Use cases: • Webapp calling remote web services • Lot of slow clients • Big Data • Serve more clients on the same hardware • Event based development (web, mobile) 4

Slide 5

Slide 5 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive, what is it? 5 More details on http://fr.slideshare.net/StphaneMaldini/intro-to-reactive-programming-52821416 • Reactive is used to broadly define event-driven systems • Reactive Manifesto defines qualities of reactive systems • Reactive programming: moving imperative logic to async, non- blocking, functional-style code, in particular when interacting with external resources

Slide 6

Slide 6 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ For reactive programming, we need tools : ☐ Reactive Streams ☐ Reactive APIs 6

Slide 7

Slide 7 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Streams • Reactive Streams is a contract for asynchronous stream processing with non-blocking back pressure • De facto standard for interop between reactive libraries • To be included in Java 9 as java.util.concurrent.Flow 7

Slide 8

Slide 8 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Streams principle 8 Publisher Subscriber Data Demand • Max(InflightData) <= demand • No data sent without demand • Demand can be unbounded • The recipient controls how much data it will receive

Slide 9

Slide 9 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Streams is 4 interfaces (+ a TCK) 9 public interface Publisher { void subscribe(Subscriber super T> s); } public interface Subscriber { void onSubscribe(Subscription s); void onNext(T t); void onError(Throwable t); void onComplete(); } public interface Subscription { void request(long n); void cancel(); } public interface Processor extends Subscriber, Publisher { }

Slide 10

Slide 10 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 10 For reactive programming, we need tools : ☑ Reactive Streams ☐ Reactive APIs

Slide 11

Slide 11 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ • Buffer, merge, concatenate, or apply a wide range 
 of transformations to your data • On the JVM: • Reactor 3.0 is 4th generation* and based on Reactive Streams • RxJava 1.x: 2nd generation* and most used implementation • Akka Stream 2.x: Lightbend 3rd generation* Reactive API • Also for other languages, for example RxJS, MostJS Reactive APIs 11 * Based on http://akarnokd.blogspot.fr/2016/03/operator-fusion-part-1.html

Slide 12

Slide 12 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor Core 3.0 • Built with major contributions from Dávid Karnok (RxJava lead) and from some Spring Framework committers • Natively based on Reactive Streams, RSC* and Java 8+ • Strong focus on efficiency • Powerful Mono API • Ever-improving debugging, logging, testing capabilities * ReactiveStreamsCommons is a research effort about reactive flows 12

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Flux (0..N elements) with ReactiveX compliant API 14

Slide 15

Slide 15 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Mono (0..1 element) 15

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Type comparaison 17 No value Single value Multiple values Blocking void T
 Future Iterable Collection Stream Non- blocking CompletableFuture CompletableFuture CompletableFuture> Reactive
 Streams Publisher Publisher Publisher RxJava Completable Single Observable Reactor Mono Mono Flux

Slide 18

Slide 18 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ https://spring.io/blog/2016/04/19/understanding-reactive-types 18

Slide 19

Slide 19 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Spring 19 • Spring projects are going reactive • Reactor Core is the reactive foundation • RxJava adapters provided • You will be able to choose your web engine:
 Tomcat, Jetty, Undertow or Netty • Most impact on Web and Data support (IO intensive) • Spring Reactive experiment • Spring Reactive Playground sample application

Slide 20

Slide 20 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Well known Controller example 20 @RestController public class UserController { private BlockingRepository repository; @RequestMapping(path = "/save-capitalized", method = RequestMethod.POST) public void saveCapitalized(@RequestBody List users) { users.forEach(u -> u.setName(u.getName().toUpperCase())); repository.save(users); } } public interface BlockingRepository { void save(List elements); Iterable findAll(); }

Slide 21

Slide 21 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Controller with Reactive types 21 @RestController public class UserController { private ReactiveRepository repository; @RequestMapping(path = "/save-capitalized", method = RequestMethod.POST) public Mono saveCapitalized(@RequestBody Flux users) { return repository.save(users.map(u -> new User(u.getName().toUpperCase())); } } public interface ReactiveRepository { Mono save(Publisher elements); Flux findAll(); }

Slide 22

Slide 22 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Blocking vs Reactive: memory consumption 22 Memory consumption Time Blocking Reactive

Slide 23

Slide 23 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Blocking vs Reactive: streaming updates 23 Number of users saved in the database Time Blocking Reactive

Slide 24

Slide 24 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Controller with Reactive return values 24 @RestController public class UserController { private ReactiveRepository repository; @RequestMapping(path = "/", method = RequestMethod.GET) public Flux findAll() { return repository.findAll(); } } • Optimized serialization when using Flux instead of List • Also perfectly suitable for Server-Sent Events

Slide 25

Slide 25 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive HTTP client with Mono 25 import static org.springframework.web.client.reactive.HttpRequestBuilders.*; import static org.springframework.web.client.reactive.WebResponseExtractors.*; Mono result = webClient .perform(get("http://localhost:8080/person") .header("X-Test-Header", "testvalue") .accept(MediaType.APPLICATION_JSON)) .extract(body(Person.class));

Slide 26

Slide 26 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive HTTP client with Flux 26 import static org.springframework.web.client.reactive.HttpRequestBuilders.*; import static org.springframework.web.client.reactive.WebResponseExtractors.*; Flux response = webClient .perform(get("http://localhost:8080/persons") .accept(MediaType.APPLICATION_JSON)) .extract(bodyStream(Person.class)); Works for: • JSON array [{"foo":"bar"},{"foo":"baz"}] • JSON Streaming {"foo":"bar"}{"foo":"baz"} • SSE with something like .extract(sseStream(Person.class))

Slide 27

Slide 27 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Perfect fit for Microservices 27 Can handle bidirectional stream processing

Slide 28

Slide 28 text

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Now let’s go to the code! 28 https://github.com/reactor/lite-rx-api-hands-on/