* Discover different ways to create Observables, Observers, and Subscribers
* Multicast in order to push data to multiple destinations and cache and replay them
* Express RxJava idiomatically with the help of Kotlin features such as extension functions and data classes
* Become familiar with various operators available in RxJava to perform common transformations and tasks
* Explore RxJava’s reactive types, including Flowable, Single, Maybe, and Completable
* Demystify Observables and how they express data and events as sequences
----
RxJava is not just a popular library for building asynchronous and event-based applications; it also enables you to create a cleaner and more readable code base. In this book, you’ll cover the core fundamentals of reactive programming and learn how to design and implement reactive libraries and applications.
Learning RxJava will help you understand how reactive programming works and guide you in writing your first example in reactive code. You’ll get to grips with the workings of Observable and Subscriber, and see how they are used in different contexts using real-world use cases. The book will also take you through multicasting and caching to help prevent redundant work with multiple Observers. You’ll then learn how to create your own RxJava operators by reusing reactive logic. As you advance, you’ll explore effective tools and libraries to test and debug RxJava code. Finally, you’ll delve into RxAndroid extensions and use Kotlin features to streamline your Android apps.
By the end of this book, you'll become proficient in writing reactive code in Java and Kotlin to build concurrent applications, including Android applications.
----
Nick Samoylov graduated from the Moscow Institute of Physics and Technology, has worked as a theoretical physicist, and learned to program as a tool for testing his mathematical models. Following the demise of the USSR, Nick created and successfully ran his own software company. In 1999, with his wife Luda and two daughters, he emigrated to the USA and has been living in Colorado ever since, working as a Java programmer. In his spare time, Nick likes to write and hike in the Rocky Mountains.
Thomas Nield is a business consultant for Southwest Airlines in Schedule Initiatives, and a maintainer for RxJavaFX and RxKotlin. Early in his career, he became fascinated with technology and its role in business analytics. After becoming proficient in Java, Kotlin, Python, SQL, and reactive programming, he became an open source contributor and an author/speaker at O'Reilly Media. He is passionate about sharing what he learns and enabling others with new skill sets. He enjoys making technical content relatable and relevant to those unfamiliar with it or who are intimidated by it. He is interested in data science, reactive programming, and the Kotlin language. He has also authored the book Getting Started with SQL, by O'Reilly Media.
Author(s): Nick Samoylov, Thomas Nield
Edition: 2
Publisher: Packt
Year: 2020
Language: English
Pages: 412
Tags: java rxjava 202 java 14
Cover
Title Page
Copyright and Credits
Dedication
About Packt
Contributors
Table of Contents
Preface
Section 1: Foundations of Reactive Programming in Java
Chapter 1: Thinking Reactively
A brief history of ReactiveX and RxJava
Thinking reactively
Why should I learn RxJava?
What will you learn in this book?
Setting up
Navigating the central repository
Using Gradle
Using Maven
A brief exposure to RxJava
RxJava 1.x, 2.x, or 3.0 – which one do I use?
When to use RxJava
Summary
Chapter 2: Observable and Observer
The Observable
The workings of Observable
Using Observable.create()
Using Observable.just()
The Observer interface
Implementing and subscribing to an Observer
Shorthand Observer with lambdas
Cold versus hot observables
A cold Observable
A hot Observable
ConnectableObservable
Other Observable sources
Observable.range()
Observable.interval()
Observable.future()
Observable.empty()
Observable.never()
Observable.error()
Observable.defer()
Observable.fromCallable()
Single, Completable, and Maybe
Single
Maybe
Completable
Disposing
Handling a disposable within an Observer
Using CompositeDisposable
Handling disposal with Observable.create()
Summary
Chapter 3: Basic Operators
Conditional operators
takeWhile() and skipWhile()
defaultIfEmpty()
switchIfEmpty()
Suppressing operators
filter()
take()
skip()
distinct()
distinctUntilChanged()
elementAt()
Transforming operators
map()
cast()
startWithItem()
sorted()
scan()
Reducing operators
count()
reduce()
Boolean operators
all()
any()
isEmpty()
contains()
sequenceEqual()
Collection operators
toList()
toSortedList()
toMap() and toMultiMap()
collect()
Error recovery operators
onErrorReturnItem() and onErrorReturn()
onErrorResumeWith()
retry()
Action operators
doOnNext() and doAfterNext()
doOnComplete() and doOnError()
doOnEach()
doOnSubscribe() and doOnDispose()
doOnSuccess()
doFinally()
Utility operators
delay()
repeat()
single( )
timestamp( )
timeInterval()
Summary
Section 2: Reactive Operators
Chapter 4: Combining Observables
Merging factories and operators
Observable.merge() factory and mergeWith() operator
flatMap()
flatMap() with combiner
Concatenating factories and operators
Observable.concat() factory and concatWith() operator
concatMap()
Ambiguous operators
Zipping operators
Combining the latest operators
withLatestFrom()
Grouping operators
Summary
Chapter 5: Multicasting, Replaying, and Caching
Understanding multicasting
Multicasting with operators
When to multicast
Automatic connection
autoConnect()
refCount() and share()
Replaying and caching
Replaying
Caching
Subjects
PublishSubject
When to use Subject
When a Subject goes wrong
Serializing a Subject object
BehaviorSubject
ReplaySubject
AsyncSubject
UnicastSubject
Summary
Chapter 6: Concurrency and Parallelization
Why concurrency is necessary
Concurrency in a nutshell
Understanding parallelization
Introducing RxJava concurrency
Keeping an application alive
Understanding schedulers
Computation
I/O
New thread
Single
Trampoline
ExecutorService
Starting and shutting down schedulers
Understanding subscribeOn()
Nuances of subscribeOn()
Understanding observeOn()
Using observeOn() for UI event threads
Nuances of observeOn()
Parallelization
Understanding unsubscribeOn()
Summary
Chapter 7: Switching, Throttling, Windowing, and Buffering
Buffering
Fixed-size buffering
Time-based buffering
Boundary-based buffering
Windowing
Fixed-size windowing
Time-based windowing
Boundary-based windowing
Throttling
throttleLast() or sample()
throttleFirst()
throttleWithTimeout() or debounce()
Switching
Grouping keystrokes
Summary
Chapter 8: Flowable and Backpressure
Understanding backpressure
An example that needs backpressure
Introducing the Flowable
When to use Flowable and backpressure
Understanding Flowable and Subscriber
Interface Subscriber
Creating Flowable
Using Flowable.create() and BackpressureStrategy
Turning Observable into Flowable and vice versa
Using onBackpressureXXX() operators
onBackPressureBuffer()
onBackPressureLatest()
onBackPressureDrop()
Using Flowable.generate()
Summary
Chapter 9: Transformers and Custom Operators
Composing new operators from existing ones using compose() and transformers
Using ObservableTransformer
Using FlowableTransformer
Avoiding a shared state with transformers
Using the to() operator for fluent conversion
Creating new operators from scratch using lift()
Implementing an ObservableOperator
Implementing a FlowableOperator
Creating a new operator for Single, Maybe, or Completable
Using RxJava2-Extras and RxJava2Extensions
Summary
Section 3: Integration of RxJava applications
Chapter 10: Testing and Debugging
Configuring JUnit
Blocking subscribers
Blocking operators
blockingFirst()
blockingGet()
blockingLast()
blockingIterable()
blockingForEach()
blockingNext()
blockingLatest()
blockingMostRecent()
Using TestObserver and TestSubscriber
Manipulating time with TestScheduler
Debugging RxJava code
Summary
Chapter 11: RxJava on Android
Creating an Android project
Configuring Retrolambda
Configuring RxJava and friends
Using RxJava and RxAndroid
Using RxBinding
Other RxAndroid bindings libraries
Life cycles and cautions using RxJava with Android
Summary
Chapter 12: Using RxJava for Kotlin
Why Kotlin?
Configuring Kotlin
Configuring Kotlin with Gradle
Configuring Kotlin with Maven
Configuring RxJava and RxKotlin
Kotlin basics
Creating a Kotlin file
Assigning properties and variables
Extension functions
Kotlin lambda expressions
Extension operators
Using RxKotlin
Dealing with SAM ambiguity
Using let() and apply()
Using let()
Using apply()
Tuple and data classes
The future of ReactiveX and Kotlin
Summary
Appendix A: Introducing Lambda Expressions
Appendix B: Functional Types
Appendix C: Mixing Object-Oriented and Reactive Programming
Appendix D: Materializing and Dematerializing
Appendix E: Understanding Schedulers
Other Books You May Enjoy
Index