SlideShare a Scribd company logo
ReactiveJava =
Reactive Programming
using RxJava
Hello!
I AM Hiten Pratap Singh
I am here because I love to dig in to experience new things.
You can find me at:
◦ hiten@nexthoughts.com
◦ http://github.com/hitenpratap/
◦ http://hprog99.wordpress.com/
I have divided this presentation into two parts
Reactive
Programming
RxJava
Reactive Programming
What’s Reactive Programming? What’s the big deal about it?
Reactive java - Reactive Programming + RxJava
“
In computing, reactive programming is a
programming paradigm oriented around data
flows and the propagation of change.
Reactive Programming
Reactive programming is programming
with asynchronous data streams.
◦ Event buses or your typical click events are really
an asynchronous event stream
◦ Streams are cheap and ubiquitous, anything can be
a stream: variables, user inputs, properties etc.
◦ For example, imagine your Twitter feed would be
a data stream in the same fashion that click events
are.
Reactive Programming
On top of that, you are given an amazing
toolbox of functions to combine, create and
filter any of those streams.
◦ A stream can be used as an input to another one.
◦ You can merge two streams.
◦ You can filter a stream to get another one that has
only those events you are interested in.
◦ You can map data values from one stream to
another new one.
A stream is a sequence of ongoing events ordered in time. It can emit three
different things: a value (of some type), an error, or a "completed" signal.
Consider that the "completed" takes place, for instance, when the current window
or view containing that button is closed.
We capture these emitted events only asynchronously, by defining a function that
will execute when a value is emitted, another function when an error is emitted,
and another function when 'completed' is emitted.
The "listening" to the stream is called subscribing. The functions we are defining
are observers. The stream is the subject (or "observable") being observed. This is
precisely the Observer Design Pattern.
Reactive Programming
Why should I consider adopting RP?
◦ RP raises the level of abstraction of your code.
◦ Code in RP will likely be more concise.
◦ Apps nowadays have an abundancy of real-time
events of every kind that enable a highly
interactive experience to the user.
◦ We need tools for properly dealing with that, and
Reactive Programming is an answer.
Reactive Programming
Key Takeaways
◦ RP is a specification for dealing with
asynchronous streams of data.
◦ Reactive provides tools for transforming and
combining streams and for managing flow-control
◦ Resembles Java Streams API but the resemblance
is purely superficial
Thinking in RP with
Example
What’s ReactiveX?
◦ A library for composing async and event based
program by using observable sequences.
◦ Created by Microsoft initially for .Net platform by
Erik Meijer
◦ Extends observer pattern-
* Supports sequence of data and/or events
* Add operators that allow you to compose sequences
together declaratively
▫ Abstracts away concerns around
* Threading and Thread Safety
* Concurrent data structures and non blocking I/O
▫ RxJava is a port of Reactive Extensions created by
Netflix.
What’s ReactiveX?
RxJava
A libraryto use mightypowers of Reactive Programmingin Java!
Maven
Getting Started
Gradle
You can also include its jar file without having to use
any of build system as well.
Reactive Components/Building Blocks
ObserverObservable Subject
Observable
Observable
In ReactiveX an observer subscribes to an Observable. Then that
observer reacts to whatever item or sequence of items the
Observable emits.This patternfacilitatesconcurrent operations
because it does not need to block while waiting for the
Observable to emit objects,but insteadit creates a sentryin the
form of an observer that stands ready to react appropriatelyat
whatever future time the Observable does so.
◦ Emits zero or more values
◦ Life Cycles
* Notifies Observer using onNext(T)
* Completes with either onError() or onCompleted()
Reactive java - Reactive Programming + RxJava
Operators
Operators By Categories
Creating
Observables
Transforming
Observables
Filtering
Observables
Combining
Observables
Error
handling
Operators
Observable
Utility
Operators
Operators By Categories(Contd.)
Conditional
and Boolean
Operators
Mathematical
and
Aggregate
Operators
Backpressure
Operators
ReactiveX provides great flexibility over operators by
letting us chaining them together or even creating new
ones.
Transforming Observable
Transforming Observable << Map
Transforming Observable << Flatmap
Filtering Observable
Filtering Observable << Take
Filtering Observable << Filter
Filtering Observable << Distinct
Filtering Observable << First
Combining Observable
Combining Observable << Merge
Combining Observable << Zip
Schedulers
If you want to introduce multithreadingintoyour cascade of
Observable operators, you can do so by instructingthose operators
(or particular Observables) to operate on particular Schedulers.
Observable Utility Operators
Observable Utility Operators << SubscribeOn
Observable Utility Operators << ObserveOn
Single
Single
◦ RxJava (and its derivatives like RxGroovy & RxScala) has developed
an Observable variant called “Single.”
◦ A Single is something like an Observable, but instead of emitting a
series of values — anywhere from none at all to an infinite number
— it always either emits one value or an error notification.
◦ For this reason, instead of subscribing to a Single with the three
methods you use to respond to notifications from an Observable
(onNext, onError, and onCompleted), you only use two methods to
subscribe:
Subjects
Subject
A Subject is a sort of bridge or proxy that is available in some
implementations of ReactiveX that acts both as an observer and as an
Observable. Because it is an observer, it can subscribe to one or more
Observables, and because it is an Observable, it can pass through the
items it observes by reemitting them, and it can also emit new items.
Because a Subject subscribes to an Observable, it will trigger that
Observable to begin emitting items (if that Observable is “cold” — that
is, if it waits for a subscription before it begins to emit items). This can
have the effect of making the resulting Subject a “hot” Observable
variant of the original “cold” Observable.
Varieties of Subject
◦ AsyncSubject
◦ BehaviorSubject
◦ PublishSubject
◦ ReplaySubject
Subject << AsyncSubject
Subject << BehaviorSubject
Subject << PublishSubject
Subject << ReplaySubject
Backpressure
Backpressure
In RxJava it is not difficult to get into a situation in which an Observable
is emitting items more rapidly than an operator or subscriber can
consume them. This presents the problem of what to do with such a
growing backlog of unconsumed items.
For example, imagine using the zip operator to zip together two infinite
Observables, one of which emits items twice as frequently as the other.
A naive implementation of the zip operator would have to maintain an
ever-expanding buffer of items emitted by the faster Observable to
eventually combine with items emitted by the slower one. This could
cause RxJava to seize an unwieldy amount of system resources.
Hot and Cold
Observable
Hot Observable
A hot Observable begins generating items to emit immediately when it
is created. Subscribers typically begin observing the sequence of items
emitted by a hot Observable from somewhere in the middle of the
sequence, beginning with the first item emitted by the Observable
subsequent to the establishment of the subscription.
Such an Observable emits items at its own pace, and it is up to its
observers to keep up.
Examples of items emitted by a hot Observable might include mouse &
keyboard events, system events, or stock prices.
Cold Observable
A cold Observable emits a particular sequence of items, but can begin
emitting this sequence when its Observer finds it to be convenient, and
at whatever rate the Observer desires, without disrupting the integrity of
the sequence.
For example if you convert a static Iterable into an Observable, that
Observable will emit the same sequence of items no matter when it is
later subscribed to or how frequently those items are observed.
Examples of items emitted by a cold Observable might include the
results of a database query, file retrieval, or web request.
Conclusion
Pros
◦ Makes Async code easy to develop
◦ Lots of resources to get started with
◦ Strong use of functional type programming
◦ Netflix used RxNetty and Reactive Programmingto
considerable performance advantage over Tomcat
◦ Web Service clients like Jerseyand Retrofit support RxJava.
Cons
◦ Learning curve and mind set change to use functional reactive
programming
◦ Appears like more code and more complex code initially–
Requires time for it to grow on you.
◦ If on Java 7 anonymous class would make this a non-starter.
Thanks!
ANY QUESTIONS?
You can find me at:
hiten@nexthoughts.com
http://github.com/hitenpratap/
http://hprog99.wordpress.com/

More Related Content

What's hot (20)

Spring Boot Microservices vs Akka Actor Cluster
Spring Boot Microservices vs Akka Actor Cluster
OpenCredo
 
Demystifying Reactive Programming
Demystifying Reactive Programming
Tom Bulatewicz, PhD
 
Grails with swagger
Grails with swagger
NexThoughts Technologies
 
Reactive: Programming -> Systems -> Architecture
Reactive: Programming -> Systems -> Architecture
Aleksey Izmailov
 
Reactive programming
Reactive programming
SUDIP GHOSH
 
Understanding Reactive Programming
Understanding Reactive Programming
Andres Almiray
 
Reactive Programming In Java Using: Project Reactor
Reactive Programming In Java Using: Project Reactor
Knoldus Inc.
 
Declarative Concurrency with Reactive Programming
Declarative Concurrency with Reactive Programming
Florian Stefan
 
What’s expected in Spring 5
What’s expected in Spring 5
Gal Marder
 
Functional reactive programming
Functional reactive programming
Hung Hoang
 
Reactive Micro Services with Java seminar
Reactive Micro Services with Java seminar
Gal Marder
 
Load test REST APIs using gatling
Load test REST APIs using gatling
Jayaram Sankaranarayanan
 
Durable Functions vs Logic App : la guerra dei workflow!!
Durable Functions vs Logic App : la guerra dei workflow!!
Massimo Bonanni
 
Coordinating Micro-Services with Spring Cloud Contract
Coordinating Micro-Services with Spring Cloud Contract
Omri Spector
 
Introduction to Akka
Introduction to Akka
Knoldus Inc.
 
Introduction to akka actors with java 8
Introduction to akka actors with java 8
Johan Andrén
 
Discovering the Service Fabric's actor model
Discovering the Service Fabric's actor model
Massimo Bonanni
 
React gsg presentation with ryan jung &amp; elias malik
React gsg presentation with ryan jung &amp; elias malik
Lama K Banna
 
Gatling - Stress test tool
Gatling - Stress test tool
Knoldus Inc.
 
Introduction to Functional Reactive Programming
Introduction to Functional Reactive Programming
Đặng Thái Sơn
 
Spring Boot Microservices vs Akka Actor Cluster
Spring Boot Microservices vs Akka Actor Cluster
OpenCredo
 
Demystifying Reactive Programming
Demystifying Reactive Programming
Tom Bulatewicz, PhD
 
Reactive: Programming -> Systems -> Architecture
Reactive: Programming -> Systems -> Architecture
Aleksey Izmailov
 
Reactive programming
Reactive programming
SUDIP GHOSH
 
Understanding Reactive Programming
Understanding Reactive Programming
Andres Almiray
 
Reactive Programming In Java Using: Project Reactor
Reactive Programming In Java Using: Project Reactor
Knoldus Inc.
 
Declarative Concurrency with Reactive Programming
Declarative Concurrency with Reactive Programming
Florian Stefan
 
What’s expected in Spring 5
What’s expected in Spring 5
Gal Marder
 
Functional reactive programming
Functional reactive programming
Hung Hoang
 
Reactive Micro Services with Java seminar
Reactive Micro Services with Java seminar
Gal Marder
 
Durable Functions vs Logic App : la guerra dei workflow!!
Durable Functions vs Logic App : la guerra dei workflow!!
Massimo Bonanni
 
Coordinating Micro-Services with Spring Cloud Contract
Coordinating Micro-Services with Spring Cloud Contract
Omri Spector
 
Introduction to Akka
Introduction to Akka
Knoldus Inc.
 
Introduction to akka actors with java 8
Introduction to akka actors with java 8
Johan Andrén
 
Discovering the Service Fabric's actor model
Discovering the Service Fabric's actor model
Massimo Bonanni
 
React gsg presentation with ryan jung &amp; elias malik
React gsg presentation with ryan jung &amp; elias malik
Lama K Banna
 
Gatling - Stress test tool
Gatling - Stress test tool
Knoldus Inc.
 
Introduction to Functional Reactive Programming
Introduction to Functional Reactive Programming
Đặng Thái Sơn
 

Viewers also liked (16)

Jsoup
Jsoup
NexThoughts Technologies
 
Introduction to gradle
Introduction to gradle
NexThoughts Technologies
 
Introduction to es6
Introduction to es6
NexThoughts Technologies
 
Hamcrest
Hamcrest
NexThoughts Technologies
 
Apache tika
Apache tika
NexThoughts Technologies
 
Introduction to thymeleaf
Introduction to thymeleaf
NexThoughts Technologies
 
Progressive Web-App (PWA)
Progressive Web-App (PWA)
NexThoughts Technologies
 
Jmh
Jmh
NexThoughts Technologies
 
JFree chart
JFree chart
NexThoughts Technologies
 
RESTEasy
RESTEasy
NexThoughts Technologies
 
Spring Web Flow
Spring Web Flow
NexThoughts Technologies
 
Unit test-using-spock in Grails
Unit test-using-spock in Grails
NexThoughts Technologies
 
Cosmos DB Service
Cosmos DB Service
NexThoughts Technologies
 
Actors model in gpars
Actors model in gpars
NexThoughts Technologies
 
Java 8 features
Java 8 features
NexThoughts Technologies
 
Vertx
Vertx
NexThoughts Technologies
 
Ad

Similar to Reactive java - Reactive Programming + RxJava (20)

RxJava - introduction & design
RxJava - introduction & design
allegro.tech
 
Reactive Programming in Java by Mario Fusco - Codemotion Rome 2015
Reactive Programming in Java by Mario Fusco - Codemotion Rome 2015
Codemotion
 
Reactive Programming for a demanding world: building event-driven and respons...
Reactive Programming for a demanding world: building event-driven and respons...
Mario Fusco
 
RxJava 2 Reactive extensions for the JVM
RxJava 2 Reactive extensions for the JVM
Netesh Kumar
 
Reactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-Java
Kasun Indrasiri
 
Streams, Streams Everywhere! An Introduction to Rx
Streams, Streams Everywhere! An Introduction to Rx
Andrzej Sitek
 
An Introduction to RxJava
An Introduction to RxJava
Sanjay Acharya
 
Reactive x
Reactive x
Gabriel Araujo
 
Introduction to RxJava on Android
Introduction to RxJava on Android
Chris Arriola
 
Intro to Functional Programming with RxJava
Intro to Functional Programming with RxJava
Mike Nakhimovich
 
Strange Async Code - ReaxtiveX
Strange Async Code - ReaxtiveX
Matthew Will
 
Intro to Rx Java
Intro to Rx Java
Syed Awais Mazhar Bukhari
 
Reactive Functional Programming with Java 8 on Android N
Reactive Functional Programming with Java 8 on Android N
Shipeng Xu
 
RxJava@DAUG
RxJava@DAUG
Maxim Volgin
 
Mario Fusco - Reactive programming in Java - Codemotion Milan 2017
Mario Fusco - Reactive programming in Java - Codemotion Milan 2017
Codemotion
 
RxJava@Android
RxJava@Android
Maxim Volgin
 
Reactive Programming with RxJava
Reactive Programming with RxJava
Grand Parade Poland
 
Microservices Part 4: Functional Reactive Programming
Microservices Part 4: Functional Reactive Programming
Araf Karsh Hamid
 
Reactive Streams and the Wide World of Groovy
Reactive Streams and the Wide World of Groovy
Steve Pember
 
The Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on Android
Fernando Cejas
 
RxJava - introduction & design
RxJava - introduction & design
allegro.tech
 
Reactive Programming in Java by Mario Fusco - Codemotion Rome 2015
Reactive Programming in Java by Mario Fusco - Codemotion Rome 2015
Codemotion
 
Reactive Programming for a demanding world: building event-driven and respons...
Reactive Programming for a demanding world: building event-driven and respons...
Mario Fusco
 
RxJava 2 Reactive extensions for the JVM
RxJava 2 Reactive extensions for the JVM
Netesh Kumar
 
Reactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-Java
Kasun Indrasiri
 
Streams, Streams Everywhere! An Introduction to Rx
Streams, Streams Everywhere! An Introduction to Rx
Andrzej Sitek
 
An Introduction to RxJava
An Introduction to RxJava
Sanjay Acharya
 
Introduction to RxJava on Android
Introduction to RxJava on Android
Chris Arriola
 
Intro to Functional Programming with RxJava
Intro to Functional Programming with RxJava
Mike Nakhimovich
 
Strange Async Code - ReaxtiveX
Strange Async Code - ReaxtiveX
Matthew Will
 
Reactive Functional Programming with Java 8 on Android N
Reactive Functional Programming with Java 8 on Android N
Shipeng Xu
 
Mario Fusco - Reactive programming in Java - Codemotion Milan 2017
Mario Fusco - Reactive programming in Java - Codemotion Milan 2017
Codemotion
 
Reactive Programming with RxJava
Reactive Programming with RxJava
Grand Parade Poland
 
Microservices Part 4: Functional Reactive Programming
Microservices Part 4: Functional Reactive Programming
Araf Karsh Hamid
 
Reactive Streams and the Wide World of Groovy
Reactive Streams and the Wide World of Groovy
Steve Pember
 
The Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on Android
Fernando Cejas
 
Ad

More from NexThoughts Technologies (20)

Alexa skill
Alexa skill
NexThoughts Technologies
 
GraalVM
GraalVM
NexThoughts Technologies
 
Docker & kubernetes
Docker & kubernetes
NexThoughts Technologies
 
Apache commons
Apache commons
NexThoughts Technologies
 
HazelCast
HazelCast
NexThoughts Technologies
 
MySQL Pro
MySQL Pro
NexThoughts Technologies
 
Microservice Architecture using Spring Boot with React & Redux
Microservice Architecture using Spring Boot with React & Redux
NexThoughts Technologies
 
Swagger
Swagger
NexThoughts Technologies
 
Solid Principles
Solid Principles
NexThoughts Technologies
 
Arango DB
Arango DB
NexThoughts Technologies
 
Jython
Jython
NexThoughts Technologies
 
Introduction to TypeScript
Introduction to TypeScript
NexThoughts Technologies
 
Smart Contract samples
Smart Contract samples
NexThoughts Technologies
 
My Doc of geth
My Doc of geth
NexThoughts Technologies
 
Geth important commands
Geth important commands
NexThoughts Technologies
 
Ethereum genesis
Ethereum genesis
NexThoughts Technologies
 
Ethereum
Ethereum
NexThoughts Technologies
 
Springboot Microservices
Springboot Microservices
NexThoughts Technologies
 
An Introduction to Redux
An Introduction to Redux
NexThoughts Technologies
 
Google authentication
Google authentication
NexThoughts Technologies
 

Recently uploaded (20)

Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...
Matsushita Laboratory
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...
Matsushita Laboratory
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 

Reactive java - Reactive Programming + RxJava

  • 2. Hello! I AM Hiten Pratap Singh I am here because I love to dig in to experience new things. You can find me at: ◦ [email protected] ◦ http://github.com/hitenpratap/ ◦ http://hprog99.wordpress.com/
  • 3. I have divided this presentation into two parts Reactive Programming RxJava
  • 4. Reactive Programming What’s Reactive Programming? What’s the big deal about it?
  • 6. “ In computing, reactive programming is a programming paradigm oriented around data flows and the propagation of change.
  • 7. Reactive Programming Reactive programming is programming with asynchronous data streams. ◦ Event buses or your typical click events are really an asynchronous event stream ◦ Streams are cheap and ubiquitous, anything can be a stream: variables, user inputs, properties etc. ◦ For example, imagine your Twitter feed would be a data stream in the same fashion that click events are.
  • 8. Reactive Programming On top of that, you are given an amazing toolbox of functions to combine, create and filter any of those streams. ◦ A stream can be used as an input to another one. ◦ You can merge two streams. ◦ You can filter a stream to get another one that has only those events you are interested in. ◦ You can map data values from one stream to another new one.
  • 9. A stream is a sequence of ongoing events ordered in time. It can emit three different things: a value (of some type), an error, or a "completed" signal. Consider that the "completed" takes place, for instance, when the current window or view containing that button is closed. We capture these emitted events only asynchronously, by defining a function that will execute when a value is emitted, another function when an error is emitted, and another function when 'completed' is emitted. The "listening" to the stream is called subscribing. The functions we are defining are observers. The stream is the subject (or "observable") being observed. This is precisely the Observer Design Pattern.
  • 10. Reactive Programming Why should I consider adopting RP? ◦ RP raises the level of abstraction of your code. ◦ Code in RP will likely be more concise. ◦ Apps nowadays have an abundancy of real-time events of every kind that enable a highly interactive experience to the user. ◦ We need tools for properly dealing with that, and Reactive Programming is an answer.
  • 11. Reactive Programming Key Takeaways ◦ RP is a specification for dealing with asynchronous streams of data. ◦ Reactive provides tools for transforming and combining streams and for managing flow-control ◦ Resembles Java Streams API but the resemblance is purely superficial
  • 12. Thinking in RP with Example
  • 14. ◦ A library for composing async and event based program by using observable sequences. ◦ Created by Microsoft initially for .Net platform by Erik Meijer ◦ Extends observer pattern- * Supports sequence of data and/or events * Add operators that allow you to compose sequences together declaratively ▫ Abstracts away concerns around * Threading and Thread Safety * Concurrent data structures and non blocking I/O ▫ RxJava is a port of Reactive Extensions created by Netflix. What’s ReactiveX?
  • 15. RxJava A libraryto use mightypowers of Reactive Programmingin Java!
  • 16. Maven Getting Started Gradle You can also include its jar file without having to use any of build system as well.
  • 19. Observable In ReactiveX an observer subscribes to an Observable. Then that observer reacts to whatever item or sequence of items the Observable emits.This patternfacilitatesconcurrent operations because it does not need to block while waiting for the Observable to emit objects,but insteadit creates a sentryin the form of an observer that stands ready to react appropriatelyat whatever future time the Observable does so. ◦ Emits zero or more values ◦ Life Cycles * Notifies Observer using onNext(T) * Completes with either onError() or onCompleted()
  • 23. Operators By Categories(Contd.) Conditional and Boolean Operators Mathematical and Aggregate Operators Backpressure Operators ReactiveX provides great flexibility over operators by letting us chaining them together or even creating new ones.
  • 35. Schedulers If you want to introduce multithreadingintoyour cascade of Observable operators, you can do so by instructingthose operators (or particular Observables) to operate on particular Schedulers.
  • 40. Single ◦ RxJava (and its derivatives like RxGroovy & RxScala) has developed an Observable variant called “Single.” ◦ A Single is something like an Observable, but instead of emitting a series of values — anywhere from none at all to an infinite number — it always either emits one value or an error notification. ◦ For this reason, instead of subscribing to a Single with the three methods you use to respond to notifications from an Observable (onNext, onError, and onCompleted), you only use two methods to subscribe:
  • 42. Subject A Subject is a sort of bridge or proxy that is available in some implementations of ReactiveX that acts both as an observer and as an Observable. Because it is an observer, it can subscribe to one or more Observables, and because it is an Observable, it can pass through the items it observes by reemitting them, and it can also emit new items. Because a Subject subscribes to an Observable, it will trigger that Observable to begin emitting items (if that Observable is “cold” — that is, if it waits for a subscription before it begins to emit items). This can have the effect of making the resulting Subject a “hot” Observable variant of the original “cold” Observable.
  • 43. Varieties of Subject ◦ AsyncSubject ◦ BehaviorSubject ◦ PublishSubject ◦ ReplaySubject
  • 49. Backpressure In RxJava it is not difficult to get into a situation in which an Observable is emitting items more rapidly than an operator or subscriber can consume them. This presents the problem of what to do with such a growing backlog of unconsumed items. For example, imagine using the zip operator to zip together two infinite Observables, one of which emits items twice as frequently as the other. A naive implementation of the zip operator would have to maintain an ever-expanding buffer of items emitted by the faster Observable to eventually combine with items emitted by the slower one. This could cause RxJava to seize an unwieldy amount of system resources.
  • 51. Hot Observable A hot Observable begins generating items to emit immediately when it is created. Subscribers typically begin observing the sequence of items emitted by a hot Observable from somewhere in the middle of the sequence, beginning with the first item emitted by the Observable subsequent to the establishment of the subscription. Such an Observable emits items at its own pace, and it is up to its observers to keep up. Examples of items emitted by a hot Observable might include mouse & keyboard events, system events, or stock prices.
  • 52. Cold Observable A cold Observable emits a particular sequence of items, but can begin emitting this sequence when its Observer finds it to be convenient, and at whatever rate the Observer desires, without disrupting the integrity of the sequence. For example if you convert a static Iterable into an Observable, that Observable will emit the same sequence of items no matter when it is later subscribed to or how frequently those items are observed. Examples of items emitted by a cold Observable might include the results of a database query, file retrieval, or web request.
  • 54. Pros ◦ Makes Async code easy to develop ◦ Lots of resources to get started with ◦ Strong use of functional type programming ◦ Netflix used RxNetty and Reactive Programmingto considerable performance advantage over Tomcat ◦ Web Service clients like Jerseyand Retrofit support RxJava.
  • 55. Cons ◦ Learning curve and mind set change to use functional reactive programming ◦ Appears like more code and more complex code initially– Requires time for it to grow on you. ◦ If on Java 7 anonymous class would make this a non-starter.
  • 56. Thanks! ANY QUESTIONS? You can find me at: [email protected] http://github.com/hitenpratap/ http://hprog99.wordpress.com/