0
# RxJava
1
2
RxJava 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.
3
4
## Package Information
5
6
- **Package Name**: rxjava
7
- **Package Type**: maven
8
- **Group ID**: io.reactivex.rxjava3
9
- **Language**: Java
10
- **Installation**:
11
```xml
12
<dependency>
13
<groupId>io.reactivex.rxjava3</groupId>
14
<artifactId>rxjava</artifactId>
15
<version>3.0.0</version>
16
</dependency>
17
```
18
- **Gradle**: `implementation 'io.reactivex.rxjava3:rxjava:3.0.0'`
19
20
## Core Imports
21
22
```java
23
import io.reactivex.rxjava3.core.*;
24
import io.reactivex.rxjava3.schedulers.Schedulers;
25
import io.reactivex.rxjava3.disposables.Disposable;
26
```
27
28
For specific functionality:
29
30
```java
31
import io.reactivex.rxjava3.core.Observable;
32
import io.reactivex.rxjava3.core.Flowable;
33
import io.reactivex.rxjava3.core.Single;
34
import io.reactivex.rxjava3.core.Maybe;
35
import io.reactivex.rxjava3.core.Completable;
36
```
37
38
## Basic Usage
39
40
```java
41
import io.reactivex.rxjava3.core.*;
42
import io.reactivex.rxjava3.schedulers.Schedulers;
43
44
// Simple Observable sequence
45
Observable.just("Hello", "World")
46
.map(String::toUpperCase)
47
.subscribe(System.out::println);
48
49
// Backpressured Flowable with scheduling
50
Flowable.range(1, 10)
51
.map(i -> i * i)
52
.subscribeOn(Schedulers.computation())
53
.observeOn(Schedulers.single())
54
.subscribe(System.out::println);
55
56
// Single value operations
57
Single.just("Hello")
58
.map(String::length)
59
.subscribe(length -> System.out.println("Length: " + length));
60
61
// Completion signaling
62
Completable.fromRunnable(() -> System.out.println("Task completed"))
63
.delay(1, TimeUnit.SECONDS)
64
.subscribe();
65
```
66
67
## Architecture
68
69
RxJava is built around several key components:
70
71
- **Reactive Types**: Five core types (Observable, Flowable, Single, Maybe, Completable) for different data emission patterns
72
- **Observer Pattern**: Publishers emit data to subscribers following the reactive streams protocol
73
- **Functional Operators**: 200+ operators for transforming, filtering, combining, and manipulating data streams
74
- **Scheduler System**: Abstraction over concurrency with built-in schedulers for different execution contexts
75
- **Backpressure Handling**: Flow control mechanisms in Flowable to handle fast producers and slow consumers
76
- **Resource Management**: Disposable pattern for subscription lifecycle and memory management
77
78
## Capabilities
79
80
### Observable Streams
81
82
Non-backpressured reactive streams for handling sequences of 0 to N items with comprehensive operator support.
83
84
```java { .api }
85
public abstract class Observable<T> implements ObservableSource<T> {
86
// Static factory methods
87
public static <T> Observable<T> just(T item);
88
public static <T> Observable<T> fromIterable(Iterable<? extends T> source);
89
public static Observable<Long> interval(long period, TimeUnit unit);
90
91
// Instance methods
92
public final <R> Observable<R> map(Function<? super T, ? extends R> mapper);
93
public final Observable<T> filter(Predicate<? super T> predicate);
94
public final Disposable subscribe(Consumer<? super T> onNext);
95
}
96
```
97
98
[Observable API](./observable.md)
99
100
### Flowable Streams
101
102
Backpressured reactive streams implementing the Reactive Streams specification for handling sequences with flow control.
103
104
```java { .api }
105
public abstract class Flowable<T> implements Publisher<T> {
106
// Static factory methods
107
public static <T> Flowable<T> just(T item);
108
public static <T> Flowable<T> fromIterable(Iterable<? extends T> source);
109
public static Flowable<Long> interval(long period, TimeUnit unit);
110
111
// Instance methods
112
public final <R> Flowable<R> map(Function<? super T, ? extends R> mapper);
113
public final Flowable<T> onBackpressureBuffer();
114
public final Disposable subscribe(Consumer<? super T> onNext);
115
}
116
```
117
118
[Flowable API](./flowable.md)
119
120
### Single Values
121
122
Reactive type for operations that emit exactly one item or an error, ideal for single-value async operations.
123
124
```java { .api }
125
public abstract class Single<T> implements SingleSource<T> {
126
// Static factory methods
127
public static <T> Single<T> just(T item);
128
public static <T> Single<T> fromCallable(Callable<? extends T> callable);
129
130
// Instance methods
131
public final <R> Single<R> map(Function<? super T, ? extends R> mapper);
132
public final Disposable subscribe(Consumer<? super T> onSuccess);
133
}
134
```
135
136
[Single API](./single.md)
137
138
### Optional Values
139
140
Reactive type for operations that emit zero or one item, perfect for optional async operations.
141
142
```java { .api }
143
public abstract class Maybe<T> implements MaybeSource<T> {
144
// Static factory methods
145
public static <T> Maybe<T> just(T item);
146
public static <T> Maybe<T> empty();
147
public static <T> Maybe<T> fromCallable(Callable<? extends T> callable);
148
149
// Instance methods
150
public final <R> Maybe<R> map(Function<? super T, ? extends R> mapper);
151
public final Disposable subscribe(Consumer<? super T> onSuccess);
152
}
153
```
154
155
[Maybe API](./maybe.md)
156
157
### Completion Signaling
158
159
Reactive type for operations that only signal completion or error without emitting items.
160
161
```java { .api }
162
public abstract class Completable implements CompletableSource {
163
// Static factory methods
164
public static Completable complete();
165
public static Completable fromRunnable(Runnable run);
166
public static Completable error(Throwable error);
167
168
// Instance methods
169
public final Completable andThen(CompletableSource next);
170
public final Disposable subscribe(Action onComplete);
171
}
172
```
173
174
[Completable API](./completable.md)
175
176
### Scheduling and Concurrency
177
178
Scheduler system providing abstraction over different execution contexts and threading models.
179
180
```java { .api }
181
public final class Schedulers {
182
public static Scheduler computation();
183
public static Scheduler io();
184
public static Scheduler single();
185
public static Scheduler trampoline();
186
public static Scheduler from(Executor executor);
187
}
188
189
public abstract class Scheduler {
190
public abstract Worker createWorker();
191
public Disposable scheduleDirect(Runnable run);
192
public Disposable scheduleDirect(Runnable run, long delay, TimeUnit unit);
193
}
194
```
195
196
[Schedulers and Concurrency](./schedulers.md)
197
198
### Resource Management
199
200
Disposable pattern for managing subscriptions and preventing memory leaks in reactive streams.
201
202
```java { .api }
203
public interface Disposable {
204
void dispose();
205
boolean isDisposed();
206
}
207
208
public final class CompositeDisposable implements Disposable, DisposableContainer {
209
public boolean add(Disposable disposable);
210
public boolean remove(Disposable disposable);
211
public void clear();
212
}
213
```
214
215
[Resource Management](./disposables.md)
216
217
### Subjects and Processors
218
219
Hot observables that act as both observer and observable, enabling multicasting of events to multiple subscribers.
220
221
```java { .api }
222
public final class PublishSubject<T> extends Subject<T> {
223
public static <T> PublishSubject<T> create();
224
public void onNext(T value);
225
public void onError(Throwable error);
226
public void onComplete();
227
}
228
229
public final class PublishProcessor<T> extends FlowableProcessor<T> {
230
public static <T> PublishProcessor<T> create();
231
public void onNext(T value);
232
public void onError(Throwable error);
233
public void onComplete();
234
}
235
```
236
237
[Subjects and Processors](./subjects.md)
238
239
## Types
240
241
```java { .api }
242
// Core observer interfaces
243
public interface Observer<T> {
244
void onSubscribe(Disposable d);
245
void onNext(T t);
246
void onError(Throwable e);
247
void onComplete();
248
}
249
250
public interface SingleObserver<T> {
251
void onSubscribe(Disposable d);
252
void onSuccess(T t);
253
void onError(Throwable e);
254
}
255
256
public interface MaybeObserver<T> {
257
void onSubscribe(Disposable d);
258
void onSuccess(T t);
259
void onError(Throwable e);
260
void onComplete();
261
}
262
263
public interface CompletableObserver {
264
void onSubscribe(Disposable d);
265
void onError(Throwable e);
266
void onComplete();
267
}
268
269
// Functional interfaces
270
public interface Consumer<T> {
271
void accept(T t) throws Throwable;
272
}
273
274
public interface Function<T, R> {
275
R apply(T t) throws Throwable;
276
}
277
278
public interface Predicate<T> {
279
boolean test(T t) throws Throwable;
280
}
281
282
public interface Action {
283
void run() throws Throwable;
284
}
285
```