Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

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 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
  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 for (int i : data) {
 if ( i % 2 == 0) {
 list.add( i * i )
 }
 }
  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 if ( % 2 == 0) x x square( ) 4 16 36
  4. 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);
  5. WHAT’S NEW IN RXJAVA 2 ▸ Reactive Streams compliant ▸

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

    Single/Maybe/Completable friendly ▸ Performance improvement
  7. 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)
  8. 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/)
  9. 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
  10. REACTIVE STREAMS public interface Subscriber<T> {
 public void onSubscribe(Subscription s);


    public void onNext(T t);
 public void onError(Throwable t);
 public void onComplete();
 }
  11. 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
  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();
 } ▸ onSubscribe onNext* (onError | onComplete)? ▸ onSubscribe, onNext, onError MUST throw NullPointerException if the given parameter is null
  13. 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)
  14. 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
  15. 2 TYPES OF DATA SOURCE ▸ Controllable (Backpressure-aware) ▸ Disk

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

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

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

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

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

    (onSuccess | onError | onComplete)? ▸ Completable:
 onSubscribe (onComplete | onError)? At most one element → No Backpressure
  23. 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);
  24. WHAT’S NEW IN RXJAVA 2 ▸ Reactive Streams compliant ▸

    Single/Maybe/Completable friendly ▸ Performance improvement
  25. PERFORMANCE IMPROVEMENT ▸ Less overhead with Observable (no BP) ▸

    Leaner architecture with fewer indirections
  26. 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
  27. LIBRARIES AROUND RXJAVA ▸ RxAndroid supports RxJava 2 # ▸

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

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

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

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

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

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

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

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

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