Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Learning RxJava
Learning RxJava

Learning RxJava: Reactive, Concurrent, and responsive applications

eBook
$27.98 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Learning RxJava

Observables and Subscribers

We already got a glimpse into the Observable and how it works in Chapter 1, Thinking Reactively. You probably have many questions on how exactly it operates and what practical applications it holds. This chapter will provide a foundation for understanding how an Observable works as well as the critical relationship it has with the Observer. We will also cover several ways to create an Observable as well make it useful by covering a few operators. To make the rest of the book flow smoothly, we will also cover all critical nuances head-on to build a solid foundation and not leave you with surprises later.

Here is what we will cover in this chapter:

  • The Observable
  • The Observer
  • Other Observable factories
  • Single, Completable, and Maybe
  • Disposable

The Observable

As introduced in Chapter 1, Thinking Reactively, the Observable is a push-based, composable iterator. For a given Observable<T>, it pushes items (called emissions) of type T through a series of operators until it finally arrives at a final Observer, which consumes the items. We will cover several ways to create an Observable, but first, let's dive into how an Observable works through its onNext(), onCompleted(), and onError() calls.

How Observables work

Before we do anything else, we need to study how an Observable sequentially passes items down a chain to an Observer. At the highest level, an Observable works by passing three types of events:

  • onNext(): This passes each item one at a time from the...

The Observer interface

The onNext(), onComplete(), and onError() methods actually define the Observer type, an abstract interface implemented throughout RxJava to communicate these events. This is the Observer definition in RxJava shown in the code snippet. Do not bother yourself about onSubscribe() for now, as we will cover it at the end of this chapter. Just bring your attention to the other three methods:

    package io.reactivex;

import io.reactivex.disposables.Disposable;

public interface Observer<T> {
void onSubscribe(Disposable d);
void onNext(T value);
void onError(Throwable e);
void onComplete();
}

Observers and source Observables are somewhat relative. In one context, a source Observable is where your Observable chain starts and where emissions originate. In our previous examples, you could say that the Observable returned from...

Cold versus hot Observables

There are subtle behaviors in a relationship between an Observable and an Observer depending on how the Observable is implemented. A major characteristic to be aware of is cold versus hot Observables, which defines how Observables behave when there are multiple Observers. First, we will cover cold Observables.

Cold Observables

Cold Observables are much like a music CD that can be replayed to each listener, so each person can hear all the tracks at any time. In the same manner, cold Observables will replay the emissions to each Observer, ensuring that all Observers get all the data. Most data-driven Observables are cold, and this includes the Observable.just() and Observable.fromIterable() factories...

Other Observable sources

We already covered a few factories to create Observable sources, including Observable.create(), Observable.just(), and Observable.fromIterable(). After our detour covering Observers and their nuances, let's pick up where we left off and cover a few more Observable factories.

Observable.range()

To emit a consecutive range of integers, you can use Observable.range(). This will emit each number from a start value and increment each emission until the specified count is reached. These numbers are all passed through the onNext() event, followed by the  onComplete() event:

    import io.reactivex.Observable;

public class Launcher {
public static void main(String[] args) {
Observable...

Single, Completable, and Maybe

There are a few specialized flavors of Observable that are explicitly set up for one or no emissions: Single, Maybe, and Completable. These all follow the Observable closely and should be intuitive to use in your reactive coding workflow. You can create them in similar ways as the Observable (for example, they each have their own create() factory), but certain Observable operators may return them too.

Single

Single<T> is essentially an Observable<T> that will only emit one item. It works just like an Observable, but it is limited only to operators that make sense for a single emission. It has its own SingleObserver interface as well:

    interface SingleObserver<T> {
void...

Disposing

When you subscribe() to an Observable to receive emissions, a stream is created to process these emissions through the Observable chain. Of course, this uses resources. When we are done, we want to dispose of these resources so that they can be garbage-collected. Thankfully, the finite Observables that call onComplete() will typically dispose of themselves safely when they are done. But if you are working with infinite or long-running Observables, you likely will run into situations where you want to explicitly stop the emissions and dispose of everything associated with that subscription. As a matter of fact, you cannot trust the garbage collector to take care of active subscriptions that you no longer need, and explicit disposal is necessary in order to prevent memory leaks.

The Disposable is a link between an Observable and an active Observer,...

Summary

This was an intense chapter, but it will provide a solid foundation as you learn how to use RxJava to tackle real-world work. RxJava, with all of its expressive power, has some nuances that are entirely due to the change of mindset it demands. It has done an impressive amount of work taking an imperative language like Java and adapting it to become reactive and functional. But this interoperability requires some understanding of the implementations between an Observable and a Observer. We touched on various ways to create Observables as well as how they interact with Observers.

Take your time trying to digest all this information but do not let it stop you from moving on to the next two chapters, where the usefulness of RxJava starts to take formation. In the next chapters, the pragmatic usefulness of RxJava will start to become clear.

 

...
Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • 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

Description

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.

Who is this book 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

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jun 20, 2017
Length: 400 pages
Edition : 1st
Language : English
ISBN-13 : 9781787123748
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

Publication date : Jun 20, 2017
Length: 400 pages
Edition : 1st
Language : English
ISBN-13 : 9781787123748
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 152.97
Reactive Programming in Kotlin
$48.99
Learning RxJava
$48.99
Java 9 Concurrency Cookbook, Second Edition
$54.99
Total $ 152.97 Stars icon

Table of Contents

12 Chapters
Thinking Reactively Chevron down icon Chevron up icon
Observables and Subscribers Chevron down icon Chevron up icon
Basic Operators Chevron down icon Chevron up icon
Combining Observables Chevron down icon Chevron up icon
Multicasting, Replaying, and Caching Chevron down icon Chevron up icon
Concurrency and Parallelization Chevron down icon Chevron up icon
Switching, Throttling, Windowing, and Buffering Chevron down icon Chevron up icon
Flowables and Backpressure Chevron down icon Chevron up icon
Transformers and Custom Operators Chevron down icon Chevron up icon
Testing and Debugging Chevron down icon Chevron up icon
RxJava on Android Chevron down icon Chevron up icon
Using RxJava for Kotlin New Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(10 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Everardo López Nov 25, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Excelente! Altamente recomendable.
Amazon Verified review Amazon
Cliente Amazon Oct 02, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Il paradigma reactive viene descritto con ampio dettaglio e molti esempi.Naturalmente serve di base una buona conoscenza di Java, anche se gli esempi ripetono continuamente molti passaggi banali.
Amazon Verified review Amazon
Wil C Apr 27, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is a fantastic, comprehensive overview of RxJava2. This goes well beyond the public javadoc (which is also fantastic as compared to most libs). Most of the book is also applicable to RxJs (4.x & 5.x). The book gives a framework/context for understanding the hundreds of available operators and good advice about when to use things.
Amazon Verified review Amazon
Corn Starch Apr 11, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I read so many articles and even another highly rated rxjava book. This is by far the most effective book and I'm finally learning!
Amazon Verified review Amazon
Cliente Amazon Oct 04, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Ótimo livro. O autor tem uma didática muito boa, abordando os temas sobre programação reativa com rxjava 2 de forma simples e objetiva
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.