reactive programming for java developers - qcon … programming for java developers rossen...

65
Reactive Programming for Java Developers Rossen Stoyanchev

Upload: trinhquynh

Post on 13-Jun-2018

255 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Reactive Programming forJava Developers

Rossen Stoyanchev

Page 2: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

About Me❖ Spring Framework committer

❖ Spring MVC, WebSocket messaging

❖ Spring 5 Reactive

Page 3: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Long-Running Shift to Concurrency

Page 4: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

10 years ago

Self-sufficient apps,

App server,

Keep it simple, don’t distribute

TodayIndependent services,

Cloud environment,

Distributed apps

Page 5: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Changing expectations

Internet scale & resilience,

Efficient use of resources,

Latency is common

Page 6: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Impact on programming model

Imperative logic not so simple when latency is the norm

Forced to deal with asynchronicity

Limits of scale

Page 7: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

There is another way

Fundamentally async & non-blocking

Using very few threads

Major shift but also major benefits

Page 8: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Reactive Programming?

Page 9: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

In this talk

How would we design an async API in Java ?

Can we do better ?

Introducing reactive libraries

Spring reactive experience

Page 10: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Design async API in Java

Page 11: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Return one thing

...

...

Page 12: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Usage

Page 13: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Return it async style

...

May occur indifferent thread

Page 14: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Usage

Ugh

Page 15: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

CompletableFuture (JDK 1.8)❖ Future with actions

❖ Actions trigger when Future completes

❖ Callback mechanism

Page 16: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Return it async style with Java 1.8

...

...

Page 17: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Usage

Async callback!

Page 18: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Usage

Requires null check

Page 19: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Return many

...

...

Page 20: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Return many

...

... No callback till all users collected

Page 21: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Return many

...

... It may be too many

Page 22: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Return nothing

...

...

Page 23: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Return nothing

...

...

Async notification:success or failure?

Page 24: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Can we do better?

Page 25: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

❖ One notification per data item

❖ One notification for either completion or error

Async results as a stream

Page 26: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Return Type Description Notificationsvoid Success onComplete()

void Failure onError(Throwable)

User Match onNext(User), onComplete()

User No match onComplete()

User Failure onError(Throwable)

List<User> Two matches onNext(User), onNext(User), onComplete()

List<User> No match onComplete()

List<User> Failure onError(Throwable)

Page 27: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

➢ Functional, declarative programming model

➢ Combine, transform, reduce sequences

➢ Focus on what, not how

Stream abstraction

Page 28: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

➢ Great example of the benefits of a stream API

➢ However built for collections mainly

➢ Pull-based, usable once

Java 8 Stream

Page 29: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

➢ Latency-sensitive data streams

➢ Infinite sequences

➢ Push-based notifications

Beyond collections

Page 30: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Reactive Libraries

Page 31: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Reactive library?➢ Stream-like API similar to Java 8

➢ Suited for any data sequence

➢ Latency-sensitive, infinite, collections

Page 32: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Project Reactor➢ Reactive Streams foundation for the JVM

➢ API similar to ReactiveX

➢ Easy to bridge to Java 8 Stream

Page 33: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Flux -- sequence of 0..N

Page 34: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Mono -- sequence of 0..1

Page 35: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Flux to Java Stream

Page 36: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Mono to CompletableFuture

Page 37: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

More than a stream API➢ Reactor is back-pressure ready

➢ Reactive Streams spec

➢ Producers must not overwhelm consumers

Page 38: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

❖ Industry collaboration

❖ Small API, rules, TCK

❖ Reactive interoperability across libraries

Reactive Streams Spec

Page 39: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

“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

Page 40: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

❖ Interfaces in java.util.concurrent.Flow

❖ SubmissionPublisher standalone bridge to Reactive Streams

❖ Tie-ins to CompletableFuture and Stream

Reactive Streams in Java 9

Page 41: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Reactive Streams API

public interface Publisher<T> {

void subscribe(Subscriber<? super T> subscriber);

}

Page 42: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Reactive Streams API

public interface Subscriber<T> {

void onSubscribe(Subscription sub);

void onNext(T item);

void onError(Throwable ex);

void onComplete();

}

Page 43: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Reactive Streams API

public interface Subscriber<T> {

void onSubscribe(Subscription sub);

void onNext(T item);

void onError(Throwable ex);

void onComplete();

}

Page 44: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Reactive repository

Page 45: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Using the reactive repository

Page 46: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Using the reactive repository

Subscriber triggers flow of data

Page 47: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Using the reactive repository

Consume all data by default

Page 48: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

OutputonSubscribe

request(unbounded)

onNext(User: Jason)

onNext(User: Jay)

...

onComplete()

Page 49: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Usage

Consume two at a time

Page 50: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Output

onSubscribe

request(2)

onNext(User: Jason)

onNext(User: Jay)

request(2)

onNext(User: Joe)

onNext(User: John)

...

Page 51: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

❖ Currently 2.5 M4 (might change to 3.0 label)

❖ GA release scheduled for July

❖ Hands-on exercise, blog post series

More on Reactor

Page 52: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Reactive Spring

Page 53: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

ReactiveSpring MVC ?

Page 54: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Annotated controllers

Page 55: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Controller Methods

Page 56: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Spring MVC Spring Web Reactive

Annotated controllers

Page 57: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

...

Page 58: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

...Mono<Object>

Page 59: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Spring MVC Spring Web Reactive

Servlet API ???

@MVC

Page 60: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Spring MVC Spring Web Reactive

Servlet API ???

Servlet Container ???

@MVC

Page 61: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Spring Web Reactive

@MVC

HTTPReactive Streams

Servlet 3.1 Reactor I/O RxNetty

Page 63: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

More Reactive Efforts

Page 64: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

Reactive Journey

Page 65: Reactive Programming for Java Developers - QCon … Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive

@rstoya05