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

Nodejs functions & modules
Nodejs functions & modulesNodejs functions & modules
Nodejs functions & modulesmonikadeshmane
 
React native felicaの紹介
React native felicaの紹介React native felicaの紹介
React native felicaの紹介tanaka taro
 
ASP.NET Core MVC + Web API with Overview
ASP.NET Core MVC + Web API with OverviewASP.NET Core MVC + Web API with Overview
ASP.NET Core MVC + Web API with OverviewShahed Chowdhuri
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJSBrainhub
 
Angular & RXJS: examples and use cases
Angular & RXJS: examples and use casesAngular & RXJS: examples and use cases
Angular & RXJS: examples and use casesFabio Biondi
 
FullStack Reativo com Spring WebFlux + Angular
FullStack Reativo com Spring WebFlux + AngularFullStack Reativo com Spring WebFlux + Angular
FullStack Reativo com Spring WebFlux + AngularLoiane Groner
 
NodeJS guide for beginners
NodeJS guide for beginnersNodeJS guide for beginners
NodeJS guide for beginnersEnoch Joshua
 
REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with SpringJoshua Long
 
Introduction to kotlin coroutines
Introduction to kotlin coroutinesIntroduction to kotlin coroutines
Introduction to kotlin coroutinesNAVER Engineering
 
Asynchronous javascript
 Asynchronous javascript Asynchronous javascript
Asynchronous javascriptEman Mohamed
 
Angular and The Case for RxJS
Angular and The Case for RxJSAngular and The Case for RxJS
Angular and The Case for RxJSSandi Barr
 
ES2015 / ES6: Basics of modern Javascript
ES2015 / ES6: Basics of modern JavascriptES2015 / ES6: Basics of modern Javascript
ES2015 / ES6: Basics of modern JavascriptWojciech Dzikowski
 
OSMC 2022 | Ignite: Observability with Grafana & Prometheus for Kafka on Kube...
OSMC 2022 | Ignite: Observability with Grafana & Prometheus for Kafka on Kube...OSMC 2022 | Ignite: Observability with Grafana & Prometheus for Kafka on Kube...
OSMC 2022 | Ignite: Observability with Grafana & Prometheus for Kafka on Kube...NETWAYS
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to JavascriptAmit Tyagi
 
Node.js Express
Node.js  ExpressNode.js  Express
Node.js ExpressEyal Vardi
 

What's hot (20)

Nodejs functions & modules
Nodejs functions & modulesNodejs functions & modules
Nodejs functions & modules
 
React native felicaの紹介
React native felicaの紹介React native felicaの紹介
React native felicaの紹介
 
ASP.NET Core MVC + Web API with Overview
ASP.NET Core MVC + Web API with OverviewASP.NET Core MVC + Web API with Overview
ASP.NET Core MVC + Web API with Overview
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJS
 
Angular & RXJS: examples and use cases
Angular & RXJS: examples and use casesAngular & RXJS: examples and use cases
Angular & RXJS: examples and use cases
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
FullStack Reativo com Spring WebFlux + Angular
FullStack Reativo com Spring WebFlux + AngularFullStack Reativo com Spring WebFlux + Angular
FullStack Reativo com Spring WebFlux + Angular
 
NodeJS guide for beginners
NodeJS guide for beginnersNodeJS guide for beginners
NodeJS guide for beginners
 
REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
 
Introduction to kotlin coroutines
Introduction to kotlin coroutinesIntroduction to kotlin coroutines
Introduction to kotlin coroutines
 
Asynchronous javascript
 Asynchronous javascript Asynchronous javascript
Asynchronous javascript
 
Rxjs ppt
Rxjs pptRxjs ppt
Rxjs ppt
 
Angular and The Case for RxJS
Angular and The Case for RxJSAngular and The Case for RxJS
Angular and The Case for RxJS
 
Vertx
VertxVertx
Vertx
 
ES2015 / ES6: Basics of modern Javascript
ES2015 / ES6: Basics of modern JavascriptES2015 / ES6: Basics of modern Javascript
ES2015 / ES6: Basics of modern Javascript
 
TypeScript VS JavaScript.pptx
TypeScript VS JavaScript.pptxTypeScript VS JavaScript.pptx
TypeScript VS JavaScript.pptx
 
OSMC 2022 | Ignite: Observability with Grafana & Prometheus for Kafka on Kube...
OSMC 2022 | Ignite: Observability with Grafana & Prometheus for Kafka on Kube...OSMC 2022 | Ignite: Observability with Grafana & Prometheus for Kafka on Kube...
OSMC 2022 | Ignite: Observability with Grafana & Prometheus for Kafka on Kube...
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
React Native
React NativeReact Native
React Native
 
Node.js Express
Node.js  ExpressNode.js  Express
Node.js Express
 

Similar to Reactive Programming in Java 8 with Rx-Java

Reactive Thinking in Java
Reactive Thinking in JavaReactive Thinking in Java
Reactive Thinking in JavaYakov Fain
 
Fault Tolerance in Distributed Environment
Fault Tolerance in Distributed EnvironmentFault Tolerance in Distributed Environment
Fault Tolerance in Distributed EnvironmentOrkhan Gasimov
 
Distributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromqDistributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromqRuben Tan
 
Virtual Flink Forward 2020: How Streaming Helps Your Staging Environment and ...
Virtual Flink Forward 2020: How Streaming Helps Your Staging Environment and ...Virtual Flink Forward 2020: How Streaming Helps Your Staging Environment and ...
Virtual Flink Forward 2020: How Streaming Helps Your Staging Environment and ...Flink Forward
 
Reactive solutions using java 9 and spring reactor
Reactive solutions using java 9 and spring reactorReactive solutions using java 9 and spring reactor
Reactive solutions using java 9 and spring reactorOrenEzer1
 
Designing Fault Tolerant Microservices
Designing Fault Tolerant MicroservicesDesigning Fault Tolerant Microservices
Designing Fault Tolerant MicroservicesOrkhan Gasimov
 
Reactive programming with rx java
Reactive programming with rx javaReactive programming with rx java
Reactive programming with rx javaCongTrung Vnit
 
WTF is Twisted?
WTF is Twisted?WTF is Twisted?
WTF is Twisted?hawkowl
 
ITSubbotik - как скрестить ежа с ужом или подводные камни внедрения функциона...
ITSubbotik - как скрестить ежа с ужом или подводные камни внедрения функциона...ITSubbotik - как скрестить ежа с ужом или подводные камни внедрения функциона...
ITSubbotik - как скрестить ежа с ужом или подводные камни внедрения функциона...Vyacheslav Lapin
 
Rethinking the debugger
Rethinking the debuggerRethinking the debugger
Rethinking the debuggerIulian Dragos
 
Solve it Differently with Reactive Programming
Solve it Differently with Reactive ProgrammingSolve it Differently with Reactive Programming
Solve it Differently with Reactive ProgrammingSupun Dissanayake
 
Микросервисы со Spring Boot & Spring Cloud
Микросервисы со Spring Boot & Spring CloudМикросервисы со Spring Boot & Spring Cloud
Микросервисы со Spring Boot & Spring CloudVitebsk DSC
 
The Need of Cloud-Native Application
The Need of Cloud-Native ApplicationThe Need of Cloud-Native Application
The Need of Cloud-Native ApplicationEmiliano Pecis
 
Web development basics (Part-5)
Web development basics (Part-5)Web development basics (Part-5)
Web development basics (Part-5)Rajat Pratap Singh
 
Reactive Thinking in Java with RxJava2
Reactive Thinking in Java with RxJava2Reactive Thinking in Java with RxJava2
Reactive Thinking in Java with RxJava2Yakov Fain
 
Ansible benelux meetup - Amsterdam 27-5-2015
Ansible benelux meetup - Amsterdam 27-5-2015Ansible benelux meetup - Amsterdam 27-5-2015
Ansible benelux meetup - Amsterdam 27-5-2015Pavel Chunyayev
 
Getting started with Apollo Client and GraphQL
Getting started with Apollo Client and GraphQLGetting started with Apollo Client and GraphQL
Getting started with Apollo Client and GraphQLMorgan Dedmon
 
Meteor Boulder meetup #1
Meteor Boulder meetup #1Meteor Boulder meetup #1
Meteor Boulder meetup #1Robert Dickert
 
Building a document e-signing workflow with Azure Durable Functions
Building a document e-signing workflow with Azure Durable FunctionsBuilding a document e-signing workflow with Azure Durable Functions
Building a document e-signing workflow with Azure Durable FunctionsJoonas Westlin
 

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

Reactive Thinking in Java
Reactive Thinking in JavaReactive Thinking in Java
Reactive Thinking in Java
 
Fault Tolerance in Distributed Environment
Fault Tolerance in Distributed EnvironmentFault Tolerance in Distributed Environment
Fault Tolerance in Distributed Environment
 
Distributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromqDistributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromq
 
Virtual Flink Forward 2020: How Streaming Helps Your Staging Environment and ...
Virtual Flink Forward 2020: How Streaming Helps Your Staging Environment and ...Virtual Flink Forward 2020: How Streaming Helps Your Staging Environment and ...
Virtual Flink Forward 2020: How Streaming Helps Your Staging Environment and ...
 
Reactive solutions using java 9 and spring reactor
Reactive solutions using java 9 and spring reactorReactive solutions using java 9 and spring reactor
Reactive solutions using java 9 and spring reactor
 
Designing Fault Tolerant Microservices
Designing Fault Tolerant MicroservicesDesigning Fault Tolerant Microservices
Designing Fault Tolerant Microservices
 
JS Event Loop
JS Event LoopJS Event Loop
JS Event Loop
 
Reactive programming with rx java
Reactive programming with rx javaReactive programming with rx java
Reactive programming with rx java
 
WTF is Twisted?
WTF is Twisted?WTF is Twisted?
WTF is Twisted?
 
ITSubbotik - как скрестить ежа с ужом или подводные камни внедрения функциона...
ITSubbotik - как скрестить ежа с ужом или подводные камни внедрения функциона...ITSubbotik - как скрестить ежа с ужом или подводные камни внедрения функциона...
ITSubbotik - как скрестить ежа с ужом или подводные камни внедрения функциона...
 
Rethinking the debugger
Rethinking the debuggerRethinking the debugger
Rethinking the debugger
 
Solve it Differently with Reactive Programming
Solve it Differently with Reactive ProgrammingSolve it Differently with Reactive Programming
Solve it Differently with Reactive Programming
 
Микросервисы со Spring Boot & Spring Cloud
Микросервисы со Spring Boot & Spring CloudМикросервисы со Spring Boot & Spring Cloud
Микросервисы со Spring Boot & Spring Cloud
 
The Need of Cloud-Native Application
The Need of Cloud-Native ApplicationThe Need of Cloud-Native Application
The Need of Cloud-Native Application
 
Web development basics (Part-5)
Web development basics (Part-5)Web development basics (Part-5)
Web development basics (Part-5)
 
Reactive Thinking in Java with RxJava2
Reactive Thinking in Java with RxJava2Reactive Thinking in Java with RxJava2
Reactive Thinking in Java with RxJava2
 
Ansible benelux meetup - Amsterdam 27-5-2015
Ansible benelux meetup - Amsterdam 27-5-2015Ansible benelux meetup - Amsterdam 27-5-2015
Ansible benelux meetup - Amsterdam 27-5-2015
 
Getting started with Apollo Client and GraphQL
Getting started with Apollo Client and GraphQLGetting started with Apollo Client and GraphQL
Getting started with Apollo Client and GraphQL
 
Meteor Boulder meetup #1
Meteor Boulder meetup #1Meteor Boulder meetup #1
Meteor Boulder meetup #1
 
Building a document e-signing workflow with Azure Durable Functions
Building a document e-signing workflow with Azure Durable FunctionsBuilding a document e-signing workflow with Azure Durable Functions
Building a document e-signing workflow with Azure Durable Functions
 

More from Kasun Indrasiri

Microservices Integration Patterns with Kafka
Microservices Integration Patterns with KafkaMicroservices Integration Patterns with Kafka
Microservices Integration Patterns with KafkaKasun Indrasiri
 
Adaptive and Iterative Integration for Microservices and Cloud Native Archite...
Adaptive and Iterative Integration for Microservices and Cloud Native Archite...Adaptive and Iterative Integration for Microservices and Cloud Native Archite...
Adaptive and Iterative Integration for Microservices and Cloud Native Archite...Kasun Indrasiri
 
Microservices for Enterprises
Microservices for Enterprises Microservices for Enterprises
Microservices for Enterprises Kasun Indrasiri
 
Bridging Microservices, APIs and Integration
Bridging Microservices, APIs and IntegrationBridging Microservices, APIs and Integration
Bridging Microservices, APIs and IntegrationKasun Indrasiri
 
Integration Microservices
Integration MicroservicesIntegration Microservices
Integration MicroservicesKasun Indrasiri
 
The Role of Enterprise Integration in Digital Transformation
The Role of Enterprise Integration in Digital TransformationThe Role of Enterprise Integration in Digital Transformation
The Role of Enterprise Integration in Digital TransformationKasun Indrasiri
 
Deep-dive into WSO2 ESB 5.0
Deep-dive into WSO2 ESB 5.0 Deep-dive into WSO2 ESB 5.0
Deep-dive into WSO2 ESB 5.0 Kasun Indrasiri
 
Microservices in Practice
Microservices in PracticeMicroservices in Practice
Microservices in PracticeKasun Indrasiri
 
WSO2-ESB - The backbone of Enterprise Integration
WSO2-ESB - The backbone of Enterprise IntegrationWSO2-ESB - The backbone of Enterprise Integration
WSO2-ESB - The backbone of Enterprise IntegrationKasun Indrasiri
 
Introduction to WSO2 Integration Platform
Introduction to WSO2 Integration PlatformIntroduction to WSO2 Integration Platform
Introduction to WSO2 Integration PlatformKasun Indrasiri
 
The Middleware technology that connects the enterprise
The Middleware technology that connects the enterpriseThe Middleware technology that connects the enterprise
The Middleware technology that connects the enterprise Kasun Indrasiri
 
API, Integration, and SOA Convergence
API, Integration, and SOA ConvergenceAPI, Integration, and SOA Convergence
API, Integration, and SOA ConvergenceKasun Indrasiri
 
The Past, Present and Future of Enterprise Integration
The Past, Present and Future of  Enterprise IntegrationThe Past, Present and Future of  Enterprise Integration
The Past, Present and Future of Enterprise IntegrationKasun Indrasiri
 
WSO2 ESB - The Backbone of Integration
WSO2 ESB - The Backbone of Integration WSO2 ESB - The Backbone of Integration
WSO2 ESB - The Backbone of Integration Kasun Indrasiri
 

More from Kasun Indrasiri (17)

Microservices Integration Patterns with Kafka
Microservices Integration Patterns with KafkaMicroservices Integration Patterns with Kafka
Microservices Integration Patterns with Kafka
 
Adaptive and Iterative Integration for Microservices and Cloud Native Archite...
Adaptive and Iterative Integration for Microservices and Cloud Native Archite...Adaptive and Iterative Integration for Microservices and Cloud Native Archite...
Adaptive and Iterative Integration for Microservices and Cloud Native Archite...
 
Microservices for Enterprises
Microservices for Enterprises Microservices for Enterprises
Microservices for Enterprises
 
Bridging Microservices, APIs and Integration
Bridging Microservices, APIs and IntegrationBridging Microservices, APIs and Integration
Bridging Microservices, APIs and Integration
 
Integration Microservices
Integration MicroservicesIntegration Microservices
Integration Microservices
 
The Role of Enterprise Integration in Digital Transformation
The Role of Enterprise Integration in Digital TransformationThe Role of Enterprise Integration in Digital Transformation
The Role of Enterprise Integration in Digital Transformation
 
Deep-dive into WSO2 ESB 5.0
Deep-dive into WSO2 ESB 5.0 Deep-dive into WSO2 ESB 5.0
Deep-dive into WSO2 ESB 5.0
 
Microintegration
MicrointegrationMicrointegration
Microintegration
 
Microservices in Practice
Microservices in PracticeMicroservices in Practice
Microservices in Practice
 
WSO2-ESB - The backbone of Enterprise Integration
WSO2-ESB - The backbone of Enterprise IntegrationWSO2-ESB - The backbone of Enterprise Integration
WSO2-ESB - The backbone of Enterprise Integration
 
Introduction to WSO2 Integration Platform
Introduction to WSO2 Integration PlatformIntroduction to WSO2 Integration Platform
Introduction to WSO2 Integration Platform
 
WSO2 Gateway
WSO2 GatewayWSO2 Gateway
WSO2 Gateway
 
The Middleware technology that connects the enterprise
The Middleware technology that connects the enterpriseThe Middleware technology that connects the enterprise
The Middleware technology that connects the enterprise
 
API, Integration, and SOA Convergence
API, Integration, and SOA ConvergenceAPI, Integration, and SOA Convergence
API, Integration, and SOA Convergence
 
The Past, Present and Future of Enterprise Integration
The Past, Present and Future of  Enterprise IntegrationThe Past, Present and Future of  Enterprise Integration
The Past, Present and Future of Enterprise Integration
 
WSO2 ESB - The Backbone of Integration
WSO2 ESB - The Backbone of Integration WSO2 ESB - The Backbone of Integration
WSO2 ESB - The Backbone of Integration
 
Connected Retail
Connected RetailConnected Retail
Connected Retail
 

Recently uploaded

Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...Akihiro Suda
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 

Recently uploaded (20)

Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 

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