Slide 1

Slide 1 text

WHAT’S NEW IN RXJAVA 2 2017.03.09 HIROSHI KUROKAWA
 FABLIC, INC.

Slide 2

Slide 2 text

WHAT’S RXJAVA ▸ Handle a sequence of data over time as a stream ▸ Instead of gathering them, responding to each of them 1 2 3 4 5 6

Slide 3

Slide 3 text

WHAT’S RXJAVA ▸ Handle a sequence of data over time as a stream ▸ Instead of gathering them, responding to each of them 1 2 3 4 5 6 for (int i : data) {
 if ( i % 2 == 0) {
 list.add( i * i )
 }
 }

Slide 4

Slide 4 text

WHAT’S RXJAVA ▸ Handle a sequence of data over time as a stream ▸ Instead of gathering them, responding to each of them 1 2 3 4 5 6 if ( % 2 == 0) x x square( ) 4 16 36

Slide 5

Slide 5 text

WHAT’S RXJAVA ▸ Handle a sequence of data over time as a stream ▸ Instead of gathering them, responding to each of them Observable.just(1, 2, 3, 4, 5, 6) .filter(x -> x % 2 == 0) .map(x -> x * x) .subscribe(System.out::println);

Slide 6

Slide 6 text

WHAT’S NEW IN RXJAVA 2 ▸ Reactive Streams compliant ▸ Single/Maybe/Completable friendly ▸ Performance improvement

Slide 7

Slide 7 text

WHAT’S NEW IN RXJAVA 2 ▸ Reactive Streams compliant ▸ Single/Maybe/Completable friendly ▸ Performance improvement

Slide 8

Slide 8 text

Reactive Streams?

Slide 9

Slide 9 text

REACTIVE STREAMS (http://www.reactive-streams.org/) ▸ Developed by some committers of Reactive Programming libraries ▸ The libraries implementing Reactive Streams can be integrated with each other ▸ Results in a Java 9 Flow API (https:// community.oracle.com/docs/DOC-1006738)

Slide 10

Slide 10 text

REACTIVE STREAMS ▸ “a standard for asynchronous stream processing with non-blocking backpressure" ▸ Specification (4 interfaces) ▸ Publisher / Subscriber / Subscription / Processor ▸ Stream manipulation (filter, map, etc.) is not included (http://www.reactive-streams.org/)

Slide 11

Slide 11 text

REACTIVE STREAMS public interface Publisher {
 public void subscribe(Subscriber super T> s);
 } ▸ Emit events to the given Subscriber ▸ It is like Observable in RxJava 1.x

Slide 12

Slide 12 text

REACTIVE STREAMS public interface Subscriber {
 public void onSubscribe(Subscription s);
 public void onNext(T t);
 public void onError(Throwable t);
 public void onComplete();
 }

Slide 13

Slide 13 text

REACTIVE STREAMS public interface Subscriber {
 public void onSubscribe(Subscription s);
 public void onNext(T t);
 public void onError(Throwable t);
 public void onComplete();
 } ▸ Subscriber can request how many events it wants via the Subscription

Slide 14

Slide 14 text

REACTIVE STREAMS public interface Subscriber {
 public void onSubscribe(Subscription s);
 public void onNext(T t);
 public void onError(Throwable t);
 public void onComplete();
 } ▸ onSubscribe onNext* (onError | onComplete)? ▸ onSubscribe, onNext, onError MUST throw NullPointerException if the given parameter is null

Slide 15

Slide 15 text

REACTIVE STREAMS public interface Subscription {
 public void request(long n);
 public void cancel();
 } ▸ Subscriber can request events through the Subscription (request) ▸ Subscriber can signal to the Publisher that it no longer needs events (cancel)

Slide 16

Slide 16 text

REACTIVE STREAMS public interface Processor extends Subscriber, Publisher {
 } ▸ Processor represents a processing stage ▸ It is just like Subject in RxJava 1.x

Slide 17

Slide 17 text

What’s Backpressure?

Slide 18

Slide 18 text

IF WITHOUT BACKPRESSURE Publisher Subscriber process( ) Num processed: 0

Slide 19

Slide 19 text

process( ) IF WITHOUT BACKPRESSURE Publisher Subscriber Num processed: 2 OutOfMemoryError

Slide 20

Slide 20 text

WITH BACKPRESSURE Publisher Subscriber process( ) subscribe & request 1

Slide 21

Slide 21 text

process( ) WITH BACKPRESSURE Publisher Subscriber

Slide 22

Slide 22 text

process( ) WITH BACKPRESSURE Publisher Subscriber request 1

Slide 23

Slide 23 text

process( ) WITH BACKPRESSURE Publisher Subscriber No OutOfMemoryError #

Slide 24

Slide 24 text

2 TYPES OF DATA SOURCE ▸ Controllable (Backpressure-aware) ▸ Disk read ▸ Database ▸ Uncontrollable (Not Backpressure-aware) ▸ GUI events

Slide 25

Slide 25 text

WHAT’S NEW IN RXJAVA 2 ▸ (cont’d) Reactive Streams compliant ▸ Single/Maybe/Completable friendly ▸ Performance improvement

Slide 26

Slide 26 text

REACTIVE STREAMS COMPLIANT ▸ Package: rx. → io.reactivex. ▸ Class: Subscriber → Observer/Subscriber, Subscription → Disposable, etc. ▸ Method: onCompleted() → onComplete(), etc.

Slide 27

Slide 27 text

BACKPRESUURE-AWARE NAMES 1.x 2.x BP 2.x No BP Observable Flowable Observable Subscriber Subscriber Observer Subject Processor Subject Subscription Disposable/ Subscription Disposable BP: Backpressure

Slide 28

Slide 28 text

AVOID NULLS Observable.just(null); // NPE Observable.fromCallable(() -> null)
 .subscribe(
 System.out ::println,
 Throwable ::printStackTrace /* NPE */);
 
 Observable.just(1)
 .map(v -> null)
 .subscribe(
 System.out ::println,
 Throwable ::printStackTrace /* NPE */);

Slide 29

Slide 29 text

ADVANTAGES OF REACTIVE STREAMS ▸ Integration with other libraries (Akka, Actor, etc.) ▸ Might make it easy to work with Java 9 APIs

Slide 30

Slide 30 text

WHAT’S NEW IN RXJAVA 2 ▸ Reactive Streams compliant ▸ Single/Maybe/Completable friendly ▸ Performance improvement

Slide 31

Slide 31 text

SINGLE/MAYBE/COMPLETABLE ▸ Single:
 onSubscribe (onSuccess | onError)? ▸ Maybe:
 onSubscribe (onSuccess | onError | onComplete)? ▸ Completable:
 onSubscribe (onComplete | onError)? At most one element → No Backpressure

Slide 32

Slide 32 text

SINGLE/MAYBE/COMPLETABLE ▸ toList() / toMap() Observable (1.x) → Single (2.x)

Slide 33

Slide 33 text

SINGLE/MAYBE/COMPLETABLE ▸ firstOrDefault() / all() / any() / count() … Observable (1.x) → Single (2.x)

Slide 34

Slide 34 text

SINGLE/MAYBE/COMPLETABLE ▸ takeFirst() / takeLast(1) … Observable (1.x) → Maybe (2.x) takeFirst

Slide 35

Slide 35 text

TAKE ADVANTAGE OF SINGLE/MAYBE/COMPLETABLE ▸ The code becomes more clear and efficient ▸ retrofit2-rxjava2-adapter supports Single and Completable @GET(“/api/items/{id}") Single getItem(@Path("id") int id); @POST(“/api/items") Completable postItem(@Body Item item);

Slide 36

Slide 36 text

WHAT’S NEW IN RXJAVA 2 ▸ Reactive Streams compliant ▸ Single/Maybe/Completable friendly ▸ Performance improvement

Slide 37

Slide 37 text

PERFORMANCE IMPROVEMENT (http://akarnokd.blogspot.jp/2016/12/the-reactive-scrabble-benchmarks.html) RxJava 1 (*) lower score is better RxJava 2

Slide 38

Slide 38 text

PERFORMANCE IMPROVEMENT ▸ Less overhead with Observable (no BP) ▸ Leaner architecture with fewer indirections

Slide 39

Slide 39 text

So should I move to RxJava 2? ¯\_(π)_/¯

Slide 40

Slide 40 text

YOU SHOULD ▸ RxJava 1.x EOL announced ▸ Feature freeze (2017 Jun) ▸ End of life (2018 March) ▸ Single / Maybe / Completable is useful ▸ Get used to the new terminologies and rules (e.g. avoiding nulls) earlier

Slide 41

Slide 41 text

LIBRARIES AROUND RXJAVA ▸ RxAndroid supports RxJava 2 # ▸ Retrofit supports RxJava 2 # ▸ RxBinding supports RxJava 2 # ▸ RxJava2Interop (https://github.com/akarnokd/ RxJava2Interop)

Slide 42

Slide 42 text

SOME HINTS FOR MIGRATION ▸ Read What’s different in 2.0 (https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0)

Slide 43

Slide 43 text

SOME HINTS FOR MIGRATION ▸ Avoid passing nulls PublishSubject subject = PublishSubject.create(); subject.onNext(. );


Slide 44

Slide 44 text

SOME HINTS FOR MIGRATION ▸ Avoid passing nulls enum Irrelevant { INSTANCE } PublishSubject subject = PublishSubject.create(); subject.onNext(Irrelevant.INSTANCE);

Slide 45

Slide 45 text

SOME HINTS FOR MIGRATION ▸ Avoid passing nulls ▸ Warning: “passing 'null' argument to parameter annotated as @NotNull"

Slide 46

Slide 46 text

SOME HINTS FOR MIGRATION ▸ Avoid passing nulls ▸ Warning: “passing 'null' argument to parameter annotated as @NotNull" ▸ Search Structurally

Slide 47

Slide 47 text

SOME HINTS FOR MIGRATION ▸ Avoid passing nulls ▸ Warning: “passing 'null' argument to parameter annotated as @NotNull" ▸ Search Structurally ▸ Be cautious on map() operator .map(key -> map.get(key))

Slide 48

Slide 48 text

SOME HINTS FOR MIGRATION ▸ Avoid passing nulls ▸ RxJava2Extensions (https://github.com/ akarnokd/RxJava2Extensions)

Slide 49

Slide 49 text

SUMMARY ▸ What’s New in RxJava 2 ▸ Reactive Streams compliant ▸ Single/Maybe/Completable friendly ▸ Performance improvement ▸ Advantages of RxJava 2 ▸ Try RxJava 2!

Slide 50

Slide 50 text

INTERESTED IN REACTIVE PROGRAMMING? ▸ I wrote an article about RxJava practices Publish at Apr. 11 2017 • Kotlin • RxJava • Firebase • Testing • Analytics http://gihyo.jp/book/2017/978-4-7741-8863-8