reactive programming for java developers - qcon new york · and java.util.stream best supports...

65
Reactive Programming for Java Developers Rossen Stoyanchev

Upload: others

Post on 16-Mar-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Reactive Programming forJava Developers

Rossen Stoyanchev

Page 2: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

About Me❖ Spring Framework committer

❖ Spring MVC, WebSocket messaging

❖ Spring 5 Reactive

Page 3: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Long-Running Shift to Concurrency

Page 4: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

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 New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Changing expectations

Internet scale & resilience,

Efficient use of resources,

Latency is common

Page 6: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

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 New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

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 New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Reactive Programming?

Page 9: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

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 New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Design async API in Java

Page 11: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Return one thing

...

...

Page 12: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Usage

Page 13: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Return it async style

...

May occur indifferent thread

Page 14: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Usage

Ugh

Page 15: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

CompletableFuture (JDK 1.8)❖ Future with actions

❖ Actions trigger when Future completes

❖ Callback mechanism

Page 16: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Return it async style with Java 1.8

...

...

Page 17: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Usage

Async callback!

Page 18: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Usage

Requires null check

Page 19: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Return many

...

...

Page 20: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Return many

...

... No callback till all users collected

Page 21: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Return many

...

... It may be too many

Page 22: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Return nothing

...

...

Page 23: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Return nothing

...

...

Async notification:success or failure?

Page 24: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Can we do better?

Page 25: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

❖ 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 New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

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 New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

➢ Functional, declarative programming model

➢ Combine, transform, reduce sequences

➢ Focus on what, not how

Stream abstraction

Page 28: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

➢ 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 New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

➢ Latency-sensitive data streams

➢ Infinite sequences

➢ Push-based notifications

Beyond collections

Page 30: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Reactive Libraries

Page 31: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

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 New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

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 New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Flux -- sequence of 0..N

Page 34: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Mono -- sequence of 0..1

Page 35: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Flux to Java Stream

Page 36: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Mono to CompletableFuture

Page 37: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

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 New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

❖ Industry collaboration

❖ Small API, rules, TCK

❖ Reactive interoperability across libraries

Reactive Streams Spec

Page 39: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

“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 New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

❖ 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 New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Reactive Streams API

public interface Publisher<T> {

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

}

Page 42: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

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 New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

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 New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Reactive repository

Page 45: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Using the reactive repository

Page 46: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Using the reactive repository

Subscriber triggers flow of data

Page 47: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Using the reactive repository

Consume all data by default

Page 48: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

OutputonSubscribe

request(unbounded)

onNext(User: Jason)

onNext(User: Jay)

...

onComplete()

Page 49: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Usage

Consume two at a time

Page 50: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

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 New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

❖ 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 New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Reactive Spring

Page 53: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

ReactiveSpring MVC ?

Page 54: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Annotated controllers

Page 55: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Controller Methods

Page 56: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Spring MVC Spring Web Reactive

Annotated controllers

Page 57: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

...

Page 58: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

...Mono<Object>

Page 59: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Spring MVC Spring Web Reactive

Servlet API ???

@MVC

Page 60: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Spring MVC Spring Web Reactive

Servlet API ???

Servlet Container ???

@MVC

Page 61: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Spring Web Reactive

@MVC

HTTPReactive Streams

Servlet 3.1 Reactor I/O RxNetty

Page 63: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

More Reactive Efforts

Page 64: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

Reactive Journey

Page 65: Reactive Programming for Java Developers - QCon New York · and java.util.stream best supports (multi-stage, possibly-parallel) "pull" style operations on the elements of collections

@rstoya05