Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What's New in RxJava 2

What's New in RxJava 2

This talk explains what is different in RxJava 2 from RxJava 1. There are 3 major changes - Reactive Streams compliant, Single/Maybe/Completable friendliness and performance improvement. From Reactive Streams point of view, it will describe how and why the changes are needed.

In addition to that, the talk will describe how to migrate to RxJava 2 briefly.

Hiroshi Kurokawa

March 09, 2017
Tweet

More Decks by Hiroshi Kurokawa

Other Decks in Technology

Transcript

  1. WHAT’S NEW IN RXJAVA 2
    2017.03.09
    HIROSHI KUROKAWA

    FABLIC, INC.

    View Slide

  2. 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

    View Slide

  3. 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 )

    }

    }

    View Slide

  4. 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

    View Slide

  5. 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);

    View Slide

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

    View Slide

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

    View Slide

  8. Reactive Streams?

    View Slide

  9. 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)

    View Slide

  10. 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/)

    View Slide

  11. 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

    View Slide

  12. REACTIVE STREAMS
    public interface Subscriber {

    public void onSubscribe(Subscription s);

    public void onNext(T t);

    public void onError(Throwable t);

    public void onComplete();

    }

    View Slide

  13. 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

    View Slide

  14. 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

    View Slide

  15. 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)

    View Slide

  16. REACTIVE STREAMS
    public interface Processor extends
    Subscriber, Publisher {

    }
    ▸ Processor represents a processing stage
    ▸ It is just like Subject in RxJava 1.x

    View Slide

  17. What’s Backpressure?

    View Slide

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

    View Slide

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

    View Slide

  20. WITH BACKPRESSURE
    Publisher
    Subscriber
    process( )
    subscribe & request 1

    View Slide

  21. process( )
    WITH BACKPRESSURE
    Publisher
    Subscriber

    View Slide

  22. process( )
    WITH BACKPRESSURE
    Publisher
    Subscriber
    request 1

    View Slide

  23. process( )
    WITH BACKPRESSURE
    Publisher
    Subscriber
    No OutOfMemoryError #

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  27. 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

    View Slide

  28. 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 */);

    View Slide

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

    View Slide

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

    View Slide

  31. SINGLE/MAYBE/COMPLETABLE
    ▸ Single:

    onSubscribe (onSuccess | onError)?
    ▸ Maybe:

    onSubscribe (onSuccess | onError | onComplete)?
    ▸ Completable:

    onSubscribe (onComplete | onError)?
    At most one element → No Backpressure

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  35. 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);

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  40. 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

    View Slide

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

    View Slide

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

    View Slide

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


    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  47. 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))

    View Slide

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

    View Slide

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

    View Slide

  50. 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

    View Slide