I’m still exploring reactive programming world and RxJava library. Recently, I’ve migrated a few of my open-source libraries from RxJava1 to RxJava2 and written yet another project in RxJava2 from the beginning. Nevertheless, I’m still learning this library and its concept. It’s very wide topic. In RxJava1 we simply had one reactive data type called
Observable. In RxJava2, we have more data types like
Completable. In this article, I’ll briefly explain their purpose and tell you when to use which. The general idea behind these types is code semantics. We should tell consumer of our code, what he or she can expect from our API. Introducing more reactive data types can increase readability and stability of our code base.
Observable is basically the same Reactive type, we had in RxJava1. It doesn’t have backpressure support. We should use
- our data source emits less than 1000 items, so there’s practically no chance of occurring
- we are working with GUI events, which usually don’t occurs very often and don’t have to be backpressured
- we are working with synchronous code on legacy JVM like Java 1.6 and we want to have streams features like in Java 8
Flowable type has very similar semantics to
Observable. We can operate on
Flowable streams with map, flatmap, filter, etc. in the same way as on the
Observable type. The main difference is backpressure support. We should use
Flowable when we are:
- dealing with 10k+ elements in a stream
- dealing with frequent events (e.g. sensors readings)
- reading/parsing files from disk
- reading values from database through JDBC
- using network/streaming I/O
- reading/writing to many blocking or pull-based data sources
To learn more, read note about Observable vs. Flowable on wiki of RxJava2 on GitHub.
Single reactive type has been redesigned from scratch in RxJava 2. It’s designed to handle just one event in an asynchronous manner. Good application of this type is single HTTP request when we expect just one response or error and nothing else. It can emit on
onSuccess (single value) or
onError event (error).
Maybe represents a deferred computation and emission of a maybe value or exception.
Maybe is a wrapper around an operation/event that may have either:
- A single result
- No result
Just take a look at the scheme. The interface of the main consumer of this type have the following methods:
Maybe is a union of
Completable providing the means to capture an emission pattern where there could be 0 or 1 item or an error signaled by some reactive source.
Completable type can be used when we have an
Observable that we don’t care about the value resulted from the operation (result is void). It handles only
onError events. Conceptually,
Maybe is a union of
Completable providing the means to capture an emission pattern where there could be 0 or 1 item or an error signalled by some reactive source. Read more about
Maybe type on RxJava wiki.
As we can see, RxJava2 gives us new types, which can help explain our intentions more clearly. We can adjust concrete type to the specific situation. In addition, we can use backpressure for the data sources, which emit a lot of elements to make our projects more robust and stable. Last, but not least RxJava2 is compatible with Reactive Streams API, which is going to be part of the Java 9 specification.
- JavaDoc for RxJava2
- What’s diffferent in 2.0?
- StackOverflow Thread: What is the difference between Observable, Completable and Single in RxJava?
- StackOverflow Thread: What’s the difference between RxJava2’s Maybe and Optional?
- Clearer RxJava intentions with Single and Completable
- Blog of Akarnokd (one of the main RxJava contributors)
- RxJava GitHub repository
- Reactive Programming with RxJava (O’Reilly Media Book)
- Reactive Streams
- Reactive Manifesto