Learning RxJava

Book description

Reactive Programming with Java and ReactiveX

About This Book

  • Explore the essential tools and operators RxJava provides, and know which situations to use them in
  • Delve into Observables and Subscribers, the core components of RxJava used for building scalable and performant reactive applications
  • Delve into the practical implementation of tools to effectively take on complex tasks such as concurrency and backpressure

Who This Book Is For

The primary audience for this book is developers with at least a fundamental mastery of Java.

Some readers will likely be interested in RxJava to make programs more resilient, concurrent, and scalable. Others may be checking out reactive programming just to see what it is all about, and to judge whether it can solve any problems they may have.

What You Will Learn

  • Learn the features of RxJava 2 that bring about many significant changes, including new reactive types such as Flowable, Single, Maybe, and Completable
  • Understand how reactive programming works and the mindset to "think reactively"
  • Demystify the Observable and how it quickly expresses data and events as sequences
  • Learn the various Rx operators that transform, filter, and combine data and event sequences
  • Leverage multicasting to push data to multiple destinations, and cache and replay them
  • Discover how concurrency and parallelization work in RxJava, and how it makes these traditionally complex tasks trivial to implement
  • Apply RxJava and Retrolambda to the Android domain to create responsive Android apps with better user experiences
  • Use RxJava with the Kotlin language to express RxJava more idiomatically with extension functions, data classes, and other Kotlin features

In Detail

RxJava is a library for composing asynchronous and event-based programs using Observable sequences for the JVM, allowing developers to build robust applications in less time.

Learning RxJava addresses all the fundamentals of reactive programming to help readers write reactive code, as well as teach them an effective approach to designing and implementing reactive libraries and applications.

Starting with a brief introduction to reactive programming concepts, there is an overview of Observables and Observers, the core components of RxJava, and how to combine different streams of data and events together. You will also learn simpler ways to achieve concurrency and remain highly performant, with no need for synchronization. Later on, we will leverage backpressure and other strategies to cope with rapidly-producing sources to prevent bottlenecks in your application. After covering custom operators, testing, and debugging, the book dives into hands-on examples using RxJava on Android as well as Kotlin.

Style and approach

This book will be different from other Rx books, taking an approach that comprehensively covers Rx concepts and practical applications.

Table of contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Thinking Reactively
    1. A brief history of ReactiveX and RxJava
    2. Thinking reactively
    3. Why should I learn RxJava?
      1. What we will learn in this book?
    4. Setting up
      1. Navigating the Central Repository
        1. Using Gradle
        2. Using Maven
    5. A quick exposure to RxJava
    6. RxJava 1.0 versus RxJava 2.0 - which one do I use?
    7. When to use RxJava
    8. Summary
  3. Observables and Subscribers
    1. The Observable
      1. How Observables work
      2. Using Observable.create()
      3. Using Observable.just()
    2. The Observer interface
      1. Implementing and subscribing to an Observer
      2. Shorthand Observers with lambdas
    3. Cold versus hot Observables
      1. Cold Observables
      2. Hot Observables
      3. ConnectableObservable
    4. Other Observable sources
      1. Observable.range()
      2. Observable.interval()
      3. Observable.future()
      4. Observable.empty()
      5. Observable.never()
      6. Observable.error()
      7. Observable.defer()
      8. Observable.fromCallable()
    5. Single, Completable, and Maybe
      1. Single
      2. Maybe
      3. Completable
    6. Disposing
      1. Handling a Disposable within an Observer
      2. Using CompositeDisposable
      3. Handling Disposal with Observable.create()
    7. Summary
  4. Basic Operators
    1. Suppressing operators
      1. filter()
      2. take()
      3. skip()
      4. takeWhile() and skipWhile()
      5. distinct()
      6. distinctUntilChanged()
      7. elementAt()
    2. Transforming operators
      1. map()
      2. cast()
      3. startWith()
      4. defaultIfEmpty()
      5. switchIfEmpty()
      6. sorted()
      7. delay()
      8. repeat()
      9. scan()
    3. Reducing operators
      1. count()
      2. reduce()
      3. all()
      4. any()
      5. contains()
    4. Collection operators
      1. toList()
      2. toSortedList()
      3. toMap() and toMultiMap()
      4. collect()
    5. Error recovery operators
      1. onErrorReturn() and onErrorReturnItem()
      2. onErrorResumeNext()
      3. retry()
    6. Action operators
      1. doOnNext(), doOnComplete(), and doOnError()
      2. doOnSubscribe() and doOnDispose()
      3. doOnSuccess()
    7. Summary
  5. Combining Observables
    1. Merging
      1. Observable.merge() and mergeWith()
      2. flatMap()
    2. Concatenation
      1. Observable.concat() and concatWith()
      2. concatMap()
    3. Ambiguous
    4. Zipping
    5. Combine latest
      1. withLatestFrom()
    6. Grouping
    7. Summary
  6. Multicasting, Replaying, and Caching
    1. Understanding multicasting
      1. Multicasting with operators
      2. When to multicast
    2. Automatic connection
      1. autoConnect()
      2. refCount() and share()
    3. Replaying and caching
      1. Replaying
      2. Caching
    4. Subjects
      1. PublishSubject
      2. When to use Subjects
      3. When Subjects go wrong
      4. Serializing Subjects
      5. BehaviorSubject
      6. ReplaySubject
      7. AsyncSubject
      8. UnicastSubject
    5. Summary
  7. Concurrency and Parallelization
    1. Why concurrency is necessary
    2. Concurrency in a nutshell
      1. Understanding parallelization
    3. Introducing RxJava concurrency
      1. Keeping an application alive
    4. Understanding Schedulers
      1. Computation
      2. IO
      3. New thread
      4. Single
      5. Trampoline
      6. ExecutorService
      7. Starting and shutting down Schedulers
    5. Understanding subscribeOn()
      1. Nuances of subscribeOn()
    6. Understanding observeOn()
      1. Using observeOn() for UI event threads
      2. Nuances of observeOn()
    7. Parallelization
    8. unsubscribeOn()
    9. Summary
  8. Switching, Throttling, Windowing, and Buffering
    1. Buffering
      1. Fixed-size buffering
      2. Time-based buffering
      3. Boundary-based buffering
    2. Windowing
      1. Fixed-size windowing
      2. Time-based windowing
      3. Boundary-based windowing
    3. Throttling
      1. throttleLast() / sample()
      2. throttleFirst()
      3. throttleWithTimeout() / debounce()
    4. Switching
    5. Grouping keystrokes
    6. Summary
  9. Flowables and Backpressure
    1. Understanding backpressure
      1. An example that needs backpressure
      2. Introducing the Flowable
      3. When to use Flowables and backpressure
        1. Use an Observable If...
        2. Use a Flowable If...
    2. Understanding the Flowable and Subscriber
      1. The Subscriber
    3. Creating a Flowable
      1. Using Flowable.create() and BackpressureStrategy
      2. Turning an Observable into a Flowable (and vice-versa)
    4. Using onBackpressureXXX() operators
      1. onBackPressureBuffer()
      2. onBackPressureLatest()
      3. onBackPressureDrop()
    5. Using Flowable.generate()
    6. Summary
  10. Transformers and Custom Operators
    1. Transformers
      1. ObservableTransformer
      2. FlowableTransformer
      3. Avoiding shared state with Transformers
    2. Using to() for fluent conversion
    3. Operators
      1. Implementing an ObservableOperator
      2. FlowableOperator
    4. Custom Transformers and operators for Singles, Maybes, and Completables
    5. Using RxJava2-Extras and RxJava2Extensions
    6. Summary
  11. Testing and Debugging
    1. Configuring JUnit
    2. Blocking subscribers
    3. Blocking operators
      1. blockingFirst()
      2. blockingGet()
      3. blockingLast()
      4. blockingIterable()
      5. blockingForEach()
      6. blockingNext()
      7. blockingLatest()
      8. blockingMostRecent()
    4. Using TestObserver and TestSubscriber
    5. Manipulating time with the TestScheduler
    6. Debugging RxJava code
    7. Summary
  12. RxJava on Android
    1. Creating the Android project
      1. Configuring Retrolambda
      2. Configuring RxJava and friends
    2. Using RxJava and RxAndroid
      1. Using RxBinding
    3. Other RxAndroid bindings libraries
    4. Life cycles and cautions using RxJava with Android
    5. Summary
  13. Using RxJava for Kotlin New
    1. Why Kotlin?
    2. Configuring Kotlin
      1. Configuring Kotlin for Gradle
      2. Configuring Kotlin for Maven
      3. Configuring RxJava and RxKotlin
    3. Kotlin basics
      1. Creating a Kotlin file
      2. Assigning properties and variables
      3. Extension functions
      4. Kotlin lambdas
    4. Extension operators
    5. Using RxKotlin
    6. Dealing with SAM ambiguity
    7. Using let() and apply()
      1. Using let()
      2. Using apply()
    8. Tuples and data classes
    9. Future of ReactiveX and Kotlin
    10. Summary
  14. Appendix
    1. Introducing lambda expressions
      1. Making a Runnable a lambda
      2. Making a Supplier a lambda
      3. Making a Consumer a lambda
      4. Making a Function a lambda
    2. Functional types
    3. Mixing object-oriented and reactive programming
    4. Materializing and Dematerializing
    5. Understanding Schedulers

Product information

  • Title: Learning RxJava
  • Author(s): Thomas Nield
  • Release date: June 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781787120426