Skip to content

Vertx Rxjava

DodaTech 1 min read

In this tutorial, you'll learn about Fix Vert.x RxJava Not Integrating. We cover key concepts, practical examples, and best practices.

The Problem

RxJava Observables created from Vert.x APIs do not emit items or complete.

Quick Fix

Use Rxified Vert.x API

Wrong:

Observable<Buffer> obs = Observable.fromFuture(vertx.fileSystem().readFile("file.txt"));

Output:

Manual conversion

Right:

RxHelper.observableStream(vertx.eventBus().consumer("address"))
    .subscribe(msg -> System.out.println(msg.body()));

Output:

RxJava native API

Use Flowable for backpressure

Wrong:

Observable.fromFuture(client.get(...)) // No backpressure

Output:

May overflow

Right:

Flowable.fromPublisher(client.get(...).toFlowable())
    .onBackpressureBuffer()
    .subscribe(System.out::println);

Output:

Backpressure handled

Handle errors with RxJava

Wrong:

Observable.fromFuture(client.get(...))
    .subscribe(data -> { }); // No error handler

Output:

Errors lost

Right:

Observable.fromFuture(client.get(...))
    .subscribe(
        data -> { },
        error -> log.error("Error", error)
    );

Output:

Error handled

Prevention

  • Use io.vertx.rxjava3 package for Rxified APIs
  • Use Flowable for streams with backpressure
  • Always provide error handlers in RxJava subscriptions

Common Mistakes with rxjava

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable

These mistakes appear frequently in real-world VERTX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does my Observable not emit any items?

The Vert.x operation may not have started. Ensure the subscription is active and the operation is triggered.

This quick fix is part of the DodaTech Spring & JVM ecosystem series. Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro