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

Developing Reactive applications with Reactive ...

Developing Reactive applications with Reactive Streams and Java 8

Slides from our Devoxx 2016 university talk.

Video: https://youtu.be/Cj4foJzPF80
First part live coding: https://github.com/reactor/lite-rx-api-hands-on/
Seconde part live coding: https://github.com/bclozel/spring-reactive-university

Sébastien Deleuze

November 07, 2016
Tweet

More Decks by Sébastien Deleuze

Other Decks in Programming

Transcript

  1. @bclozel @sdeleuze #Devoxx #reactive Brian Clozel • Lyon, France +


    • Spring Framework
 • Spring Boot
 • @bclozel This guy
  2. @bclozel @sdeleuze #Devoxx #reactive Sébastien Deleuze • Remote worker at

    Pivota from Reactor Spring Framework 5 Kotlin support • conference staff member
  3. @bclozel @sdeleuze #Devoxx #reactive Agenda • Introduction to Reactive •

    Live coding with Reactor Core 3 • Coffee break (20 minutes) • Building a Reactive application with Spring Boot • Live coding
  4. @bclozel @sdeleuze #Devoxx #reactive Reactive, what is it? Reactive is

    used to broadly define event-driven systems
  5. @bclozel @sdeleuze #Devoxx #reactive Reactive programming Moving imperative logic to

    async, non-blocking, functional-style code, in particular when interacting with external resources
  6. @bclozel @sdeleuze #Devoxx #reactive Use case: push events to the

    client Server-Sent Events or Websocket RabbitMQ broker Messages
  7. @bclozel @sdeleuze #Devoxx #reactive Other use cases • Live database

    queries • Mobile (Android) • Big Data • Real time analytics • HTTP/2 • …
  8. @bclozel @sdeleuze #Devoxx #reactive Can’t we just use Java 8

    lambdas? public class CallbackHell {
 
 public void callbackHell() {
 
 asyncMethod(a ->
 asyncMethod(b ->
 asyncMethod(c ->
 asyncMethod(
 d -> System.out.println("Values received: " + a + "," + b + "," + c + "," + d),
 dEx -> System.err.println("An error occurred: " + dEx)
 )
 , cEx -> System.err.println("An error occurred: " + cEx))
 ,bEx -> System.err.println("An error occurred: " + bEx)),
 aEx -> System.err.println("An error occurred: " + aEx));
 }
 
 private void asyncMethod(Consumer<Object> success, Consumer<? super Throwable> failure) {
 // ....
 }
 
 }
  9. @bclozel @sdeleuze #Devoxx #reactive Can’t we just use Java 8

    types? Type Non-blocking Streaming Future<T> CompletableFuture<T> X Stream<T> X InputStream / OutputStream X
  10. @bclozel @sdeleuze #Devoxx #reactive We need tools Reactor Core RxJava

    Reactive Streams Akka Streams Reactive APIs &
  11. @bclozel @sdeleuze #Devoxx #reactive Reactive Streams • Reactive Streams is

    a contract for asynchronous stream processing with non-blocking back pressure handling • De-facto standard for the behaviour of reactive libraries and for interoperability • Co-designed by Netflix, Lightbend, Pivotal, RedHat, Kaazing,
 Twitter, and many others • Implemented by RxJava 2, Reactor, Akka Stream …
  12. @bclozel @sdeleuze #Devoxx #reactive Back-pressure Publisher Subscriber Publisher is not

    allowed to send new elements, even if new ones are ready. demand: 0
  13. @bclozel @sdeleuze #Devoxx #reactive Back-pressure • Allows to control the

    amount of inflight data • Regulate the transfer between • Slow publisher and fast consumer • Fast publisher and slow consumer
  14. @bclozel @sdeleuze #Devoxx #reactive Reactive Streams is 4 interfaces (and

    a TCK) public interface Publisher<T> { void subscribe(Subscriber<? super T> s); } public interface Subscriber<T> { 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<T, R> extends Subscriber<T>, Publisher<R> { }
  15. @bclozel @sdeleuze #Devoxx #reactive Available in 2 distinct packages Same

    interfaces are included in the upcoming Java 9 in java.util.concurrent: • Flow.Publisher • Flow.Subscriber • Flow.Subscription • Flow.Processor As a standalone JAR with org.reactivestreams package: • Publisher • Subscriber • Subscription • Processor
  16. @bclozel @sdeleuze #Devoxx #reactive Reactive APIs on the JVM Apply

    a wide range of transformations to your data with operators: merge, buffer, split, transform, delay … Reactor Core RxJava Akka Streams
  17. @bclozel @sdeleuze #Devoxx #reactive Reactive APIs on the JVM (1/3)

    Reactive API RxJava 1 Akka Stream 2 Reactor Core 3 RxJava 2 Types for
 0..n elements Types for
 0..1 elements Observable Single (1)
 Completable (0) Source Sink Flow Flux Mono (0..1) Flowable Observable Single (1) Maybe (0..1)
 Completable (0)
  18. @bclozel @sdeleuze #Devoxx #reactive Reactive APIs on the JVM (2/3)

    Reactive API RxJava 1 Akka Stream 2 Reactor Core 3 RxJava 2 Reactive Streams types Non Reactive
 Streams types Observable Single
 Completable Source Sink Flow Flux Mono Flowable Observable Single Maybe
 Completable Limited back-pressure support
  19. @bclozel @sdeleuze #Devoxx #reactive Reactive APIs on the JVM (3/3)

    Reactive API RxJava 1 Akka Stream 2 Reactor Core 3 RxJava 2 Generation 2nd 3rd 4th 4th Support Limited back-pressure Reactive Streams +
 actor fusion Reactive Streams +
 operator fusion Reactive Streams +
 operator fusion
  20. @bclozel @sdeleuze #Devoxx #reactive Reactive APIs on the JVM (3/3)

    Reactive API RxJava 1 Akka Stream 2 Reactor Core 3 RxJava 2 Generation 2nd 3rd 4th 4th Support Limited back-pressure Reactive Streams +
 actor fusion Reactive Streams +
 operator fusion Reactive Streams +
 operator fusion Check out
 http://akarnokd.blogspot.fr/2016/03/operator-fusion-part-1.html
  21. @bclozel @sdeleuze #Devoxx #reactive Focus on Reactor Core 3 •

    Natively designed on top of Reactive Streams • Lightweight API with 2 types: Mono and Flux • Native Java 8 support and optimisations • Single 1 Mbytes JAR • Focus on performance • Reactive foundation of Spring Framework 5
  22. @bclozel @sdeleuze #Devoxx #reactive • Implements Reactive Streams Publisher •

    0 to n elements • Operators: flux.map(…).zip(…).flatMap(…) Flux<T>
  23. @bclozel @sdeleuze #Devoxx #reactive • Implements Reactive Streams Publisher •

    0 to 1 element • Operators: mono.then(…).otherwise(…) Mono<T>
  24. @bclozel @sdeleuze #Devoxx #reactive • Designed to test easily Reactive

    Streams Publishers • Carefully designed after writing thousands of Reactor and Spring reactive tests StepVerifier StepVerifier.create(flux)
 .expectNext("foo", "bar")
 .expectComplete()
 .verify();
  25. @bclozel @sdeleuze #Devoxx #reactive Live coding session • Creating Mono

    and Flux • StepVerifier • Transform: map() + flatMap() • Merge • Request and Back pressure handling • Error handling • Convert & adapt: RxJava 2, List • Reactive to blocking and other way around
  26. @bclozel @sdeleuze #Devoxx #reactive Building a Reactive application Building a

    web application with: • Spring Boot Reactive starter (Experimental ⚠) • Spring Boot 2.0 (SNAPSHOT ) • Spring Framework 5.0 (MILESTONE ) • Reactor 3.0 (RELEASE )
  27. @bclozel @sdeleuze #Devoxx #reactive Live coding session • Calling remote

    REST APIs • Dealing with datastores • Error handling • Rendering HTML views • Serving static resources • Server Sent Events • And more!
  28. @bclozel @sdeleuze #Devoxx #reactive Changing your mindset Common language with

    your peers
 (async, non-blocking, back-pressure, reactivex, reactive streams, operators…)
  29. @bclozel @sdeleuze #Devoxx #reactive Reactive Streams 1. Don’t implement Reactive

    Streams interfaces yourself
 (or do it "for fun") 2. Reactive Streams by itself is not enough 3. Nothing happens until something subscribes to the Publisher
  30. @bclozel @sdeleuze #Devoxx #reactive Reactive ecosystem 1. Carefully select your

    libraries / API / drivers 2. Tooling is important 3. Define additional code style rules in your team
  31. @bclozel @sdeleuze #Devoxx #reactive Links • @ProjectReactor & https://projectreactor.io •

    https://github.com/reactor/lite-rx-api-hands-on
 • https://github.com/bclozel/spring-reactive-university • https://spring.io • https://spring.io/blog/2016/04/19/understanding-reactive-types