SlideShare a Scribd company logo
1 of 65
Download to read offline
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

JAVA 8 Parallel Stream
JAVA 8 Parallel StreamJAVA 8 Parallel Stream
JAVA 8 Parallel StreamTengwen Wang
 
Reactive microservices with eclipse vert.x
Reactive microservices with eclipse vert.xReactive microservices with eclipse vert.x
Reactive microservices with eclipse vert.xRam Maddali
 
Reactive Web Development with Spring Boot 2
Reactive Web Development with Spring Boot 2Reactive Web Development with Spring Boot 2
Reactive Web Development with Spring Boot 2Mike Melusky
 
Isomorphic JavaScript – future of the web
Isomorphic JavaScript – future of the webIsomorphic JavaScript – future of the web
Isomorphic JavaScript – future of the webSigma Software
 
React - поглянемо по іншому на V в MVC
React - поглянемо по іншому на V в MVCReact - поглянемо по іншому на V в MVC
React - поглянемо по іншому на V в MVCStfalcon Meetups
 
App Mod 04: Reactive microservices with eclipse vert.x
App Mod 04: Reactive microservices with eclipse vert.xApp Mod 04: Reactive microservices with eclipse vert.x
App Mod 04: Reactive microservices with eclipse vert.xJudy Breedlove
 
001. Introduction about React
001. Introduction about React001. Introduction about React
001. Introduction about ReactBinh Quan Duc
 
Servlet vs Reactive Stacks in 5 Use Cases
Servlet vs Reactive Stacks in 5 Use CasesServlet vs Reactive Stacks in 5 Use Cases
Servlet vs Reactive Stacks in 5 Use CasesVMware Tanzu
 
Bringing Server Add-ons to the Cloud and Back Again
Bringing Server Add-ons to the Cloud and Back AgainBringing Server Add-ons to the Cloud and Back Again
Bringing Server Add-ons to the Cloud and Back AgainAtlassian
 
What is Mean Stack Development ?
What is Mean Stack Development ?What is Mean Stack Development ?
What is Mean Stack Development ?Balajihope
 
Javascript Client & Server Architectures
Javascript Client & Server ArchitecturesJavascript Client & Server Architectures
Javascript Client & Server ArchitecturesPedro Melo Pereira
 
Play Framework: Intro & High-Level Overview
Play Framework: Intro & High-Level OverviewPlay Framework: Intro & High-Level Overview
Play Framework: Intro & High-Level OverviewJosh Padnick
 
Overview of PaaS: Java experience
Overview of PaaS: Java experienceOverview of PaaS: Java experience
Overview of PaaS: Java experienceIgor Anishchenko
 
Overview of PaaS: Java experience
Overview of PaaS: Java experienceOverview of PaaS: Java experience
Overview of PaaS: Java experienceAlex Tumanoff
 
Java programming language
Java programming languageJava programming language
Java programming languageSubhashKumar329
 
Enterprise Java in 2012 and Beyond, by Juergen Hoeller
Enterprise Java in 2012 and Beyond, by Juergen Hoeller Enterprise Java in 2012 and Beyond, by Juergen Hoeller
Enterprise Java in 2012 and Beyond, by Juergen Hoeller Codemotion
 
High Availability for OpenStack
High Availability for OpenStackHigh Availability for OpenStack
High Availability for OpenStackKamesh Pemmaraju
 
Guide to Spring Reactive Programming using WebFlux
Guide to Spring Reactive Programming using WebFluxGuide to Spring Reactive Programming using WebFlux
Guide to Spring Reactive Programming using WebFluxInexture Solutions
 
Ppt for Online music store
Ppt for Online music storePpt for Online music store
Ppt for Online music storeADEEBANADEEM
 

Similar to reactive_programming_for_java_developers.pdf (20)

JAVA 8 Parallel Stream
JAVA 8 Parallel StreamJAVA 8 Parallel Stream
JAVA 8 Parallel Stream
 
Reactive microservices with eclipse vert.x
Reactive microservices with eclipse vert.xReactive microservices with eclipse vert.x
Reactive microservices with eclipse vert.x
 
Reactive Web Development with Spring Boot 2
Reactive Web Development with Spring Boot 2Reactive Web Development with Spring Boot 2
Reactive Web Development with Spring Boot 2
 
Isomorphic JavaScript – future of the web
Isomorphic JavaScript – future of the webIsomorphic JavaScript – future of the web
Isomorphic JavaScript – future of the web
 
React - поглянемо по іншому на V в MVC
React - поглянемо по іншому на V в MVCReact - поглянемо по іншому на V в MVC
React - поглянемо по іншому на V в MVC
 
App Mod 04: Reactive microservices with eclipse vert.x
App Mod 04: Reactive microservices with eclipse vert.xApp Mod 04: Reactive microservices with eclipse vert.x
App Mod 04: Reactive microservices with eclipse vert.x
 
001. Introduction about React
001. Introduction about React001. Introduction about React
001. Introduction about React
 
Servlet vs Reactive Stacks in 5 Use Cases
Servlet vs Reactive Stacks in 5 Use CasesServlet vs Reactive Stacks in 5 Use Cases
Servlet vs Reactive Stacks in 5 Use Cases
 
Bringing Server Add-ons to the Cloud and Back Again
Bringing Server Add-ons to the Cloud and Back AgainBringing Server Add-ons to the Cloud and Back Again
Bringing Server Add-ons to the Cloud and Back Again
 
What is Mean Stack Development ?
What is Mean Stack Development ?What is Mean Stack Development ?
What is Mean Stack Development ?
 
Javascript Client & Server Architectures
Javascript Client & Server ArchitecturesJavascript Client & Server Architectures
Javascript Client & Server Architectures
 
Play Framework: Intro & High-Level Overview
Play Framework: Intro & High-Level OverviewPlay Framework: Intro & High-Level Overview
Play Framework: Intro & High-Level Overview
 
Overview of PaaS: Java experience
Overview of PaaS: Java experienceOverview of PaaS: Java experience
Overview of PaaS: Java experience
 
Overview of PaaS: Java experience
Overview of PaaS: Java experienceOverview of PaaS: Java experience
Overview of PaaS: Java experience
 
Java programming language
Java programming languageJava programming language
Java programming language
 
Enterprise Java in 2012 and Beyond, by Juergen Hoeller
Enterprise Java in 2012 and Beyond, by Juergen Hoeller Enterprise Java in 2012 and Beyond, by Juergen Hoeller
Enterprise Java in 2012 and Beyond, by Juergen Hoeller
 
OOPC_Unit-I.pdf
OOPC_Unit-I.pdfOOPC_Unit-I.pdf
OOPC_Unit-I.pdf
 
High Availability for OpenStack
High Availability for OpenStackHigh Availability for OpenStack
High Availability for OpenStack
 
Guide to Spring Reactive Programming using WebFlux
Guide to Spring Reactive Programming using WebFluxGuide to Spring Reactive Programming using WebFlux
Guide to Spring Reactive Programming using WebFlux
 
Ppt for Online music store
Ppt for Online music storePpt for Online music store
Ppt for Online music store
 

Recently uploaded

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 

Recently uploaded (20)

Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 

reactive_programming_for_java_developers.pdf