3. Reactive Streams
@TimvEijndhovenJ-Fall 2018
“Reactive Streams is an initiative to provide a standard for
asynchronous stream processing with non-blocking back
pressure. This encompasses efforts aimed at runtime
environments (JVM and JavaScript) as well as network
protocols.”
4. Reactive Streams
@TimvEijndhovenJ-Fall 2018
“The scope of Reactive Streams is to find a minimal set of
interfaces, methods and protocols that will describe the
necessary operations and entities to achieve the goal—
asynchronous streams of data with non-blocking back
pressure.”
5. Reactive Streams
• Java API
• Specification
• Technology Compatibility Kit (TCK)
• Example implementations
@TimvEijndhovenJ-Fall 2018
14. @TimvEijndhovenJ-Fall 2018
RxJava
• Java implementation of Reactive eXtensions
• Targets Java 6
• Used by Vert.X
• Currently on version 2
• RX has implementations in various programming languages
22. Back pressure in real life
@TimvEijndhovenJ-Fall 2018
https://beeldbank.rws.nl/MediaObject/Details/441293
23. @TimvEijndhovenJ-Fall 2018
Scheduling
• The reactive streams
implementations are not async
by default: Subscription by
default happens on the calling
thread
• The implementations often offer
separate schedulers for IO
operations (these are
expensive, blocking threads
while waiting)
https://www.flickr.com/photos/chefranden/310767408
24. @TimvEijndhovenJ-Fall 2018
Hot or Cold?
• Cold publisher: starts
producing items when
subscription happens
• All items are sent to all
subscribers
• Hot publisher:
• produces items before
subscription happens
• Items produced before
subscription are lost
https://commons.wikimedia.org/wiki/File:Emoji_hot_and_cold.svg
25. Working with resources
• try-with-resources does not work on async streams
• Use the implementation specific functions:
• RxJava: Flowable.using(…)
• Project Reactor: Flux.using(…)
• Akka Streams: FileIO.fromPath(…)
@TimvEijndhovenJ-Fall 2018
26. Testing
• Testing async stuff with jUnit is hard
• Use the implementation specific test tooling:
• RxJava: TestSubscriber
• Reactor: StepVerifier
• Akka Streams: TestSource TestSink (akka-stream-testkit)
@TimvEijndhovenJ-Fall 2018
27. Testing
• Testing reactive streams often comes down to:
• Validating the flow of events (emitted items, errors,
completion)
• Manipulate time and/or performance characteristics
@TimvEijndhovenJ-Fall 2018
28. Trickery and Witchcraft
• Operator fusion
• Macro fusion: Replacing subsequent operators with a single operator
• Micro fusion: Share resources or internal structure between operators
@TimvEijndhovenJ-Fall 2018
• Benefits performance:
• Optimizes thread/actor usage
• Eliminates queues and boundaries
https://pxhere.com/en/photo/1193558
29. Summary
• Reactive Streams provide asynchronous stream processing
• Several implementations
• Interoperability through reactive streams specification and
Java Flow
• Easy interfaces, hard to get right
@TimvEijndhovenJ-Fall 2018
31. @TimvEijndhovenJ-Fall 2018
Thanks for your time.
Got feedback? Tweet it!
All pictures belong
to their respective
authors
https://github.com/timve/reactivestreams-demo