SlideShare a Scribd company logo
RxJava – Reactive Extensions for the JVM
An introduction to RxJava
By
Sanjay Acharya and Joel Weight
Twitter: @sanjayvacharya and @digitaljoel
1
RxJava 9/30/2015
Some reflection…
 Only recently…
 Large applications with 10s of servers
 Seconds of response times
 Hours of offline maintenance
 Gigabytes of Data
 Today’s Applications
 Deployed on anything from Mobile to Cloud Clusters with thousands of
multi core processors
 Millisecond response times
 100% uptime
 Petabytes of Data
 Traditional Architectures fail to meet today’s demands
9/30/2015RxJava
2
So How do we meet today’s
demands ?
Reactive Systems
9/30/2015RxJava
3
Reactive Definition
Reactive is being readily
responsive to a stimulus
9/30/2015RxJava
4
http://www.reactivemanifesto.org
9/30/2015RxJava
5
Event Stream(s)
9/30/2015RxJava
6
Mouse Clicks
Stocks Splunk Events
Tweets
Functional Reactive Programming
Values are pushed when ready in a non
blocking manner
Facilitates parallelism
Application of ‘functions’ on data stream
to transform data – Examples - map,
filter, zip
9/30/2015RxJava
7
Reactive Model is Push rather than Pull
ReactiveX
 A library for composing asynchronous and event based
programs by using observable sequences
 Created by Microsoft initially for the .NET platform
By Erik Meijer
 Extends Observer Pattern to
 Support sequence of data and/or events
 Adds 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
9/30/2015RxJava
8
GOF Observer Pattern
9/30/2015RxJava
9
Reactive Components
9/30/2015RxJava
10
Observable Observer
Subject
Emits Events Observes
Emitted Events
Observes and Emits
Events
rx.Observable
9/30/2015RxJava
11
public class Observable<T> { … }
 Emits zero or more values
 Life Cycle
 Notifies Observer
 onNext element
 Completes with
 onError
 Or onCompletion
Observable Observer
onNext(T)
onComplete()
onError(Throwable)
Observable Creation
9/30/2015
12
Observable<Integer> o = Observable
.create(new Observable.OnSubscribe<Integer>(){ // Action
@Override
public void call(Subscriber<Integer> subscriber){
try {
for (int i = 0; i < 10
&& ! subscriber.isUnsubscribed(); i++) {
subscriber.onNext(i);
}
if (!subscriber.isUnsubscribed()) {
subscriber.onCompleted();
}
}
catch (RuntimeException e) {
if (!subscriber.isUnsubscribed()) {
subscriber.onError(e);
}
}
}
});
RxJava
Using JDK 7
Downstream Slide
examples will use
JDK 8 Lambdas
Subscribing (Observer)
9/30/2015
13
Observable<Integer> o = Observable.create(…);
Subscription s = o.subscribe(new Observer<Integer>() {
@Override
public void onNext(Integer i) {
System.out.print(i);
}
@Override
public void onCompleted() {
System.out.println(“nAll Done!”);
}
@Override
public void onError(Throwable t) {
t.printStackTrace();
}
});
Prints –
0 to 9!
Observable from
previous slide
RxJava
0123456789
All Done!
Prints –
All Done!
9/30/2015RxJava
14
Operator Description Example
Observable.just(T) Simple values wrapped Observable.just(“HelloWorld”);
Observable.empty() Empty Sequence that
completes right away
Observable.empty();
Observable.from(Iterable<T>) Sequence from an
Iterable
Observable.from(Lists.newArrayList(“A”, “B”));
Observable.from(T []) Sequence from Array Observable.from(new Integer[] {0, 1, 2, 3, 4});
Observable.defer(ObservableFactory) Defer emitting of items
for each subscriber
Example shown later.
Observable.interval(long, TimeUnit) Emit a sequence of
numbers separated by
an interval
Observable.interval(100, TimeUnit.MILLISECONDS);
Observable.range(start, count) Emits a sequence of
integers in a range
Observable.range(100, 20);
Observable.create(OnSubscribe<T>) Most flexible and
powerful
Observable.create(new OnSubscribe…());
Observable Vs Iterator
Event Iterable (PULL) Observable (PUSH)
Retrieve Data T next(); onNext(T);
Discover the Error throws Exception onError(Throwable)
Complete !hasNext() onCompleted();
9/30/2015RxJava
15
Observable Operator Categories
9/30/2015RxJava
16
Operator Categories
9/30/2015RxJava
17
Marble Diagram
9/30/2015RxJava
18
Transformational
 Operations that transform items emitted by an
Observable
 Commonly used..
 map
 flatmap
9/30/2015RxJava
19
Map
 Transform the items
emitted by an
Observable by applying
a function to each item
9/30/2015RxJava
20
Observable<Integer> o = Observable.range(0,10);
Observable<Integer> timesTen = o.map(t -> t * 10);
Flatmap
 Transforms items emitted from
an Observable into Observables
and then merges those
emissions into a single
Observable
9/30/2015RxJava
21
Observable<Integer> o = Observable.range(0, 10);
Observable<Integer> numbersAndNegatives
= o.flatmap(t -> Observable.just(t, -t));
Filtering
 Operators that selectively emit items from a source
Observable
 Commonly used
 filter
 distinct
 take
 first
9/30/2015RxJava
22
Filter
 Emit only items from an
Observable that match a
predicate test
9/30/2015RxJava
23
Observable<Integer> o = Observable.range(0,10);
Observable<Integer> evenNos = o.filter(t -> (t % 2) == 0);
Take
 Emit only the first n items
emitted by an Observable.
9/30/2015RxJava
24
Observable<Integer> o = Observable.range(0,10);
Observable<Integer> firstTwo = o.take(2);
Combining Observables
 Operators that work with multiple source Observables to
create a single Observable
 Commonly used
 Merge
 Concat
 Zip
 zipWith
9/30/2015RxJava
25
Merge
 Combine multiple Observables by
merging their emissions
 May interleave items
9/30/2015RxJava
26
Observable<Integer> first = Observable.range(0,10);
Observable<Integer> second = Observable.range(10, 20);
Observable<Integer> merged
= Observable.merge(first, second);
Concat
 Similar to merge but combines
observable without interleaving
9/30/2015RxJava
27
Observable<Integer> first = Observable.range(0,10);
Observable<Integer> second = Observable.range(10, 20);
Observable<Integer> concat
= Observable.concat(first, second);
Zip
 combine the emissions of multiple
Observables together via a
specified function and emit single
items for each combination based
on the results of this function
9/30/2015RxJava
28
Observable<Integer> first = Observable.just(1, 2, 3);
Observable<String> second = Observable.just(“A”,“B”, “C”, “D”);
Observable<String> zipped =
Observable.zip(first, second (x,y) -> String.valueOf(x)+y);
// on subscription emits “1A”, “2B”, “3C”
ZipWith
 Instance-version of zip
9/30/2015RxJava
29
Observable<Integer> first = Observable.just(1, 2, 3);
Observable<String> second = Observable.just(“A”,“B”, “C”, “D”);
Observable<String> zipped =
first.zipWith(second, (x, y) -> String.valueOf(x)+y);
// on subscription emits “1A”, “2B”, “3C”
Decision Tree of Observables
9/30/2015RxJava
30
http://reactivex.io/documentation/operators.html#tree
Schedulers and Observable Utility
Operators
9/30/2015RxJava
31
Schedulers
9/30/2015RxJava
32
io() computational()
newThread() trampoline()
immediate() test()
from(Executor)
Observable Utility Operators
Utility Operators for working
with Observables
Common Utility Operators –
 Observable.observeOn(Scheduler)
 Observable.subscribeOn(Scheduler)
9/30/2015RxJava
33
ObserveOn()
SubscribeOn()
Main Thread Subscribe
9/30/2015
34
Observable<String> hello
= Observable.create(subscriber -> {
System.out.println(“Observable executing on:”
+ Thread.currentThread());
subscriber.onNext(“Hello World!”);
subscriber.onCompleted();
}
});
hello.subscribe(s - > {
System.out.println(“Observing on:”
+ Thread.currentThread() + s);
});
Observable executing on: [main,5,main]
Observing on: [main,5,main] Hello World
RxJava
ObserveOn
9/30/2015
35
Observable<String> hello
= Observable.create(subscriber -> {
System.out.println(“Observable executing on:”
+ Thread.currentThread());
subscriber.onNext(“Hello World!”);
subscriber.onCompleted();
}
});
hello.observeOn(Schedulers.io()).subscribe(s - > {
System.out.println(“Observing on:”
+ Thread.currentThread() + s);
});
Observable executing on: [main,5,main]
Observing on: [RxCachedThreadScheduler-1,5,main] Hello World
RxJava
SubscribeOn
9/30/2015
36
Observable<String> hello
= Observable.create(subscriber -> {
System.out.println(“Observable executing on:”
+ Thread.currentThread());
subscriber.onNext(“Hello World!”);
subscriber.onCompleted();
}
});
hello.subscribeOn(Schedulers.io()).subscribe(s - > {
System.out.println(“Observing on:”
+ Thread.currentThread() + s);
});
Observable executing on:[RxCachedThreadScheduler-1,5,main]
Observing on: [RxCachedThreadScheduler-1,5,main] Hello World
RxJava
SubscribeOn and ObserveOn
9/30/2015
37
Observable<String> hello
= Observable.create(subscriber -> {
System.out.println(“Observable executing on:”
+ Thread.currentThread());
subscriber.onNext(“Hello World!”);
subscriber.onCompleted();
}
});
hello.subscribeOn(Schedulers.computation())
.observeOn(Schedulers.io())
.subscribe(s - > {
System.out.println(“Observing on:”+ Thread.currentThread());
});
Observable executing on: [RxCompuationThreadPool-3,5,main]
Observing on: [RxCachedThreadScheduler-1,5,main] Hello World
RxJava
Reactive Example – Putting it
together
9/30/2015RxJava
38
Product Gateway Service
9/30/2015RxJava
39
Product Gateway Service
9/30/2015RxJava
40
Model
9/30/2015RxJava
41
PriceInventory
• Product display
price is lowest
Option price
• Product inventory
is total of all
Option Inventory
• Inventory and
Price are inferred
for the Product
• Product contains
Options
Synchronously Assembled Product
9/30/2015
42
Product getProduct(Long productId) {
BaseProduct bp = baseProductClient.getBaseProduct(productId); // Blocking WS Call
}
RxJava
Obtain
Base Product.
Synchronously Assembled Product
9/30/2015
43
Product getProduct(Long productId) {
BaseProduct bp = baseProductClient.getBaseProduct(productId); // Blocking WS Call
Double displayPrice = Double.MAX_VALUE;
List<Option> optionList = Lists.newArrayList();
int totalInventory = 0;
for (BaseOption bo : bp.getOptions()) {
}
}
RxJava
For Each
BaseOption
Synchronously Assembled Product
9/30/2015
44
Product getProduct(Long productId) {
BaseProduct bp = baseProductClient.getBaseProduct(productId); // Blocking WS Call
Double displayPrice = Double.MAX_VALUE;
List<Option> optionList = Lists.newArrayList();
int totalInventory = 0;
for (BaseOption bo : bp.getOptions()) {
Double price = priceClient.getPrice(bo.getId()); // Blocking WS Call
Integer inventory = inventoryClient.getInventory(bo.getId()); // Blocking WS Call
optionList.add(new Option().withId(bo.getId())..withPrice(..).withInventory(..));
totalInventory += inventory;
displayPrice = (price < lowestPrice) ? price : lowestPrice;
}
}
RxJava
Obtain Price, Inventory for ever BaseOption
and create Option.
Compute total inventory and lowest price
Synchronously Assembled Product
9/30/2015
45
Product getProduct(Long productId) {
BaseProduct bp = baseProductClient.getBaseProduct(productId); // Blocking WS Call
Double displayPrice = Double.MAX_VALUE;
List<Option> optionList = Lists.newArrayList();
int totalInventory = 0;
for (BaseOption bo : bp.getOptions()) {
Double price = priceClient.getPrice(bo.getId()); // Blocking WS Call
Integer inventory = inventoryClient.getInventory(bo.getId()); // Blocking WS Call
optionList.add(new Option().withId(bo.getId())..withPrice(..).withInventory(..));
totalInventory += inventory;
displayPrice = (price < lowestPrice) ? price : lowestPrice;
}
return new Product().withId(bp.getId().withDescription(bp.getDescription())
.withInventory(totalInventory).withPrice(displayPrice).withOptions(optionList);
}
RxJava
Build and return Product
Observable Non Blocking
9/30/2015RxJava
46
Blocking to Non Blocking (?)
9/30/2015RxJava
47
BaseProduct getBaseProductSync(Long productId) {
// Synchronous Blocking WS Call
return …;
}
Observable<BaseProduct> getBaseProduct(Long productId) {
return Observable.just(getBaseProductSync(productId));
}
Call to getBaseProductSync() is
executed right away. A blocking
operation.
Blocking to Non Blocking
9/30/2015RxJava
48
BaseProduct getBaseProductSync(Long productId) {
// Synchronous Blocking WS Call
return …;
}
Observable<BaseProduct> getBaseProduct(Long productId) {
return Observable.defer(() -> Observable.just(getBaseProductSync(productId)));
}
Observable<BaseProduct> getBaseProduct(Long productId) {
return Observable.create(t -> {
BaseProduct bp = getBaseProductSync(productId);
if (!t.isUnsubscribed()) {
t.onNext(bp);
t.onCompleted();
}
});
}
Subscribe On Schedulers.io()
9/30/2015RxJava
49
BaseProduct getBaseProductSync(Long productId) {
// Synchronous Blocking WS Call
return …;
}
Observable<BaseProduct> getBaseProduct(Long productId) {
return Observable.defer(() -> Observable.just(getBaseProductSync(productId)))
.subscribeOn(Schedulers.io());
}
Observable<BaseProduct> getBaseProduct(Long productId) {
return Observable.create(t -> {
BaseProduct bp = getBaseProductSync(productId);
if (!t.isUnsubscribed()) {
t.onNext(bp);
t.onCompleted();
}
}).subscribeOn(Schedulers.io());
}
9/30/2015RxJava
50
Observable<Product>
9/30/2015RxJava
51
Observable<BaseProduct>
Observable<Product>
9/30/2015RxJava
52
Observable<BaseProduct>
Observable<Product>
Observable<Product>
9/30/2015RxJava
53
Observable<BaseProduct>
Observable<Product>
Observable<BaseOption>
Observable<Product>
BaseProduct
9/30/2015RxJava
54
Observable<BaseProduct>
Observable<Product>
Observable<BaseOption>
Observable<Option>
Observable<Product>
9/30/2015RxJava
55
Observable<BaseProduct>
Observable<Product>
Observable<BaseOption>
Observable<Option>
Observable<Integer>
Observable<Product>
BaseOption
9/30/2015RxJava
56
Observable<BaseProduct>
Observable<Product>
Observable<BaseOption>
Observable<Option>
Observable<Double>
Observable<Product>
Observable<Integer>
9/30/2015RxJava
57
Observable<BaseProduct>
Observable<Product>
Observable<BaseOption>
Observable<Option>
Observable<Option>
Observable<Product>
Observable<Integer>
Observable<Double>
9/30/2015RxJava
58
Observable<BaseProduct>
Observable<Product>
Observable<BaseOption>
Observable<Option>
Observable<Option>
Observable<Product>
Observable<List<Option>>
Observable<Integer>
Observable<Double>
9/30/2015RxJava
59
Observable<BaseProduct>
Observable<Product>
Observable<BaseOption>
Observable<Option>
Observable<Option>
Observable<Product>
Observable<List<Option>>
Observable<Product>
Observable<Integer>
Observable<Double>
9/30/2015RxJava
60
Observable<BaseProduct>
Observable<Product>
Observable<BaseOption>
Observable<Option>
Observable<Option>
Observable<Product>
Observable<List<Option>>
Observable<Product>
Observable<Product>
Observable<Integer>
Observable<Double>
9/30/2015RxJava
61
Observable<BaseProduct>
Observable<Product>
Observable<BaseOption>
Observable<Option>
Observable<Option>
Observable<Product>
Observable<List<Option>>
Observable<Product>
Observable<Product>
Observable<Integer>
Observable<Double>
Observable Product
9/30/2015
62
Observable<Product> getProduct(Long productId) {
return baseProductClient.getBaseProduct(productId)…
}
RxJava
Get Non Blocking
Base Product
Observable Product
9/30/2015
63
Observable<Product> getProduct(Long productId) {
return baseProductClient.getBaseProduct(productId).flatMap(baseProduct -> {
}
RxJava
Flat Map Base
Product
Observable Product
9/30/2015
64
Observable<Product> getProduct(Long productId) {
return baseProductClient.getBaseProduct(productId).flatMap(baseProduct -> {
Observable<Option> options = Observable.from(baseProduct.getBaseOptions())
.flatMap(bo -> {
}
RxJava
For Every
BaseOption,
flatMap.
Observable Product
9/30/2015
65
Observable<Product> getProduct(Long productId) {
return baseProductClient.getBaseProduct(productId).flatMap(baseProduct -> {
Observable<Option> options = Observable.from(baseProduct.getBaseOptions())
.flatMap(bo -> {
Observable<Double> optionPrice = priceClient.getPrice(bo.getId()));
Observable<Integer> optionInventory = inventoryClient.getInventory(bo.getId()));
}
RxJava
For Every Base
Option obtain
Inventory and Price
Observable Product
9/30/2015
66
Observable<Product> getProduct(Long productId) {
return baseProductClient.getBaseProduct(productId).flatMap(baseProduct -> {
Observable<Option> options = Observable.from(baseProduct.getBaseOptions())
.flatMap(bo -> {
Observable<Double> optionPrice = priceClient.getPrice(bo.getId()));
Observable<Integer> optionInventory = inventoryClient.getInventory(bo.getId()));
return Observable.zip(optionPrice , optionInventory,
(price , inventory) - > {
return new Option().withId(bo.getId()).withDescription(bo.getDescription())
.withPrice(price ).withInventory(inventory);
}
);
}
RxJava
Option = Zip [Base Option +
Inventory + Price]
Observable Product
9/30/2015
67
Observable<Product> getProduct(Long productId) {
return baseProductClient.getBaseProduct(productId).flatMap(baseProduct -> {
Observable<Option> options = Observable.from(baseProduct.getBaseOptions())
.flatMap(bo -> {
Observable<Double> optionPrice = priceClient.getPrice(bo.getId()));
Observable<Integer> optionInventory = inventoryClient.getInventory(bo.getId()));
return Observable.zip(optionPrice , optionInventory,
(price , inventory) - > {
return new Option().withId(bo.getId()).withDescription(bo.getDescription())
.withPrice(price ).withInventory(inventory);
}
);
return Observable.just(new Product()).zipWith(options.toList(), (product, optionList) -> {
Integer totalInventory = computeMaxInventory(optionList);
Double lowestPrice = computeLowestPrice(optionList);
return product.withId(baseProduct.getId())
.withDescription(baseProduct.getDescription()).withPrice(lowestPrice)
.withInventory(totalInventory).withOptions(optionList);
});
});
}
RxJava
Create Product from
Options, compute
Inventory and price
Observable Product - Test
9/30/2015RxJava
68
Long productId = 1L;
Observable<Product> product = getProduct(productId);
// The Observable only starts doing something after
// an observer subscribes
product.subscribe(p -> LOG.debug(p));
Example Run
9/30/2015RxJava
69
Cold and Hot Observable
9/30/2015RxJava
70
Observable Creation
9/30/2015
71
Observable<Customer> getCustomers() {
return Observable.create(new Observable.OnSubscribe<Customer>(){
public void call(Subscriber<Customer> subscriber){
Connection conn = getConnection();
Stmt stmt = conn.createStatement();
String sql = “select id, name from customer”;
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
Long id = rs.getLong(“id”);
String name = rs.getString(“name”);
Customer c = new Customer(id, name);
subscriber.onNext(c);
}
subscriber.onComplete();
…
}
});
}
Observable<Customer> customer = getCustomers();
RxJava
Cold Observable
 Is the more common types of Observables you
will deal with
 Does nothing until subscribed to
 In example shown, Database was not accessed
till a subscriber was present
9/30/2015RxJava
72
Hot Observable
 Hot observables are "live" sequences that are occurring
whether they are being observed or not.
 The canonical example is a mouse, which of course is
free to move regardless of whether you subscribe to an
Observable<MouseMoveEvent> or not.
9/30/2015RxJava
73
Hot Observable Example – Earth emitting Greetings
9/30/2015RxJava
74
Hello 1439838374206
Hola1439838374207
Namaste 1439838374210
Bonjour 1439838375707
Emitting Greetings in multiple languages + Timestamp
One Curious Observer
9/30/2015RxJava
75
Hello 143983837900
Hola143983837901
Namaste 1439838374210
Bonjour 1439123123000
Second Curious Observer
9/30/2015RxJava
76
Hello 143983837900
Hola143983837901
Namaste 1439838374210
Bonjour 1439123123000
Hot Observable using - ConnectableObservable
 A Connectable Observable resembles an ordinary
Observable, except that it does not begin emitting items
when it is subscribed to, but only when its connect()
method is called. Calling connect(), makes the
Observable ‘HOT’.
 ConnectableObservables can be created from any
Observable by calling publish() or replay()
9/30/2015RxJava
77
Connectable Observable
9/30/2015
78
Observable<String> earthGreetings
= Observable.create(new AlienGreetingOnSubscribe());
RxJava
An Observable
that emits
Greetings from
Earth
Connectable Observable
9/30/2015
79
Observable<String> earthGreetings
= Observable.create(new AlienGreetingOnSubscribe());
ConnectableObservable<String> hotGreetingObservable
= earthGreetings.publish();
RxJava
ConnectableObservable
from source by calling
publish ()
Connectable Observable
9/30/2015
80
Observable<String> earthGreetings
= Observable.create(new AlienGreetingOnSubscribe());
ConnectableObservable<String> hotGreetingObservable
= earthGreetings.publish();
hotGreetingObservable.connect();
RxJava
connect() makes the Observable
HOT! Starts emitting greetings.
Connectable Observable
9/30/2015
81
Observable<String> earthGreetings
= Observable.create(new AlienGreetingOnSubscribe());
ConnectableObservable<String> hotGreetingObservable
= earthGreetings.publish();
hotGreetingObservable.connect();
Subscription firstAlienShip = hotGreetingObservable
.subscribe(t -> LOG.debug(“First Alien Ship Received:” + t));
RxJava
First Alien Ship
subscribes for
messages.
Connectable Observable
9/30/2015
82
Observable<String> earthGreeting
= Observable.create(new AlienGreetingOnSubscribe());
ConnectableObservable<String> hotGreetingObservable
= earthGreeting.publish();
hotGreetingObservable.connect();
Subscription firstAlienShip = hotGreetingObservable
.subscribe(t -> LOG.debug(“First Alien Ship Received:” + t));
Thread.sleep(200);
RxJava
Simulate time
passing.
Connectable Observable
9/30/2015
83
Observable<String> earthGreetings
= Observable.create(new AlienGreetingOnSubscribe());
ConnectableObservable<String> hotGreetingObservable
= earthGreetings.publish();
hotGreetingObservable.connect();
Subscription firstAlienShip = hotGreetingObservable
.subscribe(t -> LOG.debug(“First Alien Ship Received:” + t));
Thread.sleep(200);
Subscription secondAlienShip = hotGreetingObservable
.subscribe(t -> LOG.debug(“Second Alien Ship Received:” + t));
RxJava
Second Alien Ship
subscribes and
receives
messages.
Console
9/30/2015RxJava
84
No
Subscribers
First
Observer
Second
Observer
ConnectableObservable – replay()
9/30/2015
85
Observable<String> earthGreetings
= Observable.create(new AlienGreetingOnSubscribe());
ConnectableObservable<String> hotGreetingObservable
= earthGreeting.replay();
hotGreetingObservable.connect();
Thread.sleep(200); // Simulate a Delay
// Alien ship receives ALL greetings emitted
Subscription firstAlienShip = hotGreetingObservable
.subscribe(t -> LOG.debug(“First Alien Ship Received:” + t));
RxJava
HOT!!! Starts
emitting
greetings.
Console – replay()
9/30/2015RxJava
86
No
Subscribers
Observer gets it
all
PublishSubject to create a Hot Observable
Another way to create a Hot Observable
Left as an exercise for the viewer
 An example provided in slide deck
addendum
9/30/2015RxJava
87
Error Handling
9/30/2015RxJava
88
Erroring Observable
9/30/2015RxJava
89
Observable<BaseProduct> getProduct(Long productId) {
return Observable.create(new OnSubscribe() {
public void call(Subscriber subscriber) {
subscriber.onError
(new RuntimeException(“I’m Broken!!!”));
}
});
}
Error Handling – Bubbled up
9/30/2015RxJava
90
try {
Observable<BaseProduct> bp = getProduct(productId);
bp.subscribe(p -> LOG.info(p));
}
catch(RuntimeException e) {
LOG.info(“Exception Type:” + e.getClass() + “Message:”
+ e.getMessage());
}
Exception Type:class rx.exceptions.OnErrorNotImplementedException,
Message:Error Occured calling BaseProduct Service
Error Handling – Fallback
9/30/2015RxJava
91
Observable<BaseProduct> bp = getProduct(productId);
bp.onErrorResumeNext(t -> {
return Observable.just(BaseProduct.DEFAULT);
})
.subscribe(p -> LOG.info(p));
BaseProduct{id: {-1}, description: {N/A}, baseOptions: {[]}}
Error Handling – Retry
9/30/2015RxJava
92
Observable<BaseProduct> bp = getProduct(productId);
bp.retry((retryCount, throwable) -> {
LOG.info(“Base Product Error”, throwable);
return retryCount < 2 ? Boolean.TRUE:
Boolean.FALSE;
})
.subscribe(p -> LOG.info(p));
Would try twice to get the Base Product
Testing and Debugging
9/30/2015RxJava
93
Unit Testing
 rx.observers.TestSubscriber
 Do not use in production code
 Has a bunch of convenience methods
 awaitTerminalEvent()
 assertNoErrors()
 assertReceivedOnNext(…)
 assertUnsubscribed()
9/30/2015RxJava
94
Testing
9/30/2015RxJava
95
TestSubscriber<String> ts = new TestSubscriber<>();
Observable.interval(200, TimeUnit.MILLISECONDS)
.take(3)
.map(t -> {return “Hello:” + t})
.subscribe(ts);
ts.awaitTerminalEvent();
ts.assertNotErrors();
ts.assertReceivedOnNext(Lists.newArrayList(“Hello:0”,
“Hello:1”, “Hello:2”));
This is async!
What is the hardest part of
concurrent programming?
9/30/2015RxJava
96
Debugging
 Never easy!
 Not just a problem of Rx as much as simply the nature of it
 Stepping through code is severely limited
 Stack Trace can be difficult to understand
 System.out.println or debug statements your friends
 doOnNext(), doOnError()
9/30/2015RxJava
97
doOnNext()
@Test
public void doOnNextTest() {
List<String> received = Lists.newArrayList();
Observable<String> o = Observable.just(“a”,“b”,“c”) // Test Observable
.doOnNext( s -> received.add(s));
TestSubscriber<String> test = new TestSubscriber();
o.subscribe(test);
test.awaitTerminalEvent();
assertEquals(Lists.newArrayList(“a”,“b”,“c”), received);
}
9/30/2015RxJava
98
doOnCompleted()
Observable.just(1, 2, 3, 4)
.take(3) // only emit 3 items
.doOnCompleted(() ->System.out.println(“DONE!”))
.subscribe(i -> System.out.print( i ));
Output:
123DONE!
9/30/2015RxJava
99
Conclusion
9/30/2015RxJava
100
The Good
 Makes Asynchronous code easy to develop
 Lots of online resources and examples
 Strong use of functional style of programming
 Netflix used RxNetty and Reactive Programming
to considerable performance advantage over
Tomcat
 Web Service Clients like Jersey and Retrofit
support RxJava
9/30/2015RxJava
101
The Bad
The Bad
 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 classes would make this a
non-starter
 Thank heavens for Java 8 Lambdas
9/30/2015RxJava
102
The Ugly
The Ugly
Debugging and Stepping through async
code is painful
Stack Traces are difficult to follow
Author’s recommend
System.out.println or Log.debug(..)
doOnNext() and doOnError()
9/30/2015RxJava
103
Resources
 Intro to Rx - http://www.introtorx.com/
 ReactiveX – http://reactivex.io
 RxMarbles - http://rxmarbles.com/
 RxJava - https://github.com/ReactiveX/RxJava
 Air Traffic Control Simulation
 https://github.com/digitaljoel/rxAir
9/30/2015RxJava
104
Thank you and Questions…
9/30/2015RxJava
105
https://github.com/digitaljoel/rxAir
Addendum
9/30/2015RxJava
106
Subjects
9/30/2015RxJava
107
Subject
 Subject is an object that can be both Observable and
Observer
 Bridge between Observable and Observer
 Subject can subscribe to an Observable acting like an
Observer
 Subject can emit new items like an Observable
9/30/2015RxJava
108
Publish Subject
 Subject that, once an Observer
has subscribed, emits all
subsequently observed items to
the subscriber
9/30/2015RxJava
109
PublishSubject<Object> subject = PublishSubject.create();
subject.onNext(“one”);
subject.subscribe(t - > LOG.info(“Observer1:” + t));
subject.onNext(“two”);
subject.subscribe(t - > LOG.info(“Observer2:” + t));
subject.onNext(“three”);
Replay Subject
 Subject that buffers all items it
observes and replays them to any
Observer that subscribes.
9/30/2015RxJava
110
ReplaySubject<Object> subject = ReplaySubject.create();
subject.onNext(“one”);
subject.onNext(“two”);
subject.onNext(“three”);
subject.onCompleted();
subject.subscribe(t - > LOG.info(“Observer1:” + t));
subject.subscribe(t - > LOG.info(“Observer2:” + t));
Subjects not discussed
Behavior Subject
Async Subject
Slides provided at end
9/30/2015RxJava
111
Publish Subject for Hot Observable
9/30/2015
112
Observable<String> earthGreeting
= Observable.create(new AlienGreetingOnSubscribe());
Subject<String, String> publishSubject = PublishSubject.create();
earthGreeting.subscribe(publishSubject);
Subscription firstAlienShip = publishSubject
.subscribe(t -> LOG.debug(“First Alien Ship Received:” + t));
Thread.sleep(200);
Subscription secondAlienShip = publishSubject
.subscribe(t -> LOG.debug(“Second Alien Ship Received:” + t));
HOT!!!
RxJava
Console
9/30/2015RxJava
113
No
Subscribers
First
Observer
Second
Observer

More Related Content

What's hot (20)

PDF
Streams, Streams Everywhere! An Introduction to Rx
Andrzej Sitek
 
PDF
Reactive programming with RxJava
Jobaer Chowdhury
 
PPTX
Intro to Functional Programming with RxJava
Mike Nakhimovich
 
PPTX
Reactive Java (GeeCON 2014)
Tomasz Kowalczewski
 
PPTX
Introduction to Reactive Java
Tomasz Kowalczewski
 
PDF
Building Scalable Stateless Applications with RxJava
Rick Warren
 
PDF
RxJava for Android - GDG DevFest Ukraine 2015
Constantine Mars
 
PDF
Reactive Android: RxJava and beyond
Fabio Tiriticco
 
PDF
Reactive programming on Android
Tomáš Kypta
 
PPTX
Rx java in action
Pratama Nur Wijaya
 
PDF
Practical RxJava for Android
Tomáš Kypta
 
PPTX
RxJava Applied
Igor Lozynskyi
 
PDF
RxJava from the trenches
Peter Hendriks
 
PDF
Rxjava 介紹與 Android 中的 RxJava
Kros Huang
 
PDF
How to Think in RxJava Before Reacting
IndicThreads
 
PDF
RxJava applied [JavaDay Kyiv 2016]
Igor Lozynskyi
 
PPTX
Reactive Programming on Android - RxAndroid - RxJava
Ali Muzaffar
 
PPTX
Reactive programming with RxAndroid
Savvycom Savvycom
 
PDF
Introduction to RxJS
Brainhub
 
PDF
GKAC 2015 Apr. - RxAndroid
GDG Korea
 
Streams, Streams Everywhere! An Introduction to Rx
Andrzej Sitek
 
Reactive programming with RxJava
Jobaer Chowdhury
 
Intro to Functional Programming with RxJava
Mike Nakhimovich
 
Reactive Java (GeeCON 2014)
Tomasz Kowalczewski
 
Introduction to Reactive Java
Tomasz Kowalczewski
 
Building Scalable Stateless Applications with RxJava
Rick Warren
 
RxJava for Android - GDG DevFest Ukraine 2015
Constantine Mars
 
Reactive Android: RxJava and beyond
Fabio Tiriticco
 
Reactive programming on Android
Tomáš Kypta
 
Rx java in action
Pratama Nur Wijaya
 
Practical RxJava for Android
Tomáš Kypta
 
RxJava Applied
Igor Lozynskyi
 
RxJava from the trenches
Peter Hendriks
 
Rxjava 介紹與 Android 中的 RxJava
Kros Huang
 
How to Think in RxJava Before Reacting
IndicThreads
 
RxJava applied [JavaDay Kyiv 2016]
Igor Lozynskyi
 
Reactive Programming on Android - RxAndroid - RxJava
Ali Muzaffar
 
Reactive programming with RxAndroid
Savvycom Savvycom
 
Introduction to RxJS
Brainhub
 
GKAC 2015 Apr. - RxAndroid
GDG Korea
 

Similar to An Introduction to RxJava (20)

PPTX
RxJava 2 Reactive extensions for the JVM
Netesh Kumar
 
PDF
Reactive java - Reactive Programming + RxJava
NexThoughts Technologies
 
PDF
RxJava - introduction & design
allegro.tech
 
PPTX
Introduction to RxJava on Android
Chris Arriola
 
PDF
Intro to Rx Java
Syed Awais Mazhar Bukhari
 
PPTX
Microservices Part 4: Functional Reactive Programming
Araf Karsh Hamid
 
PPTX
Rxandroid
Thinh Thanh
 
PPTX
RxAndroid
Thinh Thanh
 
PDF
The Mayans Lost Guide to RxJava on Android
Fernando Cejas
 
PPTX
Reactive Programming in Java 8 with Rx-Java
Kasun Indrasiri
 
PDF
Reactive Programming in Java by Mario Fusco - Codemotion Rome 2015
Codemotion
 
PDF
Reactive Programming for a demanding world: building event-driven and respons...
Mario Fusco
 
PPTX
Reactive Programming on Android
Guilherme Branco
 
PPTX
Intro to Reactive Thinking and RxJava 2
JollyRogers5
 
PPTX
Rx for Android & iOS by Harin Trivedi
harintrivedi
 
PDF
RxJava@Android
Maxim Volgin
 
PDF
RxJava@DAUG
Maxim Volgin
 
PDF
RxJava pour Android : présentation lors du GDG Android Montréal
Sidereo
 
PDF
Saving lives with rx java
Shahar Barsheshet
 
PPTX
Functional reactive programming
Araf Karsh Hamid
 
RxJava 2 Reactive extensions for the JVM
Netesh Kumar
 
Reactive java - Reactive Programming + RxJava
NexThoughts Technologies
 
RxJava - introduction & design
allegro.tech
 
Introduction to RxJava on Android
Chris Arriola
 
Intro to Rx Java
Syed Awais Mazhar Bukhari
 
Microservices Part 4: Functional Reactive Programming
Araf Karsh Hamid
 
Rxandroid
Thinh Thanh
 
RxAndroid
Thinh Thanh
 
The Mayans Lost Guide to RxJava on Android
Fernando Cejas
 
Reactive Programming in Java 8 with Rx-Java
Kasun Indrasiri
 
Reactive Programming in Java by Mario Fusco - Codemotion Rome 2015
Codemotion
 
Reactive Programming for a demanding world: building event-driven and respons...
Mario Fusco
 
Reactive Programming on Android
Guilherme Branco
 
Intro to Reactive Thinking and RxJava 2
JollyRogers5
 
Rx for Android & iOS by Harin Trivedi
harintrivedi
 
RxJava@Android
Maxim Volgin
 
RxJava@DAUG
Maxim Volgin
 
RxJava pour Android : présentation lors du GDG Android Montréal
Sidereo
 
Saving lives with rx java
Shahar Barsheshet
 
Functional reactive programming
Araf Karsh Hamid
 
Ad

Recently uploaded (20)

PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PPTX
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PDF
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
PPTX
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PPTX
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PDF
Executive Business Intelligence Dashboards
vandeslie24
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
PPTX
Engineering the Java Web Application (MVC)
abhishekoza1981
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PPTX
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
PPTX
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
Executive Business Intelligence Dashboards
vandeslie24
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
Engineering the Java Web Application (MVC)
abhishekoza1981
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Human Resources Information System (HRIS)
Amity University, Patna
 
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
Ad

An Introduction to RxJava

Editor's Notes

  • #5: Reactive is responsive to a stimulus
  • #6: Responsive – React to users demands Elastic – React to load Resilient – React to Errors and Failures Message Driven – React to events and messages
  • #7: A Stream of Datum Finite or Infinite Disclaimer – All images used in this slide are property of original authors. We are simply using the same for demonstration.
  • #8: Reactive Programming is a push based model rather than pull. Events are pushed to you rather than you blocking and pulling for it Promotes parallelism and rich transformation of data via pure and higher order functions
  • #10: A behavior GOF Design Pattern A way to message dependent objects of changes Subject keeps a list of Objects that need to be notified of
  • #11: ReactiveX extends the behavior of the Observer Pattern and provides the mentioned items Observable and Subjects are two producing entities - Observable is the source of data stream Observer listens to emitted values Observer subscribes to an Observable Observer reacts to items emitted by Observable
  • #14: Observable that emits 0 to 9.The observer is notified via the onNext and completed with onCompleted or onError
  • #15: Some common creational operators mentioned above
  • #16: Iterable consumer blocks synchronously and pulls values from producer Observable producer asynchronously pushes values to the Observer whenever values are available RxJava Enhances GOF Observer pattern with onCompleted and onError to notify Observer
  • #21: Maps items to items of another type. This example does Integer to Integer You can go from any type to any other type using the function.
  • #22: The key is that the result of the function used by flatmap is an Observable. Resulting Observables are all merged together in true merge fashion (interleved.) Map is item -> item. FlatMap is item -> Observable
  • #23: Filter - Filter based of a Predicate test Distinct - Distinct elements from source Take - Take ‘n’ items from source First - – Take first item
  • #24: Sample filters to even numbers only. Notice that result of Func1 must be a Boolean.
  • #25: Your service returns 50 recommendations but you know you only need to display 6 of them.
  • #26: Merge – combine multiple observables into one by combining items Concat – Combines multiple observables with another without interleaving Zip – Combine emission of multiple observables together via a specified function and emit a single item for each combination of items
  • #27: One possible result could include 0, 10, 1, 11…
  • #28: Note that the first and second observables could be emitting concurrently, but the result of concat maintains the order. Result would be 1..20, not interleaved.
  • #29: Note different marble diagram here. This one is more clear than the one matching the other styles.
  • #30: Note that we are calling zipWith on first, not Observable.zip
  • #33: io() - > Scheduler for I/O bound work. Executor Thread pool grows as needed Computation() - > Returns a Scheduler for computational work like loops, algorithmic etc. newThread() - > Returns a Scheduler that spawns a new Thread for each unit of work Trampoline() -> Queues work on the current thread to be executed after current work completes Immediate() -> Creates and returns a scheduler that executes work immediately on the current thread Test() -> Creates a Scheduler that allows for debugging fromExecutor() - > From a custom Executor
  • #40: Product Gateway Service = Aggregates ( Base Product + Product Price + Inventory) to give a Product Base Product Service provides a BaseProduct which contains id, description and BaseOptions. Each Base Option contains an id and description.
  • #41: ProductGatewayService uses the three Web Service Clients to obtain back and aggregate the data to create a Product
  • #42: In the case of our model. We have a few assumptions - A Product itself has no direct Price or Inventory – they are inferred from Options - The display Price of the product is the lowest Option Price - The total Inventory of a Product is the sum of Option inventory - The Base Option is augmented with inventory and price from the Inventory and Pricing web services accordingly to produce the Product
  • #43: Blocking I/O bound call
  • #49: Two simple ways of converting a blocking call to a deferred one. Use the defer() operator that that an ObservableFactory to defer the creation of the observable. Use the Observable.OnSubscribe() to make the invocation and notify.
  • #71: Sanjay
  • #75: A Hot Earth Observable emitting Greetings in multiple languages in the hope some ‘subscriber’ hears…
  • #76: One observer picks up the signal and starts observing…
  • #77: After a period of time a second Observer picks up the signal and starts hearing…
  • #79: AlienGreetingOnSubscribe is responsible for emitting greetings periodically in all languages….
  • #86: Replaying of all items emitted to subscribers
  • #89: Joel
  • #91: Exception thrown in the subscribe. Don’t want to have to try/catch all of our observable operations.
  • #92: If onError is called, then use the Observable returned by the Action passed to onErrorResumeNext.
  • #93: If the source observable sends onError then resubscribe in the hopes that it will complete without error. For instance, if it’s a webservice call that times-out give it a second chance (if it has a low timeout!) This example is the verbose one. Without the throwable then it would track up to retryCount automatically.
  • #101: Sanjay
  • #110: TALK THROUGH THIS CODE!