Reactive Programming for Java Developers Rossen Stoyanchev
About Me ❖ Spring Framework committer ❖ Spring MVC, WebSocket messaging ❖ Spring 5 Reactive
Long-Running Shift to Concurrency
10 years ago Self-sufficient apps, App server, Keep it simple, don’t distribute Today Independent services, Cloud environment, Distributed apps
Changing expectations Internet scale & resilience, Efficient use of resources, Latency is common
Impact on programming model Imperative logic not so simple when latency is the norm Forced to deal with asynchronicity Limits of scale
There is another way Fundamentally async & non-blocking Using very few threads Major shift but also major benefits
Reactive Programming?
In this talk How would we design an async API in Java ? Can we do better ? Introducing reactive libraries Spring reactive experience
Design async API in Java
Return one thing ... ...
Usage
Return it async style ... May occur in different thread
Usage Ugh
CompletableFuture (JDK 1.8) ❖ Future with actions ❖ Actions trigger when Future completes ❖ Callback mechanism
Return it async style with Java 1.8 ... ...
Usage Async callback!
Usage Requires null check
Return many ... ...
Return many ... ... No callback till all users collected
Return many ... ... It may be too many
Return nothing ... ...
Return nothing ... ... Async notification: success or
Can we do better?
❖ One notification per data item ❖ One notification for either completion or error Async results as a stream
Return Type Description Notifications void 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)
➢ Functional, declarative programming model ➢ Combine, transform, reduce sequences ➢ Focus on what, not how Stream abstraction
➢ Great example of the benefits of a stream API ➢ However built for collections mainly ➢ Pull-based, usable once Java 8 Stream
➢ Latency-sensitive data streams ➢ Infinite sequences ➢ Push-based notifications Beyond collections
Reactive Libraries
Reactive library? ➢ Stream-like API similar to Java 8 ➢ Suited for any data sequence ➢ Latency-sensitive, infinite, collections
Project Reactor ➢ Reactive Streams foundation for the JVM ➢ API similar to ReactiveX ➢ Easy to bridge to Java 8 Stream
Flux -- sequence of 0..N
Mono -- sequence of 0..1
Flux to Java Stream
Mono to CompletableFuture
More than a stream API ➢ Reactor is back-pressure ready ➢ Reactive Streams spec ➢ Producers must not overwhelm consumers
❖ Industry collaboration ❖ Small API, rules, TCK ❖ Reactive interoperability across libraries Reactive Streams Spec
“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
❖ Interfaces in java.util.concurrent.Flow ❖ SubmissionPublisher standalone bridge to Reactive Streams ❖ Tie-ins to CompletableFuture and Stream Reactive Streams in Java 9
Reactive Streams API public interface Publisher<T> { void subscribe(Subscriber<? super T> subscriber); }
Reactive Streams API public interface Subscriber<T> { void onSubscribe(Subscription sub); void onNext(T item); void onError(Throwable ex); void onComplete(); }
Reactive Streams API public interface Subscriber<T> { void onSubscribe(Subscription sub); void onNext(T item); void onError(Throwable ex); void onComplete(); }
Reactive repository
Using the reactive repository
Using the reactive repository Subscriber triggers flow of data
Using the reactive repository Consume all data by default
Output onSubscribe request(unbounded) onNext(User: Jason) onNext(User: Jay) ... onComplete()
Usage Consume two at a time
Output onSubscribe request(2) onNext(User: Jason) onNext(User: Jay) request(2) onNext(User: Joe) onNext(User: John) ...
❖ Currently 2.5 M4 (might change to 3.0 label) ❖ GA release scheduled for July ❖ Hands-on exercise, blog post series More on Reactor
Reactive Spring
Reactive Spring MVC ?
Annotated controllers
Controller Methods
Spring MVC Spring Web Reactive Annotated controllers
...
... Mono<Object>
Spring MVC Spring Web Reactive Servlet API ??? @MVC
Spring MVC Spring Web Reactive Servlet API ??? Servlet Container ??? @MVC
Spring Web Reactive @MVC HTTP Reactive Streams Servlet 3.1 Reactor I/O RxNetty
spring-reactive Spring Framework 5.0 M1
More Reactive Efforts
Reactive Journey
@rstoya05

reactive_programming_for_java_developers.pdf