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

Hands-on Lab: Making the paradigm-shift to Reactive Programming with Spring 5 Web Reactive @ J-Fall16

Hands-on Lab: Making the paradigm-shift to Reactive Programming with Spring 5 Web Reactive @ J-Fall16

These last few years Reactive Programming has found the hearts of a significant amount of developers. The fact that Spring 5 will incorporate the possibility of programming Reactive Systems in its core framework through the Spring Web Reactive project is a very positive development (pun intended) and shows the the paradigm-shift towards declarative programming. A probably familiar phrase in the Reactive Programming context is “Everything is a Stream”. So, what is a Stream? If you’re familiar with Futures, which abstract the delivery of a single value asynchronously in the future, and Iterables, for accessing a sequence of items which have an unknown length, then you can think of a Stream as essentially being a hybrid of the two: a mechanism for asynchronously receiving multiple items. The session starts with the basics of Reactive Programming (in a nutshell) after which we’ll quickly move on to the hands-on part of the session. Using a number of small programming assignments we will familiarize you with all the concepts involving Reactive Programming, and the Spring 5 Web Reactive module. Then, with a real-life use case, we will finally show you why it is such a powerful paradigm. So let us introduce you to a way of building applications in a declarative way, as opposed to imperatively, resulting in more responsive and resilient applications.

Bas W. Knopper

November 04, 2016
Tweet

More Decks by Bas W. Knopper

Other Decks in Programming

Transcript

  1. 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
  2. • Presentation ◦ Intro ◦ Quick dive into Reactive Programming

    ◦ Reactive Streams Specification ◦ Reactor ◦ Spring Web Reactive ◦ Examples Agenda
  3. 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
  4. 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!
  5. 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.
  6. 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
  7. 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
  8. Examples - Creating Reactive Streams • Arbitrary number of items:

    Flux<String> flux = Flux. just("Hello", "World!"); • Converting an existing collection / Java 8 Stream: List<Integer> list = Arrays. asList(1, 3, 5); Flux<Integer> flux = Flux. fromIterable(list); Flux<Integer> flux = Flux. fromStream(list.stream()); • Empty stream: Flux.empty();
  9. 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]
  10. Examples - Consuming Streams With a custom Subscriber Flux.just("Hello", "

    ", "World!").subscribe( new Subscriber<String>() { @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!"); } });
  11. Spring Web Reactive Uses the familiar Spring MVC programming model

    (@Controller, etc.)... ...but with a new reactive, non-blocking engine
  12. Implementation • Implemented using Project Reactor • Uses Flux (0..N)

    and Mono (0..1) • Compatible with RxJava • Return Observables from your REST controllers
  13. 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
  14. Examples - Using Streams • With a Spring @RestController @RestController

    public class MyController { @GetMapping ("count") public Flux<Integer> count( @RequestParam ("to") int to) { return Flux.range(1, to); } } GET http://localhost:8080/count?to=5 -> [1, 2, 3, 4, 5]
  15. 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"
  16. 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
  17. Workshop part 2: Mememon Go • Demo explaining Use case

    • Make all the tests pass by implementing the code • Make the application work :)
  18. 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