SlideShare a Scribd company logo
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

More Related Content

Similar to reactive_programming_for_java_developers.pdf (20)

PPTX
JAVA 8 Parallel Stream
Tengwen Wang
 
PDF
Reactive microservices with eclipse vert.x
Ram Maddali
 
PPTX
intro to react| React: dynamic UIs, component-based, virtual DOM, JSX, effici...
IbadUddin4
 
PPTX
Reactive Web Development with Spring Boot 2
Mike Melusky
 
PPTX
Isomorphic JavaScript – future of the web
Sigma Software
 
PPTX
React - поглянемо по іншому на V в MVC
Stfalcon Meetups
 
PPTX
App Mod 04: Reactive microservices with eclipse vert.x
Judy Breedlove
 
PPTX
001. Introduction about React
Binh Quan Duc
 
PDF
Servlet vs Reactive Stacks in 5 Use Cases
VMware Tanzu
 
PDF
Bringing Server Add-ons to the Cloud and Back Again
Atlassian
 
PPTX
What is Mean Stack Development ?
Balajihope
 
PDF
Javascript Client & Server Architectures
Pedro Melo Pereira
 
PDF
Play Framework: Intro & High-Level Overview
Josh Padnick
 
PDF
"Leveraging React's Concurrency for Optimal Performance", Dara Olayebi
Fwdays
 
PPTX
Overview of PaaS: Java experience
Alex Tumanoff
 
PPTX
Overview of PaaS: Java experience
Igor Anishchenko
 
PPTX
Java programming language
SubhashKumar329
 
ODP
Enterprise Java in 2012 and Beyond, by Juergen Hoeller
Codemotion
 
PDF
OOPC_Unit-I.pdf
KRaviChythanyaCSE
 
PDF
High Availability for OpenStack
Kamesh Pemmaraju
 
JAVA 8 Parallel Stream
Tengwen Wang
 
Reactive microservices with eclipse vert.x
Ram Maddali
 
intro to react| React: dynamic UIs, component-based, virtual DOM, JSX, effici...
IbadUddin4
 
Reactive Web Development with Spring Boot 2
Mike Melusky
 
Isomorphic JavaScript – future of the web
Sigma Software
 
React - поглянемо по іншому на V в MVC
Stfalcon Meetups
 
App Mod 04: Reactive microservices with eclipse vert.x
Judy Breedlove
 
001. Introduction about React
Binh Quan Duc
 
Servlet vs Reactive Stacks in 5 Use Cases
VMware Tanzu
 
Bringing Server Add-ons to the Cloud and Back Again
Atlassian
 
What is Mean Stack Development ?
Balajihope
 
Javascript Client & Server Architectures
Pedro Melo Pereira
 
Play Framework: Intro & High-Level Overview
Josh Padnick
 
"Leveraging React's Concurrency for Optimal Performance", Dara Olayebi
Fwdays
 
Overview of PaaS: Java experience
Alex Tumanoff
 
Overview of PaaS: Java experience
Igor Anishchenko
 
Java programming language
SubhashKumar329
 
Enterprise Java in 2012 and Beyond, by Juergen Hoeller
Codemotion
 
OOPC_Unit-I.pdf
KRaviChythanyaCSE
 
High Availability for OpenStack
Kamesh Pemmaraju
 

Recently uploaded (20)

PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
Quarter 1_PPT_PE & HEALTH 8_WEEK 3-4.pptx
ronajadolpnhs
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
Controller Request and Response in Odoo18
Celine George
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Quarter 1_PPT_PE & HEALTH 8_WEEK 3-4.pptx
ronajadolpnhs
 
Horarios de distribución de agua en julio
pegazohn1978
 

reactive_programming_for_java_developers.pdf