Reactive Programming for Java Developers Rossen Stoyanchev About - - PowerPoint PPT Presentation

reactive programming for java developers
SMART_READER_LITE
LIVE PREVIEW

Reactive Programming for Java Developers Rossen Stoyanchev About - - PowerPoint PPT Presentation

Reactive Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive Long-Running Shift to Concurrency Today Independent services, Cloud


slide-1
SLIDE 1

Reactive Programming for Java Developers

Rossen Stoyanchev

slide-2
SLIDE 2

About Me

❖ Spring Framework committer ❖ Spring MVC, WebSocket messaging ❖ Spring 5 Reactive

slide-3
SLIDE 3

Long-Running Shift to Concurrency

slide-4
SLIDE 4

10 years ago

Self-sufficient apps,

App server,

Keep it simple, don’t distribute

Today

Independent services,

Cloud environment, Distributed apps

slide-5
SLIDE 5

Changing expectations

Internet scale & resilience,

Efficient use of resources,

Latency is common

slide-6
SLIDE 6

Impact on programming model

Imperative logic not so simple when latency is the norm

Forced to deal with asynchronicity

Limits of scale

slide-7
SLIDE 7

There is another way

Fundamentally async & non-blocking Using very few threads Major shift but also major benefits

slide-8
SLIDE 8

Reactive Programming?

slide-9
SLIDE 9

In this talk

How would we design an async API in Java ? Can we do better ? Introducing reactive libraries Spring reactive experience

slide-10
SLIDE 10

Design async API in Java

slide-11
SLIDE 11

Return one thing

... ...

slide-12
SLIDE 12

Usage

slide-13
SLIDE 13

Return it async style

... May occur in different thread

slide-14
SLIDE 14

Usage

Ugh

slide-15
SLIDE 15

CompletableFuture (JDK 1.8)

❖ Future with actions ❖ Actions trigger when Future completes ❖ Callback mechanism

slide-16
SLIDE 16

Return it async style with Java 1.8

... ...

slide-17
SLIDE 17

Usage

Async callback!

slide-18
SLIDE 18

Usage

Requires null check

slide-19
SLIDE 19

Return many

... ...

slide-20
SLIDE 20

Return many

... ... No callback till all users collected

slide-21
SLIDE 21

Return many

... ... It may be too many

slide-22
SLIDE 22

Return nothing

... ...

slide-23
SLIDE 23

Return nothing

... ... Async notification: success or failure?

slide-24
SLIDE 24

Can we do better?

slide-25
SLIDE 25

❖ One notification per data item ❖ One notification for either completion or error

Async results as a stream

slide-26
SLIDE 26

Return Type Description Notifications

void Success

  • nComplete()

void Failure

  • nError(Throwable)

User Match

  • nNext(User), onComplete()

User No match

  • nComplete()

User Failure

  • nError(Throwable)

List<User> Two matches

  • nNext(User), onNext(User), onComplete()

List<User> No match

  • nComplete()

List<User> Failure

  • nError(Throwable)
slide-27
SLIDE 27

➢ Functional, declarative programming model ➢ Combine, transform, reduce sequences ➢ Focus on what, not how

Stream abstraction

slide-28
SLIDE 28

➢ Great example of the benefits of a stream API ➢ However built for collections mainly ➢ Pull-based, usable once

Java 8 Stream

slide-29
SLIDE 29

➢ Latency-sensitive data streams ➢ Infinite sequences ➢ Push-based notifications

Beyond collections

slide-30
SLIDE 30

Reactive Libraries

slide-31
SLIDE 31

Reactive library?

➢ Stream-like API similar to Java 8 ➢ Suited for any data sequence ➢ Latency-sensitive, infinite, collections

slide-32
SLIDE 32

Project Reactor

➢ Reactive Streams foundation for the JVM ➢ API similar to ReactiveX ➢ Easy to bridge to Java 8 Stream

slide-33
SLIDE 33

Flux -- sequence of 0..N

slide-34
SLIDE 34

Mono -- sequence of 0..1

slide-35
SLIDE 35

Flux to Java Stream

slide-36
SLIDE 36

Mono to CompletableFuture

slide-37
SLIDE 37

More than a stream API

➢ Reactor is back-pressure ready ➢ Reactive Streams spec ➢ Producers must not overwhelm consumers

slide-38
SLIDE 38

❖ Industry collaboration ❖ Small API, rules, TCK ❖ Reactive interoperability across libraries

Reactive Streams Spec

slide-39
SLIDE 39

“No single best fluent async/parallel API. CompletionStage best supports continuation-style programming on futures, and java.util.stream best supports (multi-stage, possibly- parallel) "pull" style operations on the elements of

  • collections. Until now, one missing category was "push"

style operations on items as they become available from an active source.“

Reactive Streams included in Java 9

Doug Lea, from initial announcement

slide-40
SLIDE 40

❖ Interfaces in java.util.concurrent.Flow ❖ SubmissionPublisher standalone bridge to Reactive Streams ❖ Tie-ins to CompletableFuture and Stream

Reactive Streams in Java 9

slide-41
SLIDE 41

Reactive Streams API

public interface Publisher<T> { void subscribe(Subscriber<? super T> subscriber); }

slide-42
SLIDE 42

Reactive Streams API

public interface Subscriber<T> { void onSubscribe(Subscription sub); void onNext(T item); void onError(Throwable ex); void onComplete(); }

slide-43
SLIDE 43

Reactive Streams API

public interface Subscriber<T> { void onSubscribe(Subscription sub); void onNext(T item); void onError(Throwable ex); void onComplete(); }

slide-44
SLIDE 44

Reactive repository

slide-45
SLIDE 45

Using the reactive repository

slide-46
SLIDE 46

Using the reactive repository

Subscriber triggers flow of data

slide-47
SLIDE 47

Using the reactive repository

Consume all data by default

slide-48
SLIDE 48

Output

  • nSubscribe

request(unbounded)

  • nNext(User: Jason)
  • nNext(User: Jay)

...

  • nComplete()
slide-49
SLIDE 49

Usage

Consume two at a time

slide-50
SLIDE 50

Output

  • nSubscribe

request(2)

  • nNext(User: Jason)
  • nNext(User: Jay)

request(2)

  • nNext(User: Joe)
  • nNext(User: John)

...

slide-51
SLIDE 51

❖ Currently 2.5 M4 (might change to 3.0 label) ❖ GA release scheduled for July ❖ Hands-on exercise, blog post series

More on Reactor

slide-52
SLIDE 52

Reactive Spring

slide-53
SLIDE 53

Reactive Spring MVC ?

slide-54
SLIDE 54

Annotated controllers

slide-55
SLIDE 55

Controller Methods

slide-56
SLIDE 56

Spring MVC Spring Web Reactive

Annotated controllers

slide-57
SLIDE 57

...

slide-58
SLIDE 58

...

Mono<Object>

slide-59
SLIDE 59

Spring MVC Spring Web Reactive Servlet API ???

@MVC

slide-60
SLIDE 60

Spring MVC Spring Web Reactive Servlet API ??? Servlet Container ???

@MVC

slide-61
SLIDE 61

Spring Web Reactive

@MVC

HTTP Reactive Streams

Servlet 3.1 Reactor I/O RxNetty

slide-62
SLIDE 62

spring-reactive

Spring Framework 5.0 M1

slide-63
SLIDE 63

More Reactive Efforts

slide-64
SLIDE 64

Reactive Journey

slide-65
SLIDE 65

@rstoya05