Reactive Programming
for Java Developers
Constantin PopaEngineering #5
16:00-16:45
Reactive Programming
for Java Developers
Constantin Popa
Reactive Programming
for Java Developers
Constantin Popa
First
a
Survey
Who here uses…
Java 8
Spring framework
RxJava/Reactor
Spring WebFlux
AGENDA
Reactive Programming 101
Reactive streams – Project Reactor
Reactive web apps – Spring WebFlux
Reactive Programming 101
Why?
because blocking is evil
sync&blocking
main thread
I/O
time
main thread
resume processing
async&blocking
main thread
time
wait & join
I/O
I/O
async&nonblocking
main thread
time
Event loop
worker thread
I/O request
I/O request
I/O Data
available
how do achieve that?
without losing your mind…
Reactive Programming
“Composing asynchronous &
event-based streams
using non-blocking operators”
We don’t sacrifice much
callbacks?
Future? CompletableFuture?
Pull? Push!
vs
Iterable
-
Iterator
Publisher
-
Subscriber
Data in Flux
Publisher Subscriber
produces consumes
push events
Feedback
interfaces from
Reactive Streams
specification
now part of Java 9, under
java.util.concurrent.Flow
Subscriber<T>
onNext(T)
onComplete()
onError(Throwable)
onSubscribe(Subscription)
0..N elements
+
0..1 (complete | error)
backpressure
These interfaces
don’t do much…
Subscription.request(n)
Subscription.cancel()
Project Reactor
Flux<T>
generates 0..N elements
Marble diagrams
Give the Flux a second chance
Interactive marble diagrams on http://rxmarbles.com
(most map to Reactor operators)
Mono<T>
generates 0..1 elements
Reactive streams vs Java 8 streams
”The design center for Streams is mostly about data that can be accessed
with no latency (either from data structures or generating functions)”
“The design center for Rx is about infinite streams of events which
may arrive asynchronously.”
Brian Goetz
an Rx-inspired API
with a vocabulary of operators similar to RxJava
Flux.range map
filter
buffer Subscriber
Nothing happens
until you subscribe
Flux.range(1,3) map
filter
buffer Subscriber
123
Flux.range(1,3)
.map( number -> toShape())
.filter(shape -> isCircle())
.buffer()
.subscribe(shape -> printShape())
More awesomeness
async sub-processes with flatMap
Reactor is
concurrency-agnostic
Flux.range(1,3)
.map( number -> toShape())
.filter(shape -> isCircle())
.buffer()
.subscribe(shape -> printShape())
Flux.range(1,3)
.subscribeOn(Schedulers.elastic())
.map(number -> toShape())
.filter(shape -> isCircle())
.buffer()
.publishOn(Schedulers.immediate())
.subscribe(shape -> printShape())
Schedulers.single()
Schedulers.parallel()
Schedulers.elastic()
Schedulers.immediate()
Reactive web applications Spring
WebFlux
Use case #1
Reactive data repository
Let's recap:
HTTP GET with reactive data repository
Designed to work with both Spring MVC and Spring WebFlux
Simply return reactive types(Flux, Observable) from @Controller
@GetMapping("/cars")
@ResponseBody
public Flux<Car> getCars() {
return this.repository.findAll();
}
Flux<T>:
finite collection or infinite stream?
Use media type to decide
“application/json”
finite collection(JSON array)
“text/event-stream”
“application/stream+json”
infinite stream
DEMO
Use case #2
Response stream with backpressure
Handling backpressure
request(n)->write->flush->repeat
DEMO
Let’s recap:
HTTP GET with streaming response
Simply return reactive types(Flux, Observable) from @Controller
Backpressure handling on both Spring MVC and Spring WebFlux
Use case #3
Reactive remote service orchestration
DEMO
Let’s recap:
Reactive webclient
Orchestrate non-blocking, nested remote service calls with ease.
Similar to reactive data access
What WebFlux is good for:
High traffic
High concurrency with less hardware resources
Latency, streaming scenarios
Summary
Quite a steep learning curve
Doesn’t fit all scenarios, if it ain’t broken, don’t fix it
It’s fun time to be a Java developer!
Spring WebFlux and Reactor unlock the gates for easy Reactive
Programming on the server side and not only

Reactive programming for java developers