reactor 3.0, a jvm foundation for java 8 and reactive streams

26
Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactor 3. Reactive Foundation for Java 8 and Spring Stephane Maldini @smaldini

Upload: spring-by-pivotal

Post on 16-Apr-2017

1.526 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Reactor 3. Reactive Foundation for Java 8 and Spring

Stephane Maldini @smaldini

Page 2: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Reactor Core provides for a minimalist set of Reactive Streams ready

generators and transformers

2

WHY ?

Page 3: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 / 3

DIY Reactive Streams

Publisher<User> rickSanchez = userRepository.findUser(“rick”);

Might produce 0, 1 or N Users ! Produce User when ready

Callback for start, result, error or complete

rickSanchez.subscribe(new Subscriber<User>(){ … });

Page 4: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

DIY Reactive Streams

4

Can I have an API ?

Page 5: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 / 5

Page 6: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

3 years to mature

6

R e a c t i v e S t r e a m s C o m m o n s

I P C

A D D O N S

S p r i n g R x

C O R E

Existential questions

2013: 1.x 2014: 2.x 2016: 3.0

Microservices become the norm

Rocky style revival

Page 7: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

3x Influences and Collaborations

7

R e a c t i v e S t r e a m s C o m m o n s

I P C

A D D O N S

S p r i n g R x

C O R E

Page 8: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Flux.just("red", "white", "blue") .flatMap(value -> Mono.fromCallable(blockingStuff(value)) .subscribeOn(Schedulers.elastic())) .collect(Result::new, Result::add) .doOnNext(Result::stop) .subscribe(doWithResult);

A smarter Publisher ? Flux !

8

Page 9: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Mono, push/pull CompletionStage

9

Mono.delayMillis(3000) .map(d -> "Spring 4") .or(Mono.delayMillis(2000).map(d -> "Spring 5")) .then(t -> Mono.just(t+ " world")) .elapsed() .block();

Non!

Page 10: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Mono, push/pull CompletionStage

10

Mono.delayMillis(3000) .map(d -> "Spring 4") .or(Mono.delayMillis(2000).map(d -> "Spring 5")) .then(t -> Mono.just(t+ " world")) .elapsed() .subscribe();

Oui!

Page 11: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

ONE JAR reactor-core.jar

11

3x Simpler API Where Flux and Mono live

Make task execution great again and FIFO

Page 12: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Execution Model Freedom

12

Synchronous

Asynchronous

Scheduler / Scheduler.Worker

Page 13: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

3x more focus on Javadoc

13

http://projectreactor.io/core/docs/api

Page 14: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Learning to Flux & Mono

14

https://spring.io/blog/2016/06/07/notes-on-reactive-programming-part-i-the-reactive-

landscape

https://spring.io/blog/2016/06/13/notes-on-reactive-programming-part-ii-writing-some-code

https://spring.io/blog/2016/07/20/notes-on-reactive-programming-part-iii-a-simple-http-server-

application

Just browse spring.io/blog already

Page 15: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Bridge Existing Asynchronous CodeMono<String> response = Mono.create(emitter -> { HttpListener listener = event -> { if (event.getResponseCode() >= 400) { emitter.error(new RuntimeExeption("Failed")); } else { String body = event.getBody(); if (body.isEmpty()) { emitter.complete(); } else { emitter.complete(body.toLowerCase()); } } }; client.addListener(listener); emitter.setCancellation(() -> client.removeListener(listener)); });

15

Also exists for Flux of N items

Page 16: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Operation Fusion

• Reactor has a mission to limit the overhead in stack and message passing

• 2 key categories : • “Macro Fusion” : merge operators in one (assembly time) • “Micro Fusion” : avoid queue creation and short circuit where

possible request lifecycle.

16

Page 17: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Starve CPU’s with ParallelFlux…

17

Flux.range(1, 100000) .parallel(3) .runOn(Schedulers.parallel()) .collect(ArrayList::new, ArrayList::add) .sequential() .reduce(0, (a, b) -> a + b.size()) .subscribeWith(TestSubscriber.create()) .await(Duration.ofSeconds(5)) .assertValues(100_000) .assertNoError() .assertComplete();

x3

Page 18: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Efficient and micro bench ready

18

https://github.com/akarnokd/akarnokd-misc/tree/master/src/jmh/java/hu/akarnokd/comparison

Page 19: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Bridge Fabulous RxJava 1

19

RxJava1Adapter

Page 20: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Bridge Java 9 Reactive Streams

20

JdkFlowAdapter

Page 21: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Play with Reactor Core .NET

21

https://www.nuget.org/packages/Reactor.Core/

Page 22: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Backpressure for JS: Reactor Core TypeScript

22

https://www.npmjs.com/package/reactor-core-js

Page 23: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

BREXIT ?

23

Page 24: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

DONALD TRUMP ?

24

Page 25: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

EVERYTHING REACTIVE ?

25

Page 26: Reactor 3.0, a JVM Foundation for Java 8 and Reactive Streams

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Learn More. Stay Connected.

@ProjectReactor@smaldini

http://github.com/reactor

@springcentral spring.io/blog

@pivotal pivotal.io/blog

@pivotalcf http://engineering.pivotal.io