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.

979d93b360f80486b121486a9d063ad5?s=128

Hiroshi Kurokawa

March 09, 2017
Tweet

Transcript

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

  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
  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 )
 }
 }
  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
  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);
  6. WHAT’S NEW IN RXJAVA 2 ▸ Reactive Streams compliant ▸

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

    Single/Maybe/Completable friendly ▸ Performance improvement
  8. Reactive Streams?

  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)
  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/)
  11. REACTIVE STREAMS public interface Publisher<T> {
 public void subscribe(Subscriber<? super

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


    public void onNext(T t);
 public void onError(Throwable t);
 public void onComplete();
 }
  13. REACTIVE STREAMS public interface Subscriber<T> {
 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
  14. REACTIVE STREAMS public interface Subscriber<T> {
 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
  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)
  16. REACTIVE STREAMS public interface Processor<T, R> extends Subscriber<T>, Publisher<R> {


    } ▸ Processor represents a processing stage ▸ It is just like Subject in RxJava 1.x
  17. What’s Backpressure?

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

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

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

  21. process( ) WITH BACKPRESSURE Publisher Subscriber

  22. process( ) WITH BACKPRESSURE Publisher Subscriber request 1

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

  24. 2 TYPES OF DATA SOURCE ▸ Controllable (Backpressure-aware) ▸ Disk

    read ▸ Database ▸ Uncontrollable (Not Backpressure-aware) ▸ GUI events
  25. WHAT’S NEW IN RXJAVA 2 ▸ (cont’d) Reactive Streams compliant

    ▸ Single/Maybe/Completable friendly ▸ Performance improvement
  26. REACTIVE STREAMS COMPLIANT ▸ Package: rx. → io.reactivex. ▸ Class:

    Subscriber → Observer/Subscriber, Subscription → Disposable, etc. ▸ Method: onCompleted() → onComplete(), etc.
  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
  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 */);
  29. ADVANTAGES OF REACTIVE STREAMS ▸ Integration with other libraries (Akka,

    Actor, etc.) ▸ Might make it easy to work with Java 9 APIs
  30. WHAT’S NEW IN RXJAVA 2 ▸ Reactive Streams compliant ▸

    Single/Maybe/Completable friendly ▸ Performance improvement
  31. SINGLE/MAYBE/COMPLETABLE ▸ Single:
 onSubscribe (onSuccess | onError)? ▸ Maybe:
 onSubscribe

    (onSuccess | onError | onComplete)? ▸ Completable:
 onSubscribe (onComplete | onError)? At most one element → No Backpressure
  32. SINGLE/MAYBE/COMPLETABLE ▸ toList() / toMap() Observable (1.x) → Single (2.x)

  33. SINGLE/MAYBE/COMPLETABLE ▸ firstOrDefault() / all() / any() / count() …

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

    (2.x) takeFirst
  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<Item> getItem(@Path("id") int id); @POST(“/api/items") Completable postItem(@Body Item item);
  36. WHAT’S NEW IN RXJAVA 2 ▸ Reactive Streams compliant ▸

    Single/Maybe/Completable friendly ▸ Performance improvement
  37. PERFORMANCE IMPROVEMENT (http://akarnokd.blogspot.jp/2016/12/the-reactive-scrabble-benchmarks.html) RxJava 1 (*) lower score is better

    RxJava 2
  38. PERFORMANCE IMPROVEMENT ▸ Less overhead with Observable (no BP) ▸

    Leaner architecture with fewer indirections
  39. So should I move to RxJava 2? ¯\_(π)_/¯

  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
  41. LIBRARIES AROUND RXJAVA ▸ RxAndroid supports RxJava 2 # ▸

    Retrofit supports RxJava 2 # ▸ RxBinding supports RxJava 2 # ▸ RxJava2Interop (https://github.com/akarnokd/ RxJava2Interop)
  42. SOME HINTS FOR MIGRATION ▸ Read What’s different in 2.0

    (https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0)
  43. SOME HINTS FOR MIGRATION ▸ Avoid passing nulls PublishSubject<Void> subject

    = PublishSubject.create(); subject.onNext(. );

  44. SOME HINTS FOR MIGRATION ▸ Avoid passing nulls enum Irrelevant

    { INSTANCE } PublishSubject<Irrelevant> subject = PublishSubject.create(); subject.onNext(Irrelevant.INSTANCE);
  45. SOME HINTS FOR MIGRATION ▸ Avoid passing nulls ▸ Warning:

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

    “passing 'null' argument to parameter annotated as @NotNull" ▸ Search Structurally
  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))
  48. SOME HINTS FOR MIGRATION ▸ Avoid passing nulls ▸ RxJava2Extensions

    (https://github.com/ akarnokd/RxJava2Extensions)
  49. SUMMARY ▸ What’s New in RxJava 2 ▸ Reactive Streams

    compliant ▸ Single/Maybe/Completable friendly ▸ Performance improvement ▸ Advantages of RxJava 2 ▸ Try RxJava 2!
  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