Slides introducing reactive programming with Spring and Reactor at the Javaland "Reactive Spring" workshop, given March 2017.
Reactive Spring Workshop
View Slide
Reactive Programming 101what does it bring to the table?
WHY?
WHY?because blocking is evil
sync/blockingmain thread processingresumesI/O! app doesnothing
sync/blockingmain thread processingresumesI/OBAD! app doesnothing
async & blockingmain thread wait &join! newthreads,costly! complex
async & blockingmain thread wait &join! newthreads,costly! complexBAD
async & nonblocking“event loop”in non-blockingprocessingchunksno more threadsthan needed
how do you achieve thatwithout losing your mind ?
Reactive Programming
Composing asynchronous& event-based sequences,using non-blocking operators“”
without sacrifice
without sacrificeCallbacks ?Futures ?easy to blockhard to composecallback hell !not readable
Pull? Push!
vsIterable-IteratorPublisher-Subscriber
Data in Flux
Publisher Subscriberpush eventsproduces consumesfeedback
interfaces fromReactive Streams specPublisher Subscriberfeedbackconsumespush eventsproduces
Publisher Subscriberpush eventsproduces consumesfeedbackSubscriberonNext(T)onComplete();onError(Throwable);
Publisher Subscriberproduces consumesfeedback0..N elements+0..1 (complete | error)
Publisher Subscriberpush eventsproduces consumesfeedbackbackpressure
Publisher Subscriberpush eventsproduces consumesfeedbackcan I have anAPI though?
Reactor 3types and operators
Fluxfor 0..N elements
Monofor at most 1 element
Reactive Streamsall the way
focus on Java 8
focus on Java 8Duration, CompletableFuture, Streams
an Rx-inspired APIwith a vocabulary of operators similar to RxJava...
an Rx-inspired API...but not exactly the same
Flux/Monogeneratoroperatoroperatoroperatornothing happensuntil you subscribe
Flux/MonogeneratorSubscriberoperatoroperatoroperatornothing happensuntil you subscribe
Flux/MonogeneratorSubscriberoperatoroperatoroperatorper SubscriptionstateSubSubSub
Flux/MonogeneratorSubscriberoperatoroperatoroperatordataflowsSubSubSub
“elements of functional programming”
threadingcontexts
Reactor isagnostic
however itfacilitates switching
Schedulers
Schedulerselastic, parallel, single, timer...
publishOnswitch rest of the flux on a thread
subscribeOnmake the subscription and request happenon a particular thread
Flux/MonogeneratoroperatorsubscribeOnoperatorpublishOnoperatoroperatorSubscriberSubSubSubSubSubSub
CODINGTIME!
Bonus Slidesoperator step-by-step examples, backpressure
map, filter, buffer
Flux.rangeSubscribermapfilterbuffer
Flux.range(5, 3).map(i -> i + 3).filter(i -> i % 2 == 0).buffer(3)
Flux.range(5, 3).map(i -> i + 3).filter(i -> i % 2 == 0).buffer(3)5, 6, 7 |8, 9, 10 |8, 10 |[8,10]|
retry
Flux.fromSubscribermapfilterretryPublisher fromHTTP reactiveclient
Flux.fromSubscribermapfilterretryPublisher fromHTTP reactiveclientresubscribe
Trickier: flatMapasync sub-processes
flatMap(user -> tweetStream(user))
& muchmore...
backpressurethe "volume control"
Publisher Subscribersubscribe
Publisher Subscriberpush data as fast as possible
Publisher Subscribersubscribewith small request(eg. 1)
Publisher Subscriber1 onNext
Publisher Subscriberrequest more(eg. 2)
Publisher Subscriber2 onNext
Publisher Subscriberbackpressure
other ways of dealing with backpressureeg. drop, buffer...
Credits! Robot Devil: copyright FOX! Volume Knob: CC0 (via Pixabay)! Camel Shape: CC0 (via Pixabay)! Dromedary Shape: CC-By-SA USPN,Whidou (via Wikimedia)! Thread Balls: CC0 (via Pixabay)! Coding Time: derived from KEMUDA Computer Lab (CC-By-SA Andy.aug, via Wikimedia)! Sound Table: CC0 (via Pexels)! Dam: CC-By-SA Matthew Hatton (via geograph.org.uk)! logos: Pivotal, Spring, Twitter and Github logo copyright their respective companies.