RxJava2 – how not to get lost



If you are using Java RX, chances are, like us, you’ve already fallen in love with the reactive world which the functional programming introduces into boring, static code.

We’ve been working with RxJava for the last year and a half and everything’s been great. Last October, however, saw the release of the 2nd generation of RxJava to the World.

This short article introduces what has changed and some advice on how to adopt RxJava2.

First off, it isn’t backward compatible with RxJava. There are some great benefits which we will discuss next, but if you have a large code base in RxJava you might want to consider whether to upgrade it or wait until your next project.

So what's new?

In the new library version, the creators decided to add more detail to work with types. The Observable and the Subscriber have been better thought out. New types have appeared, such as Flowable, Maybe and Nothing and the work with the library has become more evident.


Observable lost its ability to handle MissingBackpressureException. The ability to create Your Observable from null has disappeared.

Observable.just(null); //NullPointerException
Single.just(null); //NullPointerException

The ability to use true/1 still remains, but it's better to look at Completable or Obsetvable.fromCallable().


A new type was introduced. Flowable which is separated from the old Observable. Now Observable cannot handle MissingBackpressureException. And if your Subscriber may not have enough time to process data, you will have to use Flowable. The main feature of this type is that during the work you have to point out the handling strategy of MissingBackpressureException.


The example of creating Flowable:

Flowable.create((FlowableEmitter<Integer> emitter) -> {
}, BackpressureStrategy.BUFFER);

Single and Maybe

Single defines the flow which consists of one element. If the flow is empty – NoSuchElementExction occurs. In this case the type Maybe will be useful, which can return the empty flow.

.toSingle() //NoSuchElementException
.toMaybe() //Ok

Also the first() operator’s behavior changed .:

.first() //return Single -> NoSuchElementException
.firstElement() //return Maybe -> Ok


The subscriber has also changed. In its interface there appeared one more method in which the subscription delivers.

Flowable.range(1, 10).subscribe(new Subscriber<Integer>() {
public void onSubscribe(Subscription s) {
public void onNext(Integer t) {
public void onError(Throwable t) {
public void onComplete() {

The new interface added the ability to quickly cancel the subscription.


The old Composite Subscription was changed to CompositeDisposable:

CompositeDisposable composite = new CompositeDisposable();
composite.add(Flowable.range(1, 5).subscribeWith(subscriber));


The new library version has the changed which aids distinctness while writing the reactive flows. The readability of the code has improved and due to the new types appearing the amount of thrown exceptions has diminished.

The lack of backward compatibility library is a pain so we would recommend that you start using it with a brand-new project.

Article Rating

16 Reviews
4.8 / 5.0

We hope you enjoyed this article! It's very important for us to receive your feedback. You can use these emojis to describe your feelings.

  • 5
  • 4
  • 3
  • 2
  • 1
Request a quote
prev next
Be the first to receive helpful tips from Applikey
Please enter correct email address
Fasten your seat belts, we are taking off