Slide 1

Slide 1 text

Let’s do this before we start git clone https://bitbucket.org/rlippolis/spring-web-reactive-workshop mememon-go-client -> npm install mememon-go-server -> mvn clean verify reactive-spring-intro -> mvn clean verify npm install -g angular-cli

Slide 2

Slide 2 text

Reactive Programming with Spring 5 Web Reactive Riccardo Lippolis - JDriven Bas W. Knopper - JCore

Slide 3

Slide 3 text

Who are we?

Slide 4

Slide 4 text

● Presentation ○ Intro ○ Quick dive into Reactive Programming ○ Reactive Streams Specification ○ Reactor ○ Spring Web Reactive ○ Examples Agenda

Slide 5

Slide 5 text

Quick Dive into Reactive Programming

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Increase in User Expectance Applications used to have •Couple of servers •Seconds of response time •Hours of offline maintenance •Gigabytes of data to process Today, users expect •Desktop, web & mobile support •Milliseconds response time •100% uptime •Petabytes of data to process

Slide 10

Slide 10 text

Reactive System

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Reactive Programming • Extending the observer pattern • Abstract away concerns • Low-level threading, Synchronization, Thread-safety, Concurrent data structures, Non-blocking I/O • Observable model • Streams of asynchronous events • Composable operations • No more callback hell!

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Manipulating data

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Java 8 Streams vs. Reactive Streams • A Stream is pull-based • it "asks" for items to process. • Reactive Streams are push-based • you are notified when an element is available.

Slide 18

Slide 18 text

Backpressure what if you can’t handle the data?

Slide 19

Slide 19 text

Frameworks? Libraries?

Slide 20

Slide 20 text

Reactive Extensions

Slide 21

Slide 21 text

Reactive Streams

Slide 22

Slide 22 text

Reactive Streams • A standard for asynchronous stream processing with non-blocking backpressure • Inspired by Reactive Extensions API • The API consists of the following components that are required to be provided by Reactive Stream implementations • Publisher • Subscriber • Subscription • Processor

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

“That’s it? I can implement that!” - Random developer “Just don’t...” - Bas & Riccardo

Slide 25

Slide 25 text

Reactor projectreactor.io

Slide 26

Slide 26 text

Reactor • Made by Pivotal! (Spring FTW \0/) • A foundation for asynchronous applications on the JVM • Interact directly with • Java 8 functional API • CompletableFuture • Stream • Duration • Based on the Reactive Streams Specification (same concepts, different naming) • Latest: 3.0.2.RELEASE

Slide 27

Slide 27 text

Flux and Mono

Slide 28

Slide 28 text

Flux and Mono

Slide 29

Slide 29 text

Examples - Creating Reactive Streams • Arbitrary number of items: Flux flux = Flux. just("Hello", "World!"); • Converting an existing collection / Java 8 Stream: List list = Arrays. asList(1, 3, 5); Flux flux = Flux. fromIterable(list); Flux flux = Flux. fromStream(list.stream()); • Empty stream: Flux.empty();

Slide 30

Slide 30 text

Examples - Manipulating Streams • Filtering Flux.just(1, 2, 3, 4, 5).filter(n -> n % 2 == 0); // [2, 4] • Transforming Flux.just(1, 2, 3, 4, 5).map(i -> i * 2); // [2, 4, 6, 8, 10] • Distinct only Flux.just("a", "a", "b", "c").distinct(); // [a, b, c]

Slide 31

Slide 31 text

Examples - Consuming Streams Flux.just("Hello", " ", "World!").subscribe(System. out::print); // Hello World!

Slide 32

Slide 32 text

Examples - Consuming Streams With a custom Subscriber Flux.just("Hello", " ", "World!").subscribe( new Subscriber() { @Override public void onSubscribe(Subscription s) { s.request(Long. MAX_VALUE); } @Override public void onNext(String s) { System. out.println("Received: " + s); } @Override public void onError(Throwable t) { System. err.println("Error: " + t.toString()); } @Override public void onComplete() { System. out.println("Done!"); } });

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Spring Web Reactive

Slide 35

Slide 35 text

Spring Web Reactive Uses the familiar Spring MVC programming model (@Controller, etc.)... ...but with a new reactive, non-blocking engine

Slide 36

Slide 36 text

Traditional Servlet Engine vs Reactive Engine Traditional

Slide 37

Slide 37 text

Traditional Servlet Engine vs Reactive Engine Traditional Reactive

Slide 38

Slide 38 text

Implementation • Implemented using Project Reactor • Uses Flux (0..N) and Mono (0..1) • Compatible with RxJava • Return Observables from your REST controllers

Slide 39

Slide 39 text

Current state of implementation • Version: 5.0 M2 (Not production ready!) • Focus on REST • Basic HTML rendering supported (Freemarker) • Support for • JSON (Jackson) • XML (JAXB) • SSE streaming • Zero-copy file transfers • • Functional Web Framework -> Not today

Slide 40

Slide 40 text

Examples - Using Streams • With a Spring @RestController @RestController public class MyController { @GetMapping ("count") public Flux count( @RequestParam ("to") int to) { return Flux.range(1, to); } } GET http://localhost:8080/count?to=5 -> [1, 2, 3, 4, 5]

Slide 41

Slide 41 text

Reactive Pitfalls Looks very cool, but… • Declarative style of coding is hard(er) to debug • “When is my code executed? (If ever)” • Appears to be multithreaded by default, but isn’t • Design choice "if there’s no explicit request to switch threads it stays on the same one for the next call, whatever that is"

Slide 42

Slide 42 text

Workshop part 1: Master the koans ● Open the reactive-spring-intro project ● Fix all the tests / koans in: src/test/java/com/jdriven/reactive/exercises

Slide 43

Slide 43 text

Workshop part 2: Mememon Go ● Demo explaining Use case ● Make all the tests pass by implementing the code ● Make the application work :)

Slide 44

Slide 44 text

Coming up • Support for Reactive types in other Spring projects, e.g.: • Spring Data • Spring Cloud • Spring Security • GA Release • Current release: 5.0 M2 • GA not before March 2017

Slide 45

Slide 45 text

Great work everybody!