Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.
npx @tessl/cli install tessl/maven-io-reactivex-rxjava3--rxjava@3.0.0RxJava is a comprehensive reactive programming library for the Java Virtual Machine that implements the Reactive Extensions pattern, enabling developers to compose asynchronous and event-based programs using observable sequences. It provides five reactive types (Observable, Flowable, Single, Maybe, Completable), extensive operators for data transformation, flexible scheduling, and built-in backpressure handling.
<dependency>
<groupId>io.reactivex.rxjava3</groupId>
<artifactId>rxjava</artifactId>
<version>3.0.0</version>
</dependency>implementation 'io.reactivex.rxjava3:rxjava:3.0.0'import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.schedulers.Schedulers;
import io.reactivex.rxjava3.disposables.Disposable;For specific functionality:
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.Completable;import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.schedulers.Schedulers;
// Simple Observable sequence
Observable.just("Hello", "World")
.map(String::toUpperCase)
.subscribe(System.out::println);
// Backpressured Flowable with scheduling
Flowable.range(1, 10)
.map(i -> i * i)
.subscribeOn(Schedulers.computation())
.observeOn(Schedulers.single())
.subscribe(System.out::println);
// Single value operations
Single.just("Hello")
.map(String::length)
.subscribe(length -> System.out.println("Length: " + length));
// Completion signaling
Completable.fromRunnable(() -> System.out.println("Task completed"))
.delay(1, TimeUnit.SECONDS)
.subscribe();RxJava is built around several key components:
Non-backpressured reactive streams for handling sequences of 0 to N items with comprehensive operator support.
public abstract class Observable<T> implements ObservableSource<T> {
// Static factory methods
public static <T> Observable<T> just(T item);
public static <T> Observable<T> fromIterable(Iterable<? extends T> source);
public static Observable<Long> interval(long period, TimeUnit unit);
// Instance methods
public final <R> Observable<R> map(Function<? super T, ? extends R> mapper);
public final Observable<T> filter(Predicate<? super T> predicate);
public final Disposable subscribe(Consumer<? super T> onNext);
}Backpressured reactive streams implementing the Reactive Streams specification for handling sequences with flow control.
public abstract class Flowable<T> implements Publisher<T> {
// Static factory methods
public static <T> Flowable<T> just(T item);
public static <T> Flowable<T> fromIterable(Iterable<? extends T> source);
public static Flowable<Long> interval(long period, TimeUnit unit);
// Instance methods
public final <R> Flowable<R> map(Function<? super T, ? extends R> mapper);
public final Flowable<T> onBackpressureBuffer();
public final Disposable subscribe(Consumer<? super T> onNext);
}Reactive type for operations that emit exactly one item or an error, ideal for single-value async operations.
public abstract class Single<T> implements SingleSource<T> {
// Static factory methods
public static <T> Single<T> just(T item);
public static <T> Single<T> fromCallable(Callable<? extends T> callable);
// Instance methods
public final <R> Single<R> map(Function<? super T, ? extends R> mapper);
public final Disposable subscribe(Consumer<? super T> onSuccess);
}Reactive type for operations that emit zero or one item, perfect for optional async operations.
public abstract class Maybe<T> implements MaybeSource<T> {
// Static factory methods
public static <T> Maybe<T> just(T item);
public static <T> Maybe<T> empty();
public static <T> Maybe<T> fromCallable(Callable<? extends T> callable);
// Instance methods
public final <R> Maybe<R> map(Function<? super T, ? extends R> mapper);
public final Disposable subscribe(Consumer<? super T> onSuccess);
}Reactive type for operations that only signal completion or error without emitting items.
public abstract class Completable implements CompletableSource {
// Static factory methods
public static Completable complete();
public static Completable fromRunnable(Runnable run);
public static Completable error(Throwable error);
// Instance methods
public final Completable andThen(CompletableSource next);
public final Disposable subscribe(Action onComplete);
}Scheduler system providing abstraction over different execution contexts and threading models.
public final class Schedulers {
public static Scheduler computation();
public static Scheduler io();
public static Scheduler single();
public static Scheduler trampoline();
public static Scheduler from(Executor executor);
}
public abstract class Scheduler {
public abstract Worker createWorker();
public Disposable scheduleDirect(Runnable run);
public Disposable scheduleDirect(Runnable run, long delay, TimeUnit unit);
}Disposable pattern for managing subscriptions and preventing memory leaks in reactive streams.
public interface Disposable {
void dispose();
boolean isDisposed();
}
public final class CompositeDisposable implements Disposable, DisposableContainer {
public boolean add(Disposable disposable);
public boolean remove(Disposable disposable);
public void clear();
}Hot observables that act as both observer and observable, enabling multicasting of events to multiple subscribers.
public final class PublishSubject<T> extends Subject<T> {
public static <T> PublishSubject<T> create();
public void onNext(T value);
public void onError(Throwable error);
public void onComplete();
}
public final class PublishProcessor<T> extends FlowableProcessor<T> {
public static <T> PublishProcessor<T> create();
public void onNext(T value);
public void onError(Throwable error);
public void onComplete();
}// Core observer interfaces
public interface Observer<T> {
void onSubscribe(Disposable d);
void onNext(T t);
void onError(Throwable e);
void onComplete();
}
public interface SingleObserver<T> {
void onSubscribe(Disposable d);
void onSuccess(T t);
void onError(Throwable e);
}
public interface MaybeObserver<T> {
void onSubscribe(Disposable d);
void onSuccess(T t);
void onError(Throwable e);
void onComplete();
}
public interface CompletableObserver {
void onSubscribe(Disposable d);
void onError(Throwable e);
void onComplete();
}
// Functional interfaces
public interface Consumer<T> {
void accept(T t) throws Throwable;
}
public interface Function<T, R> {
R apply(T t) throws Throwable;
}
public interface Predicate<T> {
boolean test(T t) throws Throwable;
}
public interface Action {
void run() throws Throwable;
}