Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.
—
Flowable is RxJava's implementation of the Reactive Streams specification, providing backpressured reactive streams for handling sequences of 0 to N items with built-in flow control. It implements the Publisher interface and supports standard backpressure strategies.
Factory methods for creating Flowable instances with backpressure support.
/**
* Creates a Flowable that emits a single item
* @param item the item to emit
* @return Flowable that emits the single item
*/
public static <T> Flowable<T> just(T item);
/**
* Creates a Flowable from an Iterable source
* @param source the Iterable to convert
* @return Flowable that emits items from the Iterable
*/
public static <T> Flowable<T> fromIterable(Iterable<? extends T> source);
/**
* Creates a Flowable from a Publisher (Reactive Streams)
* @param publisher the Publisher to convert
* @return Flowable wrapping the Publisher
*/
public static <T> Flowable<T> fromPublisher(Publisher<? extends T> publisher);
/**
* Creates a Flowable using a custom emitter function
* @param source the FlowableOnSubscribe function
* @param mode the BackpressureStrategy to use
* @return Flowable created from the custom emitter
*/
public static <T> Flowable<T> create(FlowableOnSubscribe<T> source, BackpressureStrategy mode);
/**
* Creates a Flowable that emits sequential integers
* @param start the starting value
* @param count the number of items to emit
* @return Flowable emitting integers from start to start+count-1
*/
public static Flowable<Integer> range(int start, int count);
/**
* Creates a Flowable that emits at specified intervals
* @param period the emission interval
* @param unit the time unit
* @return Flowable emitting sequential longs at intervals
*/
public static Flowable<Long> interval(long period, TimeUnit unit);
/**
* Creates a Flowable from a callable, executed lazily
* @param callable the Callable to execute
* @return Flowable that emits the callable result
*/
public static <T> Flowable<T> fromCallable(Callable<? extends T> callable);Transform emitted items with full backpressure support.
/**
* Transform items using a mapping function
* @param mapper function to transform each item
* @return Flowable with transformed items
*/
public final <R> Flowable<R> map(Function<? super T, ? extends R> mapper);
/**
* Transform items to Publishers and flatten the results
* @param mapper function returning Publisher for each item
* @return Flowable with flattened results
*/
public final <R> Flowable<R> flatMap(Function<? super T, ? extends Publisher<? extends R>> mapper);
/**
* Transform items to Publishers and concatenate them in order
* @param mapper function returning Publisher for each item
* @return Flowable with concatenated results in order
*/
public final <R> Flowable<R> concatMap(Function<? super T, ? extends Publisher<? extends R>> mapper);
/**
* Emit only items that pass a predicate test
* @param predicate function to test each item
* @return Flowable with filtered items
*/
public final Flowable<T> filter(Predicate<? super T> predicate);
/**
* Scan items with an accumulator function
* @param accumulator function to accumulate values
* @return Flowable emitting accumulated values
*/
public final Flowable<T> scan(BiFunction<T, T, T> accumulator);
/**
* Buffer items into lists of specified size
* @param count the size of each buffer
* @return Flowable emitting lists of buffered items
*/
public final Flowable<List<T>> buffer(int count);
/**
* Group items by a key selector function
* @param keySelector function to select grouping key
* @return Flowable emitting GroupedFlowable instances
*/
public final <K> Flowable<GroupedFlowable<K, T>> groupBy(Function<? super T, ? extends K> keySelector);Control flow when downstream can't keep up with upstream.
/**
* Buffer all upstream items when backpressure occurs
* @return Flowable that buffers all items
*/
public final Flowable<T> onBackpressureBuffer();
/**
* Buffer upstream items with specified capacity
* @param capacity maximum buffer size
* @return Flowable that buffers up to capacity items
*/
public final Flowable<T> onBackpressureBuffer(int capacity);
/**
* Drop items when backpressure occurs
* @return Flowable that drops items under backpressure
*/
public final Flowable<T> onBackpressureDrop();
/**
* Drop items with callback when backpressure occurs
* @param onDrop action called for each dropped item
* @return Flowable that drops items under backpressure
*/
public final Flowable<T> onBackpressureDrop(Consumer<? super T> onDrop);
/**
* Keep only the latest item when backpressure occurs
* @return Flowable that keeps only latest items
*/
public final Flowable<T> onBackpressureLatest();
/**
* Reduce request amounts to manage backpressure
* @param reducer function to reduce requested amounts
* @return Flowable with reduced request amounts
*/
public final Flowable<T> onBackpressureReduce(BiFunction<T, T, T> reducer);Control subscription behavior and execution context.
/**
* Subscribe with a simple onNext callback
* @param onNext function called for each emitted item
* @return Disposable for managing the subscription
*/
public final Disposable subscribe(Consumer<? super T> onNext);
/**
* Subscribe with Subscriber interface
* @param subscriber the Subscriber to receive emissions
*/
public final void subscribe(Subscriber<? super T> subscriber);
/**
* Subscribe and block until completion, returning all items
* @return Iterable of all emitted items
*/
public final Iterable<T> blockingIterable();
/**
* Subscribe and return the first item, blocking
* @return the first emitted item
*/
public final T blockingFirst();
/**
* Specify the Scheduler for subscription operations
* @param scheduler the Scheduler to use for subscriptions
* @return Flowable operating on the specified scheduler
*/
public final Flowable<T> subscribeOn(Scheduler scheduler);
/**
* Specify the Scheduler for observation operations
* @param scheduler the Scheduler to use for observations
* @return Flowable observing on the specified scheduler
*/
public final Flowable<T> observeOn(Scheduler scheduler);Convert to parallel processing with multiple streams.
/**
* Convert to parallel processing using available processors
* @return ParallelFlowable for parallel processing
*/
public final ParallelFlowable<T> parallel();
/**
* Convert to parallel processing with specified parallelism
* @param parallelism number of parallel streams
* @return ParallelFlowable for parallel processing
*/
public final ParallelFlowable<T> parallel(int parallelism);
/**
* Convert to parallel processing with custom prefetch
* @param parallelism number of parallel streams
* @param prefetch prefetch amount for each stream
* @return ParallelFlowable for parallel processing
*/
public final ParallelFlowable<T> parallel(int parallelism, int prefetch);Convert between reactive types.
/**
* Convert to Observable (loses backpressure)
* @return Observable equivalent of this Flowable
*/
public final Observable<T> toObservable();
/**
* Convert to Single (takes first item or errors)
* @return Single with the first emitted item
*/
public final Single<T> firstOrError();
/**
* Convert to Single (takes last item or errors)
* @return Single with the last emitted item
*/
public final Single<T> lastOrError();
/**
* Convert to Maybe (takes first item or completes empty)
* @return Maybe with the first emitted item or empty
*/
public final Maybe<T> firstElement();
/**
* Convert to Completable (ignores items, keeps completion/error)
* @return Completable that signals completion or error
*/
public final Completable ignoreElements();/**
* Interface for creating custom Flowable sources
*/
public interface FlowableOnSubscribe<T> {
void subscribe(FlowableEmitter<T> emitter) throws Throwable;
}
/**
* Emitter interface for custom Flowable creation
*/
public interface FlowableEmitter<T> extends Emitter<T> {
void onNext(T value);
void onError(Throwable error);
void onComplete();
long requested();
}
/**
* Subscriber interface following Reactive Streams specification
*/
public interface FlowableSubscriber<T> extends Subscriber<T> {
void onSubscribe(Subscription s);
void onNext(T t);
void onError(Throwable t);
void onComplete();
}
/**
* Backpressure strategy enumeration
*/
public enum BackpressureStrategy {
MISSING, // No backpressure handling
ERROR, // Error when backpressure occurs
BUFFER, // Buffer all items
DROP, // Drop items when backpressured
LATEST // Keep only latest items
}
/**
* Grouped Flowable for groupBy operations
*/
public abstract class GroupedFlowable<K, T> extends Flowable<T> {
public abstract K getKey();
}Usage Examples:
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.BackpressureStrategy;
import io.reactivex.rxjava3.schedulers.Schedulers;
import java.util.concurrent.TimeUnit;
// Basic Flowable with backpressure handling
Flowable.range(1, 1000000)
.onBackpressureBuffer(1000)
.observeOn(Schedulers.computation())
.subscribe(System.out::println);
// Custom Flowable creation with backpressure
Flowable.<String>create(emitter -> {
for (int i = 0; i < 10; i++) {
if (emitter.isCancelled()) break;
emitter.onNext("Item " + i);
}
if (!emitter.isCancelled()) {
emitter.onComplete();
}
}, BackpressureStrategy.BUFFER)
.subscribe(System.out::println);
// Parallel processing
Flowable.range(1, 100)
.parallel()
.runOn(Schedulers.computation())
.map(x -> x * x)
.sequential()
.subscribe(System.out::println);
// Reactive Streams interop
Publisher<String> publisher = Flowable.just("Hello", "World");
Flowable.fromPublisher(publisher)
.subscribe(System.out::println);
// Backpressure strategies
Flowable.interval(1, TimeUnit.MILLISECONDS)
.onBackpressureDrop(dropped -> System.out.println("Dropped: " + dropped))
.observeOn(Schedulers.single())
.subscribe(System.out::println);Install with Tessl CLI
npx tessl i tessl/maven-io-reactivex-rxjava3--rxjava