# 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. ## Install Save in your project root: # RxJava — Reactive Extensions for the JVM ## Quick Use ```xml io.reactivex.rxjava3 rxjava 3.1.9 ``` ```java Observable.just("Hello", "RxJava") .map(String::toUpperCase) .subscribe(System.out::println); ``` ## 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 - https://github.com/ReactiveX/RxJava - https://reactivex.io/ --- Source: https://tokrepo.com/en/workflows/08e1a5b4-4409-11f1-9bc6-00163e2b0d79 Author: AI Open Source