Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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
critici...
Wilder
Rodrigues
- X-Men; A.I.; and programming geek;
- 21 years hitting keyboards;
- Father of 3;
- Attended Computer Sci...
What is it
about?
- The foundations
- A glance at FP
- Reactive Programming
- Reactive Streams
- Java 9 & Spring 5
4
The Foundations
5
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 inp...
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
Concepts
Higher-order functions
First-class functions
Pure functions
Recursion
Strict vs non-strict evaluation
Type system...
Reactive
Programming
Propagation of changes
Dependency graph
Propagation algorithms
Back-pressure
Higher-order RP
Lazy eva...
Propagation of
Changes
By assigning the result of an expression:
a = b + c;
The variable ‘a’ only changes once the
express...
Essence of
Implementations
Dependency Graph:
Nodes -> computations;
edges -> dependency
relationships.
Propagation algorit...
Essence of
Implementations
Higher-order RP
Data flows -> Data
flows
Turing completeness
Lazy evaluation
length([2*2, 3*3, ...
Reactive Streams
or flow-controlled components
14
Reactive Manifesto
Responsive
Responsiveness is the cornerstone of
usability and utility.
Resilient
It’s achieved by repli...
Tackle the issues
Fully 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.: i...
Reactive
Generations
3rd gen.: fix back-pressure properly with
Reactive Stream spec -> 4 interfaces, 30 rules
and 7 method...
0th generation -
DDP
Customer API
Observer subscribes to a
Subject for a given data
type.
Transport Stream processing
Pars...
JDK 9 is Reactive
JDK 8 was as well. :P
20
Interfaces and static methods for establishing flow-
controlled components.
Contains seven “one-way” message style defined...
Flow controlled
components
Flow
defaultBufferSize()
Flow.Processor
extends Publisher and Subscriber
Flow.Publisher
subscri...
Flow controlled
components
Flow.Subscriber
onSubscribe(Flow.Subscription subscription)
onNext(T item)
onError(Throwable th...
Demo
part I
24
Spring 5
Web Reactive Framework
25
The 4th
Generation
Extends Reactive
Streams spec with
Mono and Flux API
types.
Reactive
serialisation and
deserialisation
...
Generators, Operators
and Subscribers
A Flux is a publisher of a sequence of events:
Flux<String> flux = Flux.just("a", "b...
Generators, Operators
and Subscribers
Calling .subscribe() will initiate the data
flow:
upper.log()
.map(String::toUpperCa...
Threads, Schedulers and
Background Processing
Methods to control the thread boundaries:
.subscribeOn(Schedulers.parallel()...
Threads, Schedulers and
Background Processing
Split production and consumption of the data
with .publishOn():
Flux.just("a...
Reactive Web
Reactive web framework:
@[Rest]Controller
programming model, but
on a reactive, non-
blocking engine.
Reactiv...
Demo
part II
32
References
https://en.wikipedia.org/wiki/Lambda_calculus
https://en.wikipedia.org/wiki/Turing_completeness
https://en.wiki...
34
Upcoming SlideShare
Loading in …5
×

Embracing Reactive Streams with Java 9 and Spring 5

2,391 views

Published on

This talk is about going back to the basics, when Reactive Programming was not a buzz and not used in the Enterprise Arena. There will be moments when you will regret being here, but then we will present you with the Beauty of the Seventies. This talk intends to make you travel through time, experiencing how things were done back then. From the Beauty of the Seventies we will move straight to the GoF Observer pattern, which has been implemented over and over again with Publish / Subscriber mechanisms. That will depict the foundations of what we have seen with Functional Programming, Reactive Programming, FRP and Reactive Streams. Although all that sounds interesting, it’s not our last stop. We still have to go over Java 9 and Spring Web Reactive Framework. It embraces Reactive Streams and Reactor, by defying the challenge of switching from imperative-style to non-blocking logic. It might be seem by some as shiny and new, but it’s based on a well established idea that has been helping us for more than 40 years.

Published in: Technology

Embracing Reactive Streams with Java 9 and Spring 5

  1. 1. Embracing Reactive Streams Java 9 & Spring 5 1
  2. 2. 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
  3. 3. 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
  4. 4. What is it about? - The foundations - A glance at FP - Reactive Programming - Reactive Streams - Java 9 & Spring 5 4
  5. 5. The Foundations 5
  6. 6. A bit of history 1930s - 1960s ƛ-Calculus Kleene-Rosser Paradox Simply Typed ƛ No longer a formalism 6
  7. 7. 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 ComputabilityThesis 7
  8. 8. 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
  9. 9. Concepts Higher-order functions First-class functions Pure functions Recursion Strict vs non-strict evaluation Type systems Referential Transparency 9
  10. 10. Reactive Programming Propagation of changes Dependency graph Propagation algorithms Back-pressure Higher-order RP Lazy evaluation 10
  11. 11. 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
  12. 12. Essence of Implementations Dependency Graph: Nodes -> computations; edges -> dependency relationships. Propagation algorithms: Push, pull, or hybrid. 12
  13. 13. Essence of Implementations Higher-order RP Data flows -> Data flows Turing completeness Lazy evaluation length([2*2, 3*3, 4/0]) 13
  14. 14. Reactive Streams or flow-controlled components 14
  15. 15. Reactive Manifesto Responsive 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
  16. 16. Tackle the issues Fully non-blocking and asynchronous behaviour by enabling the parallel use of computer resources. 16
  17. 17. 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
  18. 18. 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
  19. 19. 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
  20. 20. JDK 9 is Reactive JDK 8 was as well. :P 20
  21. 21. 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
  22. 22. Flow controlled components Flow defaultBufferSize() Flow.Processor extends Publisher and Subscriber Flow.Publisher subscribe(Flow.Subscriber<? super T> subscriber) 22
  23. 23. Flow controlled components Flow.Subscriber onSubscribe(Flow.Subscription subscription) onNext(T item) onError(Throwable throwable) onComplete() Flow.Subscription request(long n) cancel() 23
  24. 24. Demo part I 24
  25. 25. Spring 5 Web Reactive Framework 25
  26. 26. The 4th Generation Extends Reactive Streams spec with Mono and Flux API types. Reactive serialisation and deserialisation 4th, not 3rd! 26
  27. 27. 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
  28. 28. 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
  29. 29. 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
  30. 30. 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
  31. 31. Reactive Web Reactive web framework: @[Rest]Controller programming model, but on a reactive, non- blocking engine. Reactive WebClient with full back-pressure support. 31
  32. 32. Demo part II 32
  33. 33. References https://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. 34. 34

×