Slide 1

Slide 1 text

Reactive Spring 5 Reactive Programming with Spring WebFlux and Spring Data Corneil du Plessis

Slide 2

Slide 2 text

A defnition reactive programming is an asynchronous programming paradigm concerned with data streams and the propagation of change Wikipedia

Slide 3

Slide 3 text

Reactive Manifesto • Responsive • Resilient • Elastic • Message Driven

Slide 4

Slide 4 text

Why reactive programming? • Handle back-pressure • Communicate change • Scalability • Performance • Reliability

Slide 5

Slide 5 text

Reactive Programming Models • Windows API • JVM Based • NIO • LMAX Disruptor • Akka Actor Model • JavaScript • AJAX • Promises • jQuery • Angular • React • FRP – Functional Reactive Programming • map, reduce, flter • Languages not limited to: • Scala • Kotlin • Haskell • Elm • Groovy • JavaScript • Java 8

Slide 6

Slide 6 text

Road to Reactive Spring • RxJava – 2011-2012 by Netfix • Project Reactor – 2013 by Pivotal • Reactive Streams Specifcation – 2014 collaboration by Netfix, Pivotal, Red Hat, Twitter, Typesafe and more. • reactor-spring • Spring 5

Slide 7

Slide 7 text

Implementing Technologies HttpHander / WebHander API • Netty • Undertow • Tomcat with Servlet 3.1 • Jetty with Servlet 3.1 Reactive Databases • MongoDB Reactive Streams Driver • Reactive Cassandra (RxJava) • Lettuce (Reactive Streams for Redis) • Reactive Couchbase.

Slide 8

Slide 8 text

Reactive Streams specifcation • Publisher • Subscriber • Subscription • Processor

Slide 9

Slide 9 text

Reactive Streams – Sequence Diagrams Subscribe Publish

Slide 10

Slide 10 text

Project Reactor • Implements Reactive Streams specifcation • Infuenced by RxJava • Infuenced by Java 8 Streams • Improved Developer Experience • Mono - 0 or One • Flux - 0 or More

Slide 11

Slide 11 text

Project Reactor - Mono static Mono just(T data); static Mono fromFuture( CompleteableFuture); Mono log(String category); T block(); T block(Duration timeout); map, flatMap, filter

Slide 12

Slide 12 text

Project Reactor - Flux static Flux just(T data); static Flux just(T … data); static Flux fromStream(Stream s); static Flux fromIterable i); Flux log(String category); static Mono> collectList(); Mono next(); Iterable toIterable(); Stream toStream(); map, flatMap, filter, reduce

Slide 13

Slide 13 text

Reactive Spring WebFlux • Annotated Controllers • @Controller or @RestController • @RequestMapping / @GetMapping / @PostMapping etc. • Flux instead of Collection • Functional Endpoints • Router Function • Handler Function • HandlerFilter Function

Slide 14

Slide 14 text

Spring MVC @RestController public class HistoryController { private HistoryInterface locHist; public HistoryController(HistoryInterface locHist) { this. locHist = locHist; } @GetMapping(path = "/search") public Collection find( @RequestParam("startDate") Date startDate, @RequestParam("endDate") Date endDate) { return locHist.find(startDate, endDate); } }

Slide 15

Slide 15 text

Reactive Spring WebFlux @RestController public class HistoryController { private HistoryInterface locHist; public HistoryController(HistoryInterface locHist) { this. locHist = locHist; } @GetMapping(path = "/search") public Flux find( @RequestParam("startDate") Date startDate, @RequestParam("endDate") Date endDate) { return locHist.find(startDate, endDate); } }

Slide 16

Slide 16 text

Reactive Spring WebFlux (Functional Endpoint) public class HistoryHandler { private HistoryInterface locHist; public History HistoryHandler( HistoryInterface locHist) { this. locHist = locHist; } public Mono find(ServerRequest request) { Date startDate = DateUtils.from(request.queryParam("startDate")); Date endDate = DateUtils.from(request.queryParam("endDate")); return ServerResponse.ok() .contentType(APPLICATION_JSON) .body(locHist.find(startDate, endDate), LocationHistory.class); } }

Slide 17

Slide 17 text

Reactive Spring WebFlux (Functional Endpoint) @Configuration @EnableWebFlux public class HistoryConfiguration { @Autowire private HistoryHandler histHandler; @Bean RouterFunction router() { return route(GET("/search") .and(accept(APPLICATION_JSON)), histHandler::find); } }

Slide 18

Slide 18 text

Spring Data public interface LocationHistoryRepository extends CrudRepository { Collection findByTimestampBetween( Date s, Date e); }

Slide 19

Slide 19 text

Reactive Spring Data public interface LocationHistoryRepository extends ReactiveCrudRepository { Flux findByTimestampBetween( Date s, Date e); }

Slide 20

Slide 20 text

Spring Data JPA • Spring Data JPA does not have Reactive Support. • Streams will provide best experience until reactive JDBC and JPA is available. // Repository method Stream findByDateBetween( Date startDate, Date endDate); // Service method return Flux.fromStream(repository.findByDateBetween(s,e));

Slide 21

Slide 21 text

Demo Non-reactive • Spring Boot 1.5.8 • Spring MVC • Spring Data MongoDB Reactive • Spring Boot 2.0.3 • Spring WebFlux • Spring Data Reactive MongoDB

Slide 22

Slide 22 text

Call timing Non-reactive • 0.000 >> controller • 0.000 >> service • 0.409 << service • 0.409 << controller Reactive • 0.000 >> controller • 0.000 >> service • 0.001 << service • 0.001 << controller

Slide 23

Slide 23 text

Synchronous process

Slide 24

Slide 24 text

Asynchronous process

Slide 25

Slide 25 text

Network timing Non-reactive Type Time Name lookup 0.004 Connect 0.005 Pre transfer 0.005 Start Transfer 0.419 Total 0.532 Reactive Type Time Name lookup 0.004 Connect 0.005 Pre transfer 0.005 Start Transfer 0.385 Total 0.425

Slide 26

Slide 26 text

WebFlux vs Servlet

Slide 27

Slide 27 text

More aspects of Reactive Programming • Error handling • Back pressure • Retry • Timeouts • Transactions

Slide 28

Slide 28 text

Thoughts on Reactive Spring • Pros • Great documentation • Preserves program structure • Safer concurrency • Simpler abstraction than RxJava without sacrifcing the power. • Facilitates good practice • Kotlin support in Core • Cons • Debugging complexity • Could not fnd any other negative comments

Slide 29

Slide 29 text

See Also • ReactiveX • Languages • RxJava • RxKotlin • RxGroovy • RxClojure • RxSwift • Rx.Net (C#) and more… • Platforms • RxAndroid • RxCocoa • RxNetty • Akka & Akka Streams • Ratpack • Vert.x • Play Framework • Languages • Reactor Scala Extensions • Reactor Kotlin Extensions (now in core) • Messaging Communication • Reactor Kafka • Reactor RabbitMQ • Reactor Aeron • Reactor Netty

Slide 30

Slide 30 text

Finally • Contact: @corneil • Source - https://github.com/corneil/reactive-spring-5 • Resources • Project Reactor Site - https://projectreactor.io/ • David Karnok's Blog - http://akarnokd.blogspot.co.za/ • WebFlux vs Servlet https://dzone.com/articles/raw-performance-numbers-spring-boot-2-webfux-vs-s Questions?

Slide 31

Slide 31 text

No content