SlideShare a Scribd company logo
Reactive Programming in Java 8
with Rx-Java
Kasun Indrasiri
Software Architect, WSO2
January 2016
Why Reactive Programming?
• Why another programming paradigm?
• Things are different now!
• Conventional software applications won’t be able to fulfill the
modern enterprise needs.
• Its time to rethink about our software architecture patterns and
programming techniques.
What it actually means to be Reactive?
• “Readily responsive to a stimulus” - merriam-webster
• Reactive Systems :
• responds in a timely manner
• stays responsive in the face of failure
• stays responsive under varying workload
• rely on asynchronous message-passing
Reactive Programming
• A programming paradigm that helps you to build
‘Reactive Systems’. E.g.: Google Spreadsheet
• In a reactive world, we can't just wait for a function
result, a network call, or a database query to return.
• Every moment we wait for something, we lose the
opportunity to do other things in parallel.
• Reactive programming can be done in several ways
• Akka Actors
• Reactive Extensions (Rx) – reactive + functional
Why Rx-Java?
• The Netflix story..
The Netflix API
• How to reducing network chattiness?
• Granular API.
• client applications to make multiple calls that need to be assembled in order
to render a single user experience
The Netflix API
• Discrete requests from devices, should be collapsed into
a single request
• Server-side concurrency is needed to effectively reduce
network chattiness.
• Nested, conditional, parallel execution of backend
network calls.
Embrace Concurrency
• Blocking calls?... Not anymore.
• Server-side concurrency is a must.
• Low level primitive of concurrency.
• Concurrent programming is hard!
Java Futures
• Represents the result of an asynchronous computation.
• Can be used to retrieve the result asynchronously at a
later point in time
• Cons:
• Retrieving value from future is a blocking call : Future.get()
• Messy with Nested Futures -> complex to implement/maintain.
Callbacks
• A callback is a piece of executable code that is passed as
an argument to other code, which is expected to call
back the argument at some convenient time.
Callbacks
• Use case :
• Client send a request to the Application
• Application calls service A
• Get the response from ServiceA and send it to service B
• Get the response from ServiceB and send it to Service C
• Get the response from ServiceC and send it to the Client
Service A
Service B
Service C
1
2
3
ApplicationClient
Callbacks
• Callback Hell!
• Callbacks based applications get extremely complex when we have nested
callbacks.
• Leads to messy code, hard to troubleshoot.
• Example : https://github.com/kasun04/rnd/tree/master/rx-
java/src/main/java/org/kasun/rnd/rxjava/serviceinvoker/callback
Reactive Extensions - Rx
• ReactiveX is a library for composing asynchronous and
event-based programs by using observable sequences.
• ‘The Observer pattern done right.’
• Initially implemented for C# by Microsoft, later ported to
many languages by Netflix Rx.
• Rx-Java is the Java implementation.
Observer Pattern
• ‘Subject’ notifies the ‘observers’ when a change has
happened in the subject.
Observable
• Rx-Java introduces ‘Observable’ data type by extending
the traditional Observer Pattern.
Observers
Observable
Observable vs Observer Pattern
• The producer signal that there is no more data available:
the
• onCompleted() event
• The producer can signal that an error occurred
• onError() event
Observable and Subscriber
• The producer signal that there is no more data available:
the
ObservableSubscriber
subscribe
onNext*
onCompleted
Observable and Subscriber – In Action
Creating Observable
• Observable.create()
• Observable.just()
• Observable.from()
Observable and Operators
• Observable -> Operator -> Subscriber
Filtering Observables
• Selectively emit items from a source Observable
Filtering Observables
• Filtering Operators
Transforming Observables
• Transform items that are emitted by an Observable
Transforming Observables
• Transforming Operators
Filtering and Transforming – In Action
Transforming Observables
• FlatMap
• Observable.flatMap() takes the emissions of one Observable and returns the
emissions of another Observable to take its place.
Observable<List<String>> query(String text){...}
query("Hello, world!")
.flatMap(new Func1<List<String>, Observable<String>>() {
@Override
public Observable<String> call(List<String> urls) {
return Observable.from(urls);
}
})
.subscribe(url -> System.out.println(url));
FlatMap – In Action
More Operators
• Combining Observables
• Error Handling Operators
• Observable Utility Operators
• Conditional and Boolean Operators
http://reactivex.io/documentation/operators.html
Rx-Java in Action
• Use case : Service Orchestration
Source Code : https://github.com/kasun04/rnd/tree/master/rx-
java/src/main/java/org/kasun/rnd/rxjava/serviceinvoker/rx/nonblocking
Service A
Service B
Service C
1
2
3
ApplicationClient
Scheduler
• Rx is single threaded by default.
• Schedulers are the easiest way to bring multi-threading
into your apps.
• subscribeOn
• observerOn
Scheduler
• Schedulers.io(): meant for I/O-bound work
• Schedulers.computation(): meant for computational
work such as event-loops
• Schedulers.newThread(): creates a new thread for
each unit of work
Rx-Java in Action
• Use case : Service Orchestration with Schedulers
Source Code : https://github.com/kasun04/rnd/tree/master/rx-
java/src/main/java/org/kasun/rnd/rxjava/serviceinvoker/rx/nonblocking
Service A
Service B
Service C
1
2
3
ApplicationClient
Conclusion
• Get rid of blocking code!
• Think ‘non-blocking’
• Think reactive
• Rx-Java – steep learning curve
• Develop use cases
• Leverage Java 8
Questions?
References
• http://reactivex.io/documentation
• http://blog.danlew.net/2014/09/15/grokking-rxjava-
part-1/
• http://techblog.netflix.com/2013/02/rxjava-netflix-
api.html
• Source Code :
https://github.com/kasun04/rnd/tree/master/rx-java
Thank You

More Related Content

What's hot (20)

PPTX
Introduction to GraphQL
Bhargav Anadkat
 
PDF
Solve cross cutting concerns with aspect oriented programming (aop)
Siva Prasad Rao Janapati
 
PDF
Reactive Programming in Java and Spring Framework 5
Richard Langlois P. Eng.
 
PDF
Introduction to Spring webflux
Knoldus Inc.
 
PDF
Angular 2 observables
Geoffrey Filippi
 
PPTX
React js
Nikhil Karkra
 
PDF
Loom Virtual Threads in the JDK 19
José Paumard
 
PDF
React Router: React Meetup XXL
Rob Gietema
 
PDF
WEB DEVELOPMENT USING REACT JS
MuthuKumaran Singaravelu
 
PPTX
Introduction to React JS
Lohith Goudagere Nagaraj
 
PPTX
What is Ajax technology?
JavaTpoint.Com
 
PDF
pg / shardman: шардинг в PostgreSQL на основе postgres / fdw, pg / pathman и ...
Ontico
 
PDF
Garage4Hackers Ranchoddas Webcast Series - Bypassing Modern WAF's Exemplified...
Garage4hackers.com
 
PPTX
Spring integration
Dominik Strzyżewski
 
PDF
Introduction to Spring WebFlux #jsug #sf_a1
Toshiaki Maki
 
PPTX
What is component in reactjs
manojbkalla
 
PPTX
Part One: Building Web Apps with the MERN Stack
MongoDB
 
PDF
Service workers
jungkees
 
PDF
Introducing Async/Await
Valeri Karpov
 
PPTX
Spring Webflux
Carlos E. Salazar
 
Introduction to GraphQL
Bhargav Anadkat
 
Solve cross cutting concerns with aspect oriented programming (aop)
Siva Prasad Rao Janapati
 
Reactive Programming in Java and Spring Framework 5
Richard Langlois P. Eng.
 
Introduction to Spring webflux
Knoldus Inc.
 
Angular 2 observables
Geoffrey Filippi
 
React js
Nikhil Karkra
 
Loom Virtual Threads in the JDK 19
José Paumard
 
React Router: React Meetup XXL
Rob Gietema
 
WEB DEVELOPMENT USING REACT JS
MuthuKumaran Singaravelu
 
Introduction to React JS
Lohith Goudagere Nagaraj
 
What is Ajax technology?
JavaTpoint.Com
 
pg / shardman: шардинг в PostgreSQL на основе postgres / fdw, pg / pathman и ...
Ontico
 
Garage4Hackers Ranchoddas Webcast Series - Bypassing Modern WAF's Exemplified...
Garage4hackers.com
 
Spring integration
Dominik Strzyżewski
 
Introduction to Spring WebFlux #jsug #sf_a1
Toshiaki Maki
 
What is component in reactjs
manojbkalla
 
Part One: Building Web Apps with the MERN Stack
MongoDB
 
Service workers
jungkees
 
Introducing Async/Await
Valeri Karpov
 
Spring Webflux
Carlos E. Salazar
 

Similar to Reactive Programming in Java 8 with Rx-Java (20)

PDF
RxJava - introduction & design
allegro.tech
 
PDF
The Mayans Lost Guide to RxJava on Android
Fernando Cejas
 
PDF
Reactive java - Reactive Programming + RxJava
NexThoughts Technologies
 
PPTX
Intro to Functional Programming with RxJava
Mike Nakhimovich
 
PDF
Reactive Programming in Java by Mario Fusco - Codemotion Rome 2015
Codemotion
 
PDF
Reactive Programming for a demanding world: building event-driven and respons...
Mario Fusco
 
PDF
Streams, Streams Everywhere! An Introduction to Rx
Andrzej Sitek
 
PPTX
Mario Fusco - Reactive programming in Java - Codemotion Milan 2017
Codemotion
 
PPTX
Microservices Part 4: Functional Reactive Programming
Araf Karsh Hamid
 
PPTX
Rx presentation
Ali Mahfud
 
PPTX
Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
PPTX
Introduction to RxJava on Android
Chris Arriola
 
PPTX
Functional reactive programming
Araf Karsh Hamid
 
PPTX
Reactive Programming on Android - RxAndroid - RxJava
Ali Muzaffar
 
PDF
Building Scalable Stateless Applications with RxJava
Rick Warren
 
PPTX
RxJava 2 Reactive extensions for the JVM
Netesh Kumar
 
PDF
RxJava@DAUG
Maxim Volgin
 
PPTX
Reactive programming with rx java
CongTrung Vnit
 
PDF
Sperasoft Talks: RxJava Functional Reactive Programming on Android
Sperasoft
 
PPTX
Reactive Programming with RxJava
Grand Parade Poland
 
RxJava - introduction & design
allegro.tech
 
The Mayans Lost Guide to RxJava on Android
Fernando Cejas
 
Reactive java - Reactive Programming + RxJava
NexThoughts Technologies
 
Intro to Functional Programming with RxJava
Mike Nakhimovich
 
Reactive Programming in Java by Mario Fusco - Codemotion Rome 2015
Codemotion
 
Reactive Programming for a demanding world: building event-driven and respons...
Mario Fusco
 
Streams, Streams Everywhere! An Introduction to Rx
Andrzej Sitek
 
Mario Fusco - Reactive programming in Java - Codemotion Milan 2017
Codemotion
 
Microservices Part 4: Functional Reactive Programming
Araf Karsh Hamid
 
Rx presentation
Ali Mahfud
 
Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
Introduction to RxJava on Android
Chris Arriola
 
Functional reactive programming
Araf Karsh Hamid
 
Reactive Programming on Android - RxAndroid - RxJava
Ali Muzaffar
 
Building Scalable Stateless Applications with RxJava
Rick Warren
 
RxJava 2 Reactive extensions for the JVM
Netesh Kumar
 
RxJava@DAUG
Maxim Volgin
 
Reactive programming with rx java
CongTrung Vnit
 
Sperasoft Talks: RxJava Functional Reactive Programming on Android
Sperasoft
 
Reactive Programming with RxJava
Grand Parade Poland
 
Ad

More from Kasun Indrasiri (17)

PDF
Microservices Integration Patterns with Kafka
Kasun Indrasiri
 
PPTX
Adaptive and Iterative Integration for Microservices and Cloud Native Archite...
Kasun Indrasiri
 
PDF
Microservices for Enterprises
Kasun Indrasiri
 
PDF
Bridging Microservices, APIs and Integration
Kasun Indrasiri
 
PDF
Integration Microservices
Kasun Indrasiri
 
PPTX
The Role of Enterprise Integration in Digital Transformation
Kasun Indrasiri
 
PDF
Deep-dive into WSO2 ESB 5.0
Kasun Indrasiri
 
PDF
Microintegration
Kasun Indrasiri
 
PDF
Microservices in Practice
Kasun Indrasiri
 
PPT
WSO2-ESB - The backbone of Enterprise Integration
Kasun Indrasiri
 
PPTX
Introduction to WSO2 Integration Platform
Kasun Indrasiri
 
PPTX
WSO2 Gateway
Kasun Indrasiri
 
PPTX
The Middleware technology that connects the enterprise
Kasun Indrasiri
 
PDF
API, Integration, and SOA Convergence
Kasun Indrasiri
 
PDF
The Past, Present and Future of Enterprise Integration
Kasun Indrasiri
 
PPTX
WSO2 ESB - The Backbone of Integration
Kasun Indrasiri
 
PPTX
Connected Retail
Kasun Indrasiri
 
Microservices Integration Patterns with Kafka
Kasun Indrasiri
 
Adaptive and Iterative Integration for Microservices and Cloud Native Archite...
Kasun Indrasiri
 
Microservices for Enterprises
Kasun Indrasiri
 
Bridging Microservices, APIs and Integration
Kasun Indrasiri
 
Integration Microservices
Kasun Indrasiri
 
The Role of Enterprise Integration in Digital Transformation
Kasun Indrasiri
 
Deep-dive into WSO2 ESB 5.0
Kasun Indrasiri
 
Microintegration
Kasun Indrasiri
 
Microservices in Practice
Kasun Indrasiri
 
WSO2-ESB - The backbone of Enterprise Integration
Kasun Indrasiri
 
Introduction to WSO2 Integration Platform
Kasun Indrasiri
 
WSO2 Gateway
Kasun Indrasiri
 
The Middleware technology that connects the enterprise
Kasun Indrasiri
 
API, Integration, and SOA Convergence
Kasun Indrasiri
 
The Past, Present and Future of Enterprise Integration
Kasun Indrasiri
 
WSO2 ESB - The Backbone of Integration
Kasun Indrasiri
 
Connected Retail
Kasun Indrasiri
 
Ad

Recently uploaded (20)

PDF
Salesforce Experience Cloud Consultant.pdf
VALiNTRY360
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PDF
ERP Consulting Services and Solutions by Contetra Pvt Ltd
jayjani123
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
UITP Summit Meep Pitch may 2025 MaaS Rebooted
campoamor1
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PPTX
Get Started with Maestro: Agent, Robot, and Human in Action – Session 5 of 5
klpathrudu
 
PPTX
Build a Custom Agent for Agentic Testing.pptx
klpathrudu
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PDF
Add Background Images to Charts in IBM SPSS Statistics Version 31.pdf
Version 1 Analytics
 
PDF
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
PDF
Dipole Tech Innovations – Global IT Solutions for Business Growth
dipoletechi3
 
Salesforce Experience Cloud Consultant.pdf
VALiNTRY360
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
ERP Consulting Services and Solutions by Contetra Pvt Ltd
jayjani123
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
UITP Summit Meep Pitch may 2025 MaaS Rebooted
campoamor1
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Get Started with Maestro: Agent, Robot, and Human in Action – Session 5 of 5
klpathrudu
 
Build a Custom Agent for Agentic Testing.pptx
klpathrudu
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
Add Background Images to Charts in IBM SPSS Statistics Version 31.pdf
Version 1 Analytics
 
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
Dipole Tech Innovations – Global IT Solutions for Business Growth
dipoletechi3
 

Reactive Programming in Java 8 with Rx-Java

  • 1. Reactive Programming in Java 8 with Rx-Java Kasun Indrasiri Software Architect, WSO2 January 2016
  • 2. Why Reactive Programming? • Why another programming paradigm? • Things are different now! • Conventional software applications won’t be able to fulfill the modern enterprise needs. • Its time to rethink about our software architecture patterns and programming techniques.
  • 3. What it actually means to be Reactive? • “Readily responsive to a stimulus” - merriam-webster • Reactive Systems : • responds in a timely manner • stays responsive in the face of failure • stays responsive under varying workload • rely on asynchronous message-passing
  • 4. Reactive Programming • A programming paradigm that helps you to build ‘Reactive Systems’. E.g.: Google Spreadsheet • In a reactive world, we can't just wait for a function result, a network call, or a database query to return. • Every moment we wait for something, we lose the opportunity to do other things in parallel. • Reactive programming can be done in several ways • Akka Actors • Reactive Extensions (Rx) – reactive + functional
  • 5. Why Rx-Java? • The Netflix story..
  • 6. The Netflix API • How to reducing network chattiness? • Granular API. • client applications to make multiple calls that need to be assembled in order to render a single user experience
  • 7. The Netflix API • Discrete requests from devices, should be collapsed into a single request • Server-side concurrency is needed to effectively reduce network chattiness. • Nested, conditional, parallel execution of backend network calls.
  • 8. Embrace Concurrency • Blocking calls?... Not anymore. • Server-side concurrency is a must. • Low level primitive of concurrency. • Concurrent programming is hard!
  • 9. Java Futures • Represents the result of an asynchronous computation. • Can be used to retrieve the result asynchronously at a later point in time • Cons: • Retrieving value from future is a blocking call : Future.get() • Messy with Nested Futures -> complex to implement/maintain.
  • 10. Callbacks • A callback is a piece of executable code that is passed as an argument to other code, which is expected to call back the argument at some convenient time.
  • 11. Callbacks • Use case : • Client send a request to the Application • Application calls service A • Get the response from ServiceA and send it to service B • Get the response from ServiceB and send it to Service C • Get the response from ServiceC and send it to the Client Service A Service B Service C 1 2 3 ApplicationClient
  • 12. Callbacks • Callback Hell! • Callbacks based applications get extremely complex when we have nested callbacks. • Leads to messy code, hard to troubleshoot. • Example : https://github.com/kasun04/rnd/tree/master/rx- java/src/main/java/org/kasun/rnd/rxjava/serviceinvoker/callback
  • 13. Reactive Extensions - Rx • ReactiveX is a library for composing asynchronous and event-based programs by using observable sequences. • ‘The Observer pattern done right.’ • Initially implemented for C# by Microsoft, later ported to many languages by Netflix Rx. • Rx-Java is the Java implementation.
  • 14. Observer Pattern • ‘Subject’ notifies the ‘observers’ when a change has happened in the subject.
  • 15. Observable • Rx-Java introduces ‘Observable’ data type by extending the traditional Observer Pattern. Observers Observable
  • 16. Observable vs Observer Pattern • The producer signal that there is no more data available: the • onCompleted() event • The producer can signal that an error occurred • onError() event
  • 17. Observable and Subscriber • The producer signal that there is no more data available: the ObservableSubscriber subscribe onNext* onCompleted
  • 18. Observable and Subscriber – In Action
  • 19. Creating Observable • Observable.create() • Observable.just() • Observable.from()
  • 20. Observable and Operators • Observable -> Operator -> Subscriber
  • 21. Filtering Observables • Selectively emit items from a source Observable
  • 23. Transforming Observables • Transform items that are emitted by an Observable
  • 25. Filtering and Transforming – In Action
  • 26. Transforming Observables • FlatMap • Observable.flatMap() takes the emissions of one Observable and returns the emissions of another Observable to take its place. Observable<List<String>> query(String text){...} query("Hello, world!") .flatMap(new Func1<List<String>, Observable<String>>() { @Override public Observable<String> call(List<String> urls) { return Observable.from(urls); } }) .subscribe(url -> System.out.println(url));
  • 27. FlatMap – In Action
  • 28. More Operators • Combining Observables • Error Handling Operators • Observable Utility Operators • Conditional and Boolean Operators http://reactivex.io/documentation/operators.html
  • 29. Rx-Java in Action • Use case : Service Orchestration Source Code : https://github.com/kasun04/rnd/tree/master/rx- java/src/main/java/org/kasun/rnd/rxjava/serviceinvoker/rx/nonblocking Service A Service B Service C 1 2 3 ApplicationClient
  • 30. Scheduler • Rx is single threaded by default. • Schedulers are the easiest way to bring multi-threading into your apps. • subscribeOn • observerOn
  • 31. Scheduler • Schedulers.io(): meant for I/O-bound work • Schedulers.computation(): meant for computational work such as event-loops • Schedulers.newThread(): creates a new thread for each unit of work
  • 32. Rx-Java in Action • Use case : Service Orchestration with Schedulers Source Code : https://github.com/kasun04/rnd/tree/master/rx- java/src/main/java/org/kasun/rnd/rxjava/serviceinvoker/rx/nonblocking Service A Service B Service C 1 2 3 ApplicationClient
  • 33. Conclusion • Get rid of blocking code! • Think ‘non-blocking’ • Think reactive • Rx-Java – steep learning curve • Develop use cases • Leverage Java 8
  • 35. References • http://reactivex.io/documentation • http://blog.danlew.net/2014/09/15/grokking-rxjava- part-1/ • http://techblog.netflix.com/2013/02/rxjava-netflix- api.html • Source Code : https://github.com/kasun04/rnd/tree/master/rx-java