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

Reactive Programming with RxJava

Reactive Programming with RxJava

Reactive Programming with RxJava for handling stream data

Sunghyouk Bae

April 25, 2018
Tweet

More Decks by Sunghyouk Bae

Other Decks in Programming

Transcript

  1. Agenda • Why Reactive Programming? • RxJava Observable • RxJava

    Operators • RxJava Schedulers • References 2
  2. ReactiveX • Started by Microsoft Rx Lib (Reactive Extensions) •

    For composing asynchronous and event-based programs by using observable sequences • Easy way to implement complicated non-blocking system • Easy to combine multiple data source responses • Resiliency / Fault tolerance / Deal heavy load • Why Reactive
  3. Reactive Streams Publisher Subscriber Subscribe then request data (Backpressure) 0..N

    Data published then 0..1 (Error or Completed) Example : Using Message Queue 1. Polling message queue for new message (traditional Kafka client) 2. Listener or callback for new message (spring-kafka use listener) 3. Reactive stream (kakfa streams) is reactive streams
  4. Iterable vs Observable Event Iterable (pull) Observable (push) Receive data

    T next() onNext(T) Discover error Throw Exception onError(Exception) Complete !hasNext() onCompleted()
  5. Use Observable getDataFromLocalMemory() .skip(10) .take(5) .map { s -> s

    + " transformed" } .forEach { println(“next => " + it) } Iterable Observable getDataFromNetwork() .skip(10) .take(5) .map { s -> s + " transformed" } .subscribe { println(“next => " + it) }
  6. Why Use Observable • Observables are composable • Observables are

    Flexible • Observables are less opinionated • Various implementation (thread-pool, event loops, non- blocking I/O) • Callback is hard to implement and maintain • Not only for UI, Useful for Distributed System
  7. Cold vs Hot Observable • Cold Observable • Like VOD

    (Video on demand) (Finite stream) • At time to subscribe, you can receive first data • Other subscribe, receive first data also • Hot Observable • Like TV or Radio (Infinite Stream) • At time to subscribe, you can receive published data at that time
  8. Other Objects • Flowable (RxJava2) • Observable with back pressure

    • Single • onSuccess/onFailure • Completable • onCompleted with no returns • Maybe (RxJava2) • Single + Completable
  9. Subject • Observable + Observer • Varieties of Subject •

    AsyncSubject - take last elements • BehaviorSubject - like hot observable + latest element • PublishSubject - like hot observable • ReplaySubject - cold observable
  10. RxJava Operators • Factory operators • create, defer, from, interval,

    just, range, repeat … • Transform • buffer, flatMap, groupBy, map, scan, window • Filtering • debounce, distinct, elementAt, filter, first, ignoreElements, last, sample, skip, skipLast, take, takeLast • Combine • and, then, when, combineLatest, join, merge, startWith, switch, zip
  11. RxJava Operators • Error handling (catch, retry) • Utility •

    delay, do, materialize/dematerialize, observeOn, serialize, subscribe, subscribeOn, timeInterval, timeout, timestamp, using • Conditional • all, amb, contains, defaultIfEmpty, sequenceEqual, skipUntil, skipWhile, takeUntil, takeWhile • Math/Aggregator • average, concat, count, max, min, reduce, sum
  12. RxJava Operators • ConnectableObservable operators • connect, publish, refcount, replay

    • Convert • to • Backpressure • onBackPressureBuffer, onBackpressureDrop, onBackpressureLatest
  13. Schedulers • Observable is single thread in default • Scheduling

    emit data - subscribeOn(scheduler) • Scheduling subscribe data - observeOn(scheduler) • Type of schedulers • computation, io, newThread, single, trampoline, from … • TestScheduler for simulate Observable timely emit
  14. References • Books • Reactive Programming with RxJava • Articles

    • Why Reactive • Advanced Reactive Java • RxJava Part1, Part 2, Part 3 • Testing RxJava, RxJava2