VICTOR GRAZI VP, NOMURA
Reactive Programming is an emerging paradigm for non-blocking concurrent programming. This means you can have multiple workers without allocating resources to multiple threads! In this presentation we will take a look at what reactive programming is and why it has become so popular. Then we will look at Pivotal's Reactor implementation, along with a deep dive into some coding examples.
9. Transform and combine Flux streams
Flux
.filter(…)
.subscribe(…)
9
• Or even
Flux
• Or
Flux
.map(…)
.subscribe(…)
Each transforma+on returns a new resultant Flux.
etc., etc., etc.
.filter(…)
.map(…)
.subscribe(…)
.filter(…)
.map(…)
.doOnNext(System.out::println)
.subscribe(…)
11. Combine Fluxes to produce new Fluxes
Examples: merge – merges elements as they arrive.
zip – combines elements in sequence.
firstEmikng – returns the first stream to emit an element.
11
• Methods take one Flux as input and return another Flux.
13. Transforming Fluxes
Methods take one stream as input and return another stream.
• Flux.take(n) – Takes first n elements only.
• Flux.skip(n) – Skips first n elements, then takes the rest
• Flux.distinct() – Returns a new Flux with duplicates eliminated.
• Flux.distinctUntilChanged() – Eliminates consecutive duplicates.
• Flux.filter((x)->condition()) – Retains elements matching filter condition.
• Flux.map(Some::mapper) – Replaces elements with other elements.
• Flux.flatMap(Some::mapper) – Replaces elements with Fluxes.
13
15. Difference between Java Stream API and Reac0ve
15
Java Streams ReacFve Streams
Pull based Push based
Basically a way to iterate
collec+ons declara+vely
A@ach to real-+me feeds
Generally synchronous data Real-+me, concurrency, flow control
Streams can only be used once Reac+ve streams are highly reusable
No control of +ming Control back-pressure strategies
No composi+on of streams Advanced composi+on and
transforma+on
Finite amount of data Data sizes from zero to infinity
17. Asynchronous Streams
Built in support for concurrent publishers and subscribers
• Flux.observeOn(scheduler) – Specifies the thread for the Observer
• Flux.subscribeOn(scheduler) – Specifies the thread for the Subscriber
Types of schedulers
• Schedulers.immediate() – Parks current process and uses current thread
• Schedulers.computa2on() – The system-assigned computa+on thread
• Schedulers.io() – The system-assigned IO thread
• Schedulers.trampoline() – Uses the current thread, once it is done here
• Schedulers.newThread() – Uses a new thread
• Schedulers.from(Executor) – On the named executor
17
18. Cold Fluxes and Hot Fluxes
18
• Cold Fluxes
• Won’t begin pumping un+l a subscriber is a@ached.
• Each subscriber receives all of the events, beginning
from the historical first.
• Hot Fluxes
• Generally read live data, for example data feeds or
mouse movements.
• Begin pumping on connec+on.
• Each subscriber gets the latest feeds as they pump
19. Addi0onal Cold Fluxes (for Development)
• Flux.empty() – Completes on the first subscrip+on, without
emikng any values.
• Flux.never() – Emits no values and never completes.
• Flux.error() – Emits an onError() no+fica+on immediately on
every subscriber. No other values are emi@ed.
• Flux.doOnNext() – Diagnos+cs
19
20. Crea0ng Hot Fluxes (Flowables)
20
o Call “publish” on a cold Flux
ConnectableFlux<Long> hotFlux =
coldFlux.publish();
o Call “connect” to start pumping, with or without subscribers
hotFlux.connect();
hotFlux.subscribe(
val -> System.out.println("Subscriber >> " +val));