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

What's hot (20)

Streams, Streams Everywhere! An Introduction to Rx
Streams, Streams Everywhere! An Introduction to RxStreams, Streams Everywhere! An Introduction to Rx
Streams, Streams Everywhere! An Introduction to Rx
 
Reactive programming with RxJava
Reactive programming with RxJavaReactive programming with RxJava
Reactive programming with RxJava
 
Intro to Functional Programming with RxJava
Intro to Functional Programming with RxJavaIntro to Functional Programming with RxJava
Intro to Functional Programming with RxJava
 
Reactive Java (GeeCON 2014)
Reactive Java (GeeCON 2014)Reactive Java (GeeCON 2014)
Reactive Java (GeeCON 2014)
 
Introduction to Reactive Java
Introduction to Reactive JavaIntroduction to Reactive Java
Introduction to Reactive Java
 
Building Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJava
 
RxJava for Android - GDG DevFest Ukraine 2015
RxJava for Android - GDG DevFest Ukraine 2015RxJava for Android - GDG DevFest Ukraine 2015
RxJava for Android - GDG DevFest Ukraine 2015
 
Reactive Android: RxJava and beyond
Reactive Android: RxJava and beyondReactive Android: RxJava and beyond
Reactive Android: RxJava and beyond
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on Android
 
Rx java in action
Rx java in actionRx java in action
Rx java in action
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for Android
 
RxJava Applied
RxJava AppliedRxJava Applied
RxJava Applied
 
RxJava from the trenches
RxJava from the trenchesRxJava from the trenches
RxJava from the trenches
 
Rxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJavaRxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJava
 
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
 
RxJava applied [JavaDay Kyiv 2016]
RxJava applied [JavaDay Kyiv 2016]RxJava applied [JavaDay Kyiv 2016]
RxJava applied [JavaDay Kyiv 2016]
 
Reactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJavaReactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJava
 
Reactive programming with RxAndroid
Reactive programming with RxAndroidReactive programming with RxAndroid
Reactive programming with RxAndroid
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJS
 
GKAC 2015 Apr. - RxAndroid
GKAC 2015 Apr. - RxAndroidGKAC 2015 Apr. - RxAndroid
GKAC 2015 Apr. - RxAndroid
 

Similar to An Introduction to RxJava

COS30008 Semester 1, 2016 Dr. Markus Lumpe 1 Swinbu.docx
COS30008 Semester 1, 2016 Dr. Markus Lumpe  1 Swinbu.docxCOS30008 Semester 1, 2016 Dr. Markus Lumpe  1 Swinbu.docx
COS30008 Semester 1, 2016 Dr. Markus Lumpe 1 Swinbu.docx
vanesaburnand
 

Similar to An Introduction to RxJava (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
 
Iniciación rx java
Iniciación rx javaIniciación rx java
Iniciación rx java
 
Tech Talk #4 : RxJava and Using RxJava in MVP - Dương Văn Tới
Tech Talk #4 : RxJava and Using RxJava in MVP - Dương Văn TớiTech Talk #4 : RxJava and Using RxJava in MVP - Dương Văn Tới
Tech Talk #4 : RxJava and Using RxJava in MVP - Dương Văn Tới
 
Akka.NET streams and reactive streams
Akka.NET streams and reactive streamsAkka.NET streams and reactive streams
Akka.NET streams and reactive streams
 
Rx workshop
Rx workshopRx workshop
Rx workshop
 
4Developers 2015: Programowanie synchroniczne i asynchroniczne - dwa światy k...
4Developers 2015: Programowanie synchroniczne i asynchroniczne - dwa światy k...4Developers 2015: Programowanie synchroniczne i asynchroniczne - dwa światy k...
4Developers 2015: Programowanie synchroniczne i asynchroniczne - dwa światy k...
 
2 презентация rx java+android
2 презентация rx java+android2 презентация rx java+android
2 презентация rx java+android
 
RxJava2 Slides
RxJava2 SlidesRxJava2 Slides
RxJava2 Slides
 
响应式编程及框架
响应式编程及框架响应式编程及框架
响应式编程及框架
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on Android
 
RxJava и Android. Плюсы, минусы, подводные камни
RxJava и Android. Плюсы, минусы, подводные камниRxJava и Android. Плюсы, минусы, подводные камни
RxJava и Android. Плюсы, минусы, подводные камни
 
The Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on AndroidThe Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on Android
 
Intro to Reactive Thinking and RxJava 2
Intro to Reactive Thinking and RxJava 2Intro to Reactive Thinking and RxJava 2
Intro to Reactive Thinking and RxJava 2
 
Rx java2 - Should I use it?
Rx java2 - Should I use it?Rx java2 - Should I use it?
Rx java2 - Should I use it?
 
Control system Lab record
Control system Lab record Control system Lab record
Control system Lab record
 
COS30008 Semester 1, 2016 Dr. Markus Lumpe 1 Swinbu.docx
COS30008 Semester 1, 2016 Dr. Markus Lumpe  1 Swinbu.docxCOS30008 Semester 1, 2016 Dr. Markus Lumpe  1 Swinbu.docx
COS30008 Semester 1, 2016 Dr. Markus Lumpe 1 Swinbu.docx
 
Saving lives with rx java
Saving lives with rx javaSaving lives with rx java
Saving lives with rx java
 
Funtional Reactive Programming with Examples in Scala + GWT
Funtional Reactive Programming with Examples in Scala + GWTFuntional Reactive Programming with Examples in Scala + GWT
Funtional Reactive Programming with Examples in Scala + GWT
 
Java Performance Puzzlers
Java Performance PuzzlersJava Performance Puzzlers
Java Performance Puzzlers
 
Intro to Rx Java
Intro to Rx JavaIntro to Rx Java
Intro to Rx Java
 

Recently uploaded

Recently uploaded (20)

Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
 
WSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration Tooling
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
WSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in UgandaWSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in Uganda
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million PeopleWSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & InnovationWSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdfAzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
 

An Introduction to RxJava

Editor's Notes

  1. Reactive is responsive to a stimulus
  2. Responsive – React to users demands Elastic – React to load Resilient – React to Errors and Failures Message Driven – React to events and messages
  3. 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.
  4. 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
  5. 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
  6. 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
  7. Observable that emits 0 to 9.The observer is notified via the onNext and completed with onCompleted or onError
  8. Some common creational operators mentioned above
  9. 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
  10. 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.
  11. 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
  12. Filter - Filter based of a Predicate test Distinct - Distinct elements from source Take - Take ‘n’ items from source First - – Take first item
  13. Sample filters to even numbers only. Notice that result of Func1 must be a Boolean.
  14. Your service returns 50 recommendations but you know you only need to display 6 of them.
  15. 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
  16. One possible result could include 0, 10, 1, 11…
  17. 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.
  18. Note different marble diagram here. This one is more clear than the one matching the other styles.
  19. Note that we are calling zipWith on first, not Observable.zip
  20. 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
  21. 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.
  22. ProductGatewayService uses the three Web Service Clients to obtain back and aggregate the data to create a Product
  23. 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
  24. Blocking I/O bound call
  25. 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.
  26. Sanjay
  27. A Hot Earth Observable emitting Greetings in multiple languages in the hope some ‘subscriber’ hears…
  28. One observer picks up the signal and starts observing…
  29. After a period of time a second Observer picks up the signal and starts hearing…
  30. AlienGreetingOnSubscribe is responsible for emitting greetings periodically in all languages….
  31. Replaying of all items emitted to subscribers
  32. Joel
  33. Exception thrown in the subscribe. Don’t want to have to try/catch all of our observable operations.
  34. If onError is called, then use the Observable returned by the Action passed to onErrorResumeNext.
  35. 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.
  36. Sanjay
  37. TALK THROUGH THIS CODE!