embracing reactive streams with java 9 and spring 5

34
Embracing Reactive Streams Java 9 & Spring 5 1

Upload: wilder-rodrigues

Post on 06-Jan-2017

220 views

Category:

Technology


3 download

TRANSCRIPT

Embracing Reactive Streams

Java 9 & Spring 5

1

One doesn’t need to be a subject matter expert in order to share

knowledge. Whilst the one sharing must be open to

criticism, the one listening has to fact check, give feedback and spread what has been learned. The sharing process has to be

organic.

2

Wilder Rodrigues

- X-Men; A.I.; and programming geek; - 21 years hitting keyboards; - Father of 3; - Attended Computer Science for 3 years.

@wilderrodrigues @ekholabs

3

What is it about?

- The foundations - A glance at FP - Reactive Programming - Reactive Streams

- Java 9 & Spring 5

4

The Foundations5

A bit of history

1930s - 1960s

ƛ-Calculus

Kleene-Rosser Paradox

Simply Typed ƛ

No longer a formalism

6

Anonymously

square_sum(x, y) = x2 + y2 (x, y) -> x2 + y2 id(x) = x x -> x

(x, y) -> x2 + y2 x -> (y -> x2 + y2)

Single input

The stored-program computer

Combinatory Logic Currying Curry’s Paradox

Kleene-Rosser Paradox

Com

puta

bility

The

sis

7

A glance at FP the old new thing

1950s: LISP

1960: APL

1970s: ML, Scheme and FP

1987: Haskell

1990s: J and K

2000s: Q

8

ConceptsHigher-order functions

First-class functions

Pure functions

Recursion

Strict vs non-strict evaluation

Type systems

Referential Transparency

9

Reactive Programming

Propagation of changes

Dependency graph

Propagation algorithms

Back-pressure

Higher-order RP

Lazy evaluation

10

Propagation of Changes

By assigning the result of an expression:

a = b + c;

The variable ‘a’ only changes once the expression is evaluated.

The difference with RP is that ‘a’ will be updated at any time that ‘b’ or ‘c’ changes.

11

Essence of Implementations

Dependency Graph:

Nodes -> computations;

edges -> dependency relationships.

Propagation algorithms:

Push, pull, or hybrid.

12

Essence of Implementations

Higher-order RP

Data flows -> Data flows

Turing completeness

Lazy evaluation

length([2*2, 3*3, 4/0])

13

Reactive Streamsor flow-controlled components

14

Reactive ManifestoResponsive

Responsiveness is the cornerstone of usability and utility.

Resilient

It’s achieved by replication, containment, isolation and delegation.

Elastic

React to changes in the input rate.

Message-Driven

Ensures loose coupling, isolation and location transparency.

15

Tackle the issuesFully non-blocking and asynchronous behaviour by enabling the parallel use of computer resources.

16

Reactive Generations

0th gen.: consists of j.u.Observable and its callback cousins addXXXListener() -> JDK 1.0

1st gen.: issues addressed by Meijer & MS and first libs born -> Rx.NET, Reactive4Java and RxJava.

2nd gen.: issues on synchronous cancellation and back-pressure address by the RxJava team.

17

Reactive Generations

3rd gen.: fix back-pressure properly with Reactive Stream spec -> 4 interfaces, 30 rules and 7 methods. Behold the trinity: RxJava 2.x, Project Reactor and Akka-Streams.

4th gen.: fluent libraries on top of Reactive Streams spec now called operator-fusion.

5th gen.: extensions to support reactive IO operations in the form of bi-directional channels.

18

0th generation - DDP

Customer API

Observer subscribes to a Subject for a given data type.

Transport Stream processing

Parse header.

Feed publisher.

Notify subscribers - if any.

19

JDK 9 is ReactiveJDK 8 was as well. :P

20

Interfaces and static methods for establishing flow-controlled components. Contains seven “one-way” message style defined methods. Non-blocking back pressure with a pull strategy, where the consumer requests a number of messages to the producer. Collaboration between people from Kaazing, Netflix, Pivotal, Red Hat, Twitter, Typesafe, and others.

21

Flow controlled components

Flow

defaultBufferSize()

Flow.Processor

extends Publisher and Subscriber

Flow.Publisher

subscribe(Flow.Subscriber<? super T> subscriber)

22

Flow controlled components

Flow.Subscriber

onSubscribe(Flow.Subscription subscription)

onNext(T item)

onError(Throwable throwable)

onComplete()

Flow.Subscription

request(long n)

cancel()

23

Demopart I

24

Spring 5Web Reactive Framework

25

The 4th Generation

Extends Reactive Streams spec with Mono and Flux API types.

Reactive serialisation and deserialisation

4th, not 3rd!

26

Generators, Operators and Subscribers

A Flux is a publisher of a sequence of events:

Flux<String> flux = Flux.just("a", "b", “c”);

Mono<String> mono = Mono.just(“a”);

Logging to standard out by calling the .log() operator:

Flux<String> upper = flux.log().map(String::toUpperCase);

27

Generators, Operators and Subscribers

Calling .subscribe() will initiate the data flow:

upper.log() .map(String::toUpperCase)

.subscribe();

Or with a Consumer or a vanila Runnable:

.subscribe(System.out::println);28

Threads, Schedulers and Background Processing

Methods to control the thread boundaries:

.subscribeOn(Schedulers.parallel())

Switch processing to separate threads (splitting publisher items to another publisher):

Flux.just("a", "b", "c") .log() .flatMap(value -> Mono.just(value.toUpperCase()) .subscribeOn(Schedulers.parallel()), 2) .subscribe(value -> { log.info("Consumed: " + value); })

29

Threads, Schedulers and Background Processing

Split production and consumption of the data with .publishOn():

Flux.just("a", "b", "c")

.log()

.map(String::toUpperCase)

.subscribeOn(Schedulers.parallel("sub")) .publishOn(Schedulers.parallel("pub"), 2)

.subscribe(value -> {

log.info("Consumed: " + value); });

30

Reactive Web

Reactive web framework: @[Rest]Controller programming model, but on a reactive, non-blocking engine.

Reactive WebClient with full back-pressure support.

31

Demopart II

32

Referenceshttps://en.wikipedia.org/wiki/Lambda_calculus

https://en.wikipedia.org/wiki/Turing_completeness

https://en.wikipedia.org/wiki/Universal_Turing_machine

https://en.wikipedia.org/wiki/Kleene%E2%80%93Rosser_paradox

https://en.wikipedia.org/wiki/Curry%27s_paradox

https://en.wikipedia.org/wiki/Church%E2%80%93Turing_thesis

https://en.wikipedia.org/wiki/Currying

https://en.wikipedia.org/wiki/Von_Neumann_architecture

http://www.reactive-streams.org/

http://www.reactivemanifesto.org/

https://en.wikipedia.org/wiki/Functional_programming

https://en.wikipedia.org/wiki/Reactive_programming#Higher-order_reactive_programming

https://en.wikipedia.org/wiki/Functional_reactive_programming

http://akarnokd.blogspot.nl/2016/03/operator-fusion-part-1.html

https://github.com/reactor/reactor-core/blob/master/README.md

33

34