SlideShare a Scribd company logo
1 of 35
Download to read offline
Intro to RxJavaWhat is Reactive Programming?
Prepared By: awais.mazhar@mobilelive.ca
Hello!
I am Awais Mazhar
Today I’ll give some intro about
reactive programming and RxJava.
We will discuss about
+ Reactive Programming
+ Why Rx ?
+ When Rx ?
+ What is Observer pattern?
+ Brief intro to RxJava
+ Some code examples
Reactive Programming
A programming paradigm oriented around data flows and the propagation
of change. This means that it should be possible to express static or
dynamic data flows with ease in the programming languages used, and that
the underlying execution model will automatically propagate changes
through the data flow.
Reactive programming is programming with asynchronous data
streams.
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.
Why Rx ?
Rx is a powerful tool that enables the solution of problems in an elegant
declarative style, familiar to functional programmers. Rx has several
benefits:
+ Unitive
Queries in Rx are done in the same style as other libraries inspired by functional
programming, such as Java streams. In Rx, one can use functional style
transformations on event streams.
+ Declarative
Functional transformations are read in a declarative way.
Why Rx ?
+ Extensible
RxJava can be extended with custom operators. Although Java does not allow for
this to happen in an elegant way, RxJava offers all the extensibility one can find Rx
implementations in other languages.
+ Composable
Rx operators can be combined to produce more complicated operations.
+ Transformative
Rx operators can transform one type of data to another, reducing, mapping or
expanding streams as needed.
When Rx?
Rx is fit for composing and consuming sequences of events.
Should be used When:
+ UI events like mouse move, button click
+ Domain events like property changed, collection updated, "Order
Filled", "Registration accepted" etc.
+ Infrastructure events like from file watcher, system.
+ Integration events like a broadcast from a message bus or a push
event from WebSockets API or other low latency middleware.
+ Integration with a CEP engine like StreamInsight or StreamBase.
Observer pattern
The observer pattern is a software design pattern in which an object, called
the subject, maintains a list of its dependents, called observers, and notifies
them automatically of any state changes, usually by calling one of their
methods.
It is mainly used to implement distributed event handling systems, in "event
driven" software. Most modern languages such as Java and C# have built in
"event" constructs which implement the observer pattern components, for
easy programming and short code.
Observer pattern
The observer pattern can cause memory leaks, known as the lapsed
listener problem, because in basic implementation it requires both explicit
registration and explicit deregistration, as in the dispose pattern, because
the subject holds strong references to the observers, keeping them alive.
This can be prevented by the subject holding weak references to the
observers.
A UML diagram of the observer pattern is on next Slide.
Observer pattern
- Java Example
Here is an example written
in Java that takes keyboard
input and treats each input
line as an event.
Observer pattern
- Java Example
When a string is supplied
from System.in, method is
called, in order to notify all
observers of the event's
occurrence.
Deep into RxJava
Some Key Types, default methods, and many more
RxJava
Rx is based around two fundamental types, while several others
expand the functionality around the core types. Those two core types
are the Observable and the Observer, which will be introduced in
coming slides. We will also introduce Subjects, which ease the learning
curve.
RxJava - Observable
This class contains a lot of the implementation of Rx, including all of the
core operators. Subscribe method is very important.
public final Subscription subscribe(Subscriber<? super T> subscriber)
This method is used to receive the values emitted by the observable.
Values are pushed to the subscriber, which is then responsible for the
behaviour intended by the consumer.
RxJava - Observable
An observable pushes 3 kinds of events.
+ Values
+ Completion, which indicates that no more values will be pushed.
+ Errors, if something caused the sequence to fail. These events also
imply termination.
RxJava - Observer
This interface has three methods, behaviour that is executed every
time the observable pushes a value. The observer will have its onNext
called zero or more times, optionally followed by an onCompleted or an
onError. No calls happen after a call to onError or onCompleted.
interface Observer<T> {
void onCompleted();
void onError(java.lang.Throwable e);
void onNext(T t);
}
RxJava - Observer
- Java Implementation
Here is an Observable which
is only pushing data one
time to it's all subscribers
(as OnNext() is called once).
And Subscriber is printing
whatever received.
RxJava - Subject
Subjects are an extension of the Observable that also implements the
Observer interface.They can have events pushed to them (like observers),
which they then push further to their own subscribers (like
observables).There are a few different implementations of Subject.
We will now examine the most important ones and their differences.
RxJava - Subject
- PublishSubject
When a value is pushed into
a PublishSubject, the
subject pushes it to every
subscriber that is
subscribed to it at that
moment.
RxJava - Subject
- ReplaySubject
When a new subscription is
made, the event sequence is
replayed from the start for the
new subscriber. After catching
up, every subscriber receives
new events as they come.
BehaviorSubject is same as this
but it only remember the last
value.
RxJava - Subject
- AsyncSubject
It also caches the last value.
The difference now is that it
doesn't emit anything until
the sequence completes. Its
use is to emit a single value
and immediately complete.
RxJava - Lifetime management
We have control over when to begin and stop accepting values.
Subscriptions may be linked to allocated resources that we will want to
release at the end of a sequence. Rx provides control over your
subscriptions to enable you to do that.
- Subscribing
There are several overloads to Observable.subscribe, which are shorthands for
the same thing.
Subscription subscribe() //Subscription subscribe(Subscriber<? super T> subscriber)
Subscription subscribe(Action1<? super T> onNext)
Subscription subscribe(Action1<? super T> onNext, Action1<java.lang.Throwable>
onError)
RxJava - Lifetime management
You can also stop receiving values before a sequence terminates. Every
subscribe overload returns an instance of Subscription, which is an
interface with 2 methods:
boolean isUnsubscribed()
void unsubscribe()
Calling unsubscribe will stop events from being pushed to your
observer.
RxJava -Life Mgt
- Unsubscribing
Here in this example, after
receiving two values we
unsubscribed our subscriber
and you can see output as
well.
RxJava - Observable Factory Methods
In previous examples we used Subjects and manually pushed values
into them to create a sequence. We used that sequence to
demonstrate some key concepts and the first and most important Rx
method, subscribe.
RxJava -Observable
- Observable.just
The just method creates an
Observable that will emit a
predefined sequence of
values, supplied on creation,
and then terminate.
RxJava -Observable
- Observable.empty
This observable will emit a
single onCompleted and
nothing else.
RxJava -Observable
- Observable.create
It is basically a function that
takes a Subscriber<T> for
type T. Inside it we can
manually determine the
events that are pushed to
the subscriber.
RxJava -Observable
- Observable.range
It emits the specified range
of integers.
- Observable.Interval
It will create an infinite
sequence of ticks,
separated by the specified
time duration.
RxJava -Observer
- Filter
filter takes a predicate
function that makes a
boolean decision for each
value emitted. If the decision
is false, the item is omitted
from the filtered sequence.
RxJava -Observer
- distinct
It filters out any element
that has already appeared
in the sequence.
That’s All !
You may have a cup of coffee now
Thanks!
Any questions?
You can find me at:
+ syedawaismazhar@gmail.com
References
+ https://en.wikipedia.org/wiki/Reactive_programming
+ https://medium.com/@kevalpatel2106/what-is-reactive-programming-da37c1611382
+ https://gist.github.com/staltz/868e7e9bc2a7b8c1f754
+ https://en.wikipedia.org/wiki/Observer_pattern
+ https://github.com/Froussios/Intro-To-RxJava/blob/master/Part%202%20-%20Sequence%20
Basics/2.%20Reducing%20a%20sequence.md

More Related Content

What's hot

Reactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJavaReactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJavaNexThoughts Technologies
 
Streamlining with rx
Streamlining with rxStreamlining with rx
Streamlining with rxAkhil Dad
 
RxJava from the trenches
RxJava from the trenchesRxJava from the trenches
RxJava from the trenchesPeter Hendriks
 
Extending Spark With Java Agent (handout)
Extending Spark With Java Agent (handout)Extending Spark With Java Agent (handout)
Extending Spark With Java Agent (handout)Jaroslav Bachorik
 
Java in mule part 3
Java in mule part 3Java in mule part 3
Java in mule part 3vasanthii9
 
RxJava pour Android : présentation lors du GDG Android Montréal
RxJava pour Android : présentation lors du GDG Android MontréalRxJava pour Android : présentation lors du GDG Android Montréal
RxJava pour Android : présentation lors du GDG Android MontréalSidereo
 
Dynamic autoselection and autotuning of machine learning models forcloud netw...
Dynamic autoselection and autotuning of machine learning models forcloud netw...Dynamic autoselection and autotuning of machine learning models forcloud netw...
Dynamic autoselection and autotuning of machine learning models forcloud netw...Venkat Projects
 
Lecture: Advanced Reflection. MetaLinks
Lecture: Advanced Reflection. MetaLinksLecture: Advanced Reflection. MetaLinks
Lecture: Advanced Reflection. MetaLinksMarcus Denker
 
Life of a Label (PromCon2016, Berlin)
Life of a Label (PromCon2016, Berlin)Life of a Label (PromCon2016, Berlin)
Life of a Label (PromCon2016, Berlin)Brian Brazil
 
Architectural patterns part 4
Architectural patterns part 4Architectural patterns part 4
Architectural patterns part 4assinha
 
New Microsoft Word Document.doc
New Microsoft Word Document.docNew Microsoft Word Document.doc
New Microsoft Word Document.docbutest
 
Comparing different concurrency models on the JVM
Comparing different concurrency models on the JVMComparing different concurrency models on the JVM
Comparing different concurrency models on the JVMMario Fusco
 

What's hot (20)

Reactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJavaReactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJava
 
Streamlining with rx
Streamlining with rxStreamlining with rx
Streamlining with rx
 
RxJava from the trenches
RxJava from the trenchesRxJava from the trenches
RxJava from the trenches
 
Struts2
Struts2Struts2
Struts2
 
Struts2
Struts2Struts2
Struts2
 
Struts 2
Struts 2Struts 2
Struts 2
 
Mule java part-3
Mule java part-3Mule java part-3
Mule java part-3
 
Extending Spark With Java Agent (handout)
Extending Spark With Java Agent (handout)Extending Spark With Java Agent (handout)
Extending Spark With Java Agent (handout)
 
Mule java part-3
Mule java part-3Mule java part-3
Mule java part-3
 
Java in mule part 3
Java in mule part 3Java in mule part 3
Java in mule part 3
 
Struts2 notes
Struts2 notesStruts2 notes
Struts2 notes
 
Java for newcomers
Java for newcomersJava for newcomers
Java for newcomers
 
RxJava pour Android : présentation lors du GDG Android Montréal
RxJava pour Android : présentation lors du GDG Android MontréalRxJava pour Android : présentation lors du GDG Android Montréal
RxJava pour Android : présentation lors du GDG Android Montréal
 
Qtp stuff
Qtp stuffQtp stuff
Qtp stuff
 
Dynamic autoselection and autotuning of machine learning models forcloud netw...
Dynamic autoselection and autotuning of machine learning models forcloud netw...Dynamic autoselection and autotuning of machine learning models forcloud netw...
Dynamic autoselection and autotuning of machine learning models forcloud netw...
 
Lecture: Advanced Reflection. MetaLinks
Lecture: Advanced Reflection. MetaLinksLecture: Advanced Reflection. MetaLinks
Lecture: Advanced Reflection. MetaLinks
 
Life of a Label (PromCon2016, Berlin)
Life of a Label (PromCon2016, Berlin)Life of a Label (PromCon2016, Berlin)
Life of a Label (PromCon2016, Berlin)
 
Architectural patterns part 4
Architectural patterns part 4Architectural patterns part 4
Architectural patterns part 4
 
New Microsoft Word Document.doc
New Microsoft Word Document.docNew Microsoft Word Document.doc
New Microsoft Word Document.doc
 
Comparing different concurrency models on the JVM
Comparing different concurrency models on the JVMComparing different concurrency models on the JVM
Comparing different concurrency models on the JVM
 

Similar to Intro to Rx Java

RxJava 2 Reactive extensions for the JVM
RxJava 2  Reactive extensions for the JVMRxJava 2  Reactive extensions for the JVM
RxJava 2 Reactive extensions for the JVMNetesh Kumar
 
Reactive programming with rx java
Reactive programming with rx javaReactive programming with rx java
Reactive programming with rx javaCongTrung Vnit
 
Reactive programming
Reactive programmingReactive programming
Reactive programmingsaykopatt
 
IPT Reactive Java IoT Demo - BGOUG 2018
IPT Reactive Java IoT Demo - BGOUG 2018IPT Reactive Java IoT Demo - BGOUG 2018
IPT Reactive Java IoT Demo - BGOUG 2018Trayan Iliev
 
Reactive Programming in Java and Spring Framework 5
Reactive Programming in Java and Spring Framework 5Reactive Programming in Java and Spring Framework 5
Reactive Programming in Java and Spring Framework 5Richard Langlois P. Eng.
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJSAbul Hasan
 
My Gentle Introduction to RxJS
My Gentle Introduction to RxJSMy Gentle Introduction to RxJS
My Gentle Introduction to RxJSMattia Occhiuto
 
Building Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaRick Warren
 
BASICS OF VERT.X - A toolkit for building asynchronous and reactive app
BASICS OF VERT.X - A toolkit for building asynchronous and reactive appBASICS OF VERT.X - A toolkit for building asynchronous and reactive app
BASICS OF VERT.X - A toolkit for building asynchronous and reactive appHanaStevanovic
 
RxJava2 Slides
RxJava2 SlidesRxJava2 Slides
RxJava2 SlidesYarikS
 
Reactive programming and RxJS
Reactive programming and RxJSReactive programming and RxJS
Reactive programming and RxJSRavi Mone
 
Akka Microservices Architecture And Design
Akka Microservices Architecture And DesignAkka Microservices Architecture And Design
Akka Microservices Architecture And DesignYaroslav Tkachenko
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJSBrainhub
 
## Introducing a reactive Scala-Akka based system in a Java centric company
## Introducing a reactive Scala-Akka based system in a Java centric company## Introducing a reactive Scala-Akka based system in a Java centric company
## Introducing a reactive Scala-Akka based system in a Java centric companyMilan Aleksić
 
Prezo tooracleteam (2)
Prezo tooracleteam (2)Prezo tooracleteam (2)
Prezo tooracleteam (2)Sharma Podila
 
How to Think in RxJava Before Reacting
How to Think in RxJava Before ReactingHow to Think in RxJava Before Reacting
How to Think in RxJava Before ReactingIndicThreads
 

Similar to Intro to Rx Java (20)

RxJava 2 Reactive extensions for the JVM
RxJava 2  Reactive extensions for the JVMRxJava 2  Reactive extensions for the JVM
RxJava 2 Reactive extensions for the JVM
 
DZone_RC_RxJS
DZone_RC_RxJSDZone_RC_RxJS
DZone_RC_RxJS
 
Reactive programming with rx java
Reactive programming with rx javaReactive programming with rx java
Reactive programming with rx java
 
Reactive programming
Reactive programmingReactive programming
Reactive programming
 
IPT Reactive Java IoT Demo - BGOUG 2018
IPT Reactive Java IoT Demo - BGOUG 2018IPT Reactive Java IoT Demo - BGOUG 2018
IPT Reactive Java IoT Demo - BGOUG 2018
 
Rx java workshop
Rx java workshop Rx java workshop
Rx java workshop
 
Reactive Programming in Java and Spring Framework 5
Reactive Programming in Java and Spring Framework 5Reactive Programming in Java and Spring Framework 5
Reactive Programming in Java and Spring Framework 5
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJS
 
My Gentle Introduction to RxJS
My Gentle Introduction to RxJSMy Gentle Introduction to RxJS
My Gentle Introduction to RxJS
 
Reactive mesh
Reactive meshReactive mesh
Reactive mesh
 
Building Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJava
 
Reactors.io
Reactors.ioReactors.io
Reactors.io
 
BASICS OF VERT.X - A toolkit for building asynchronous and reactive app
BASICS OF VERT.X - A toolkit for building asynchronous and reactive appBASICS OF VERT.X - A toolkit for building asynchronous and reactive app
BASICS OF VERT.X - A toolkit for building asynchronous and reactive app
 
RxJava2 Slides
RxJava2 SlidesRxJava2 Slides
RxJava2 Slides
 
Reactive programming and RxJS
Reactive programming and RxJSReactive programming and RxJS
Reactive programming and RxJS
 
Akka Microservices Architecture And Design
Akka Microservices Architecture And DesignAkka Microservices Architecture And Design
Akka Microservices Architecture And Design
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJS
 
## Introducing a reactive Scala-Akka based system in a Java centric company
## Introducing a reactive Scala-Akka based system in a Java centric company## Introducing a reactive Scala-Akka based system in a Java centric company
## Introducing a reactive Scala-Akka based system in a Java centric company
 
Prezo tooracleteam (2)
Prezo tooracleteam (2)Prezo tooracleteam (2)
Prezo tooracleteam (2)
 
How to Think in RxJava Before Reacting
How to Think in RxJava Before ReactingHow to Think in RxJava Before Reacting
How to Think in RxJava Before Reacting
 

More from Syed Awais Mazhar Bukhari

More from Syed Awais Mazhar Bukhari (6)

Android App Bundles - Overview
Android App Bundles - OverviewAndroid App Bundles - Overview
Android App Bundles - Overview
 
CDD - Atomic Design Methodology
CDD - Atomic Design MethodologyCDD - Atomic Design Methodology
CDD - Atomic Design Methodology
 
Introduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTXIntroduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTX
 
Insight into java 1.8, OOP VS FP
Insight into java 1.8, OOP VS FPInsight into java 1.8, OOP VS FP
Insight into java 1.8, OOP VS FP
 
Methods of data recovery
Methods of data recoveryMethods of data recovery
Methods of data recovery
 
Introduction to triggers
Introduction to triggersIntroduction to triggers
Introduction to triggers
 

Recently uploaded

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 

Recently uploaded (20)

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 

Intro to Rx Java

  • 1. Intro to RxJavaWhat is Reactive Programming? Prepared By: awais.mazhar@mobilelive.ca
  • 2. Hello! I am Awais Mazhar Today I’ll give some intro about reactive programming and RxJava.
  • 3. We will discuss about + Reactive Programming + Why Rx ? + When Rx ? + What is Observer pattern? + Brief intro to RxJava + Some code examples
  • 4. Reactive Programming A programming paradigm oriented around data flows and the propagation of change. This means that it should be possible to express static or dynamic data flows with ease in the programming languages used, and that the underlying execution model will automatically propagate changes through the data flow. Reactive programming is programming with asynchronous data streams. 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.
  • 5. Why Rx ? Rx is a powerful tool that enables the solution of problems in an elegant declarative style, familiar to functional programmers. Rx has several benefits: + Unitive Queries in Rx are done in the same style as other libraries inspired by functional programming, such as Java streams. In Rx, one can use functional style transformations on event streams. + Declarative Functional transformations are read in a declarative way.
  • 6. Why Rx ? + Extensible RxJava can be extended with custom operators. Although Java does not allow for this to happen in an elegant way, RxJava offers all the extensibility one can find Rx implementations in other languages. + Composable Rx operators can be combined to produce more complicated operations. + Transformative Rx operators can transform one type of data to another, reducing, mapping or expanding streams as needed.
  • 7. When Rx? Rx is fit for composing and consuming sequences of events. Should be used When: + UI events like mouse move, button click + Domain events like property changed, collection updated, "Order Filled", "Registration accepted" etc. + Infrastructure events like from file watcher, system. + Integration events like a broadcast from a message bus or a push event from WebSockets API or other low latency middleware. + Integration with a CEP engine like StreamInsight or StreamBase.
  • 8. Observer pattern The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems, in "event driven" software. Most modern languages such as Java and C# have built in "event" constructs which implement the observer pattern components, for easy programming and short code.
  • 9. Observer pattern The observer pattern can cause memory leaks, known as the lapsed listener problem, because in basic implementation it requires both explicit registration and explicit deregistration, as in the dispose pattern, because the subject holds strong references to the observers, keeping them alive. This can be prevented by the subject holding weak references to the observers. A UML diagram of the observer pattern is on next Slide.
  • 10.
  • 11. Observer pattern - Java Example Here is an example written in Java that takes keyboard input and treats each input line as an event.
  • 12. Observer pattern - Java Example When a string is supplied from System.in, method is called, in order to notify all observers of the event's occurrence.
  • 13. Deep into RxJava Some Key Types, default methods, and many more
  • 14. RxJava Rx is based around two fundamental types, while several others expand the functionality around the core types. Those two core types are the Observable and the Observer, which will be introduced in coming slides. We will also introduce Subjects, which ease the learning curve.
  • 15. RxJava - Observable This class contains a lot of the implementation of Rx, including all of the core operators. Subscribe method is very important. public final Subscription subscribe(Subscriber<? super T> subscriber) This method is used to receive the values emitted by the observable. Values are pushed to the subscriber, which is then responsible for the behaviour intended by the consumer.
  • 16. RxJava - Observable An observable pushes 3 kinds of events. + Values + Completion, which indicates that no more values will be pushed. + Errors, if something caused the sequence to fail. These events also imply termination.
  • 17. RxJava - Observer This interface has three methods, behaviour that is executed every time the observable pushes a value. The observer will have its onNext called zero or more times, optionally followed by an onCompleted or an onError. No calls happen after a call to onError or onCompleted. interface Observer<T> { void onCompleted(); void onError(java.lang.Throwable e); void onNext(T t); }
  • 18. RxJava - Observer - Java Implementation Here is an Observable which is only pushing data one time to it's all subscribers (as OnNext() is called once). And Subscriber is printing whatever received.
  • 19. RxJava - Subject Subjects are an extension of the Observable that also implements the Observer interface.They can have events pushed to them (like observers), which they then push further to their own subscribers (like observables).There are a few different implementations of Subject. We will now examine the most important ones and their differences.
  • 20. RxJava - Subject - PublishSubject When a value is pushed into a PublishSubject, the subject pushes it to every subscriber that is subscribed to it at that moment.
  • 21. RxJava - Subject - ReplaySubject When a new subscription is made, the event sequence is replayed from the start for the new subscriber. After catching up, every subscriber receives new events as they come. BehaviorSubject is same as this but it only remember the last value.
  • 22. RxJava - Subject - AsyncSubject It also caches the last value. The difference now is that it doesn't emit anything until the sequence completes. Its use is to emit a single value and immediately complete.
  • 23. RxJava - Lifetime management We have control over when to begin and stop accepting values. Subscriptions may be linked to allocated resources that we will want to release at the end of a sequence. Rx provides control over your subscriptions to enable you to do that. - Subscribing There are several overloads to Observable.subscribe, which are shorthands for the same thing. Subscription subscribe() //Subscription subscribe(Subscriber<? super T> subscriber) Subscription subscribe(Action1<? super T> onNext) Subscription subscribe(Action1<? super T> onNext, Action1<java.lang.Throwable> onError)
  • 24. RxJava - Lifetime management You can also stop receiving values before a sequence terminates. Every subscribe overload returns an instance of Subscription, which is an interface with 2 methods: boolean isUnsubscribed() void unsubscribe() Calling unsubscribe will stop events from being pushed to your observer.
  • 25. RxJava -Life Mgt - Unsubscribing Here in this example, after receiving two values we unsubscribed our subscriber and you can see output as well.
  • 26. RxJava - Observable Factory Methods In previous examples we used Subjects and manually pushed values into them to create a sequence. We used that sequence to demonstrate some key concepts and the first and most important Rx method, subscribe.
  • 27. RxJava -Observable - Observable.just The just method creates an Observable that will emit a predefined sequence of values, supplied on creation, and then terminate.
  • 28. RxJava -Observable - Observable.empty This observable will emit a single onCompleted and nothing else.
  • 29. RxJava -Observable - Observable.create It is basically a function that takes a Subscriber<T> for type T. Inside it we can manually determine the events that are pushed to the subscriber.
  • 30. RxJava -Observable - Observable.range It emits the specified range of integers. - Observable.Interval It will create an infinite sequence of ticks, separated by the specified time duration.
  • 31. RxJava -Observer - Filter filter takes a predicate function that makes a boolean decision for each value emitted. If the decision is false, the item is omitted from the filtered sequence.
  • 32. RxJava -Observer - distinct It filters out any element that has already appeared in the sequence.
  • 33. That’s All ! You may have a cup of coffee now
  • 34. Thanks! Any questions? You can find me at: + syedawaismazhar@gmail.com
  • 35. References + https://en.wikipedia.org/wiki/Reactive_programming + https://medium.com/@kevalpatel2106/what-is-reactive-programming-da37c1611382 + https://gist.github.com/staltz/868e7e9bc2a7b8c1f754 + https://en.wikipedia.org/wiki/Observer_pattern + https://github.com/Froussios/Intro-To-RxJava/blob/master/Part%202%20-%20Sequence%20 Basics/2.%20Reducing%20a%20sequence.md