Cette page est affichée en anglais. Une traduction française est en cours.
ConfigsApr 29, 2026·3 min de lecture

RxJava — Reactive Extensions for the JVM

RxJava is a library for composing asynchronous and event-based programs using observable sequences on the JVM, enabling clean handling of complex data flows and concurrency.

Introduction

RxJava implements the ReactiveX specification for the JVM, bringing the observer pattern, iterator pattern, and functional programming together into a single API. It simplifies asynchronous programming by treating everything — from UI events to network calls — as composable streams of data.

What RxJava Does

  • Models asynchronous data flows as Observable, Flowable, Single, Maybe, and Completable types
  • Provides a rich operator library (map, filter, flatMap, zip, merge, and hundreds more) for stream transformation
  • Manages threading and concurrency through pluggable Schedulers
  • Supports backpressure handling via Flowable for producers faster than consumers
  • Integrates with Retrofit, Room, Spring, and most JVM frameworks

Architecture Overview

RxJava's core is the reactive type hierarchy. Observables push items to Observers through a subscription. Operators are implemented as intermediate Observables that intercept and transform items. Schedulers abstract thread pools, letting developers declaratively switch execution contexts with subscribeOn and observeOn. Disposables allow cancellation and resource cleanup.

Self-Hosting & Configuration

  • Add the rxjava3 dependency via Maven Central or Gradle
  • No external service required — it is a pure library dependency
  • Configure custom Schedulers for specific thread pool sizes via Schedulers.from(Executor)
  • Set global error handling with RxJavaPlugins.setErrorHandler for undeliverable exceptions
  • Use RxJava BOM for consistent version alignment across RxJava, RxAndroid, and RxKotlin

Key Features

  • Over 400 operators for filtering, combining, transforming, and error handling
  • Backpressure strategies (BUFFER, DROP, LATEST, ERROR) for flow control
  • Built-in testing support via TestObserver and TestScheduler for deterministic tests
  • Lazy evaluation: streams do nothing until subscribed
  • Interoperability with Reactive Streams, CompletableFuture, and Kotlin coroutines via adapters

Comparison with Similar Tools

  • Project Reactor — similar API with Mono/Flux; preferred in Spring WebFlux, while RxJava is more common in Android
  • Kotlin Coroutines + Flow — language-level async; simpler syntax but fewer operators out of the box
  • CompletableFuture — standard JDK async; lacks composable stream semantics and backpressure
  • Akka Streams — actor-based streaming with Alpakka connectors; heavier runtime footprint
  • Mutiny — Quarkus reactive library; simpler API but smaller ecosystem

FAQ

Q: Is RxJava still relevant with Kotlin Coroutines available? A: Yes. RxJava excels at complex event stream composition with its vast operator set. Many Android and backend codebases rely on it, and it interops cleanly with coroutines via rxjava3-coroutines.

Q: What is the difference between Observable and Flowable? A: Observable does not support backpressure and is suited for small, finite streams or UI events. Flowable supports backpressure strategies and is designed for high-volume or unbounded data sources.

Q: How do I handle errors in RxJava? A: Use operators like onErrorReturn, onErrorResumeNext, or retry to recover from errors within a stream. Unhandled errors propagate to the global error handler.

Q: Can RxJava be used on Android? A: Yes. RxAndroid provides Android-specific Schedulers (mainThread), and RxJava is widely used for network calls, database queries, and UI event handling on Android.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires