Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.
—
Completable is RxJava's reactive type for operations that only signal completion or error without emitting items. It's ideal for actions like save operations, cleanup tasks, or any fire-and-forget operations where you only care about success or failure.
Factory methods for creating Completable instances.
/**
* Creates a Completable that completes immediately
* @return Completable that signals completion
*/
public static Completable complete();
/**
* Creates a Completable from a Runnable, executed lazily
* @param run the Runnable to execute
* @return Completable that executes the Runnable and completes
*/
public static Completable fromRunnable(Runnable run);
/**
* Creates a Completable from an Action, executed lazily
* @param action the Action to execute
* @return Completable that executes the Action and completes
*/
public static Completable fromAction(Action action);
/**
* Creates a Completable using a custom emitter function
* @param source the CompletableOnSubscribe function
* @return Completable created from the custom emitter
*/
public static Completable create(CompletableOnSubscribe source);
/**
* Creates a Completable that only calls onError
* @param error the error to emit
* @return Completable that emits the error
*/
public static Completable error(Throwable error);
/**
* Creates a Completable that defers creation until subscription
* @param completableSupplier supplier of Completable to defer
* @return Completable that defers to supplied Completable
*/
public static Completable defer(Supplier<? extends CompletableSource> completableSupplier);
/**
* Creates a Completable that never completes or errors
* @return Completable that never signals anything
*/
public static Completable never();
/**
* Creates a Completable that completes after a delay
* @param delay the delay duration
* @param unit the time unit
* @return Completable that completes after the delay
*/
public static Completable timer(long delay, TimeUnit unit);
/**
* Creates a Completable from a Future, ignoring the result
* @param future the Future to convert
* @return Completable that completes when Future completes
*/
public static Completable fromFuture(Future<?> future);Chain Completables and continue with other reactive types.
/**
* Continue with another Completable after this one completes
* @param next Completable to execute after this one
* @return Completable that executes both in sequence
*/
public final Completable andThen(CompletableSource next);
/**
* Continue with a Single after this Completable completes
* @param next Single to execute after this completes
* @return Single that executes after this Completable
*/
public final <T> Single<T> andThen(SingleSource<T> next);
/**
* Continue with an Observable after this Completable completes
* @param next Observable to execute after this completes
* @return Observable that executes after this Completable
*/
public final <T> Observable<T> andThen(ObservableSource<T> next);
/**
* Continue with a Flowable after this Completable completes
* @param next Flowable to execute after this completes
* @return Flowable that executes after this Completable
*/
public final <T> Flowable<T> andThen(Publisher<T> next);
/**
* Continue with a Maybe after this Completable completes
* @param next Maybe to execute after this completes
* @return Maybe that executes after this Completable
*/
public final <T> Maybe<T> andThen(MaybeSource<T> next);Combine multiple Completable sources.
/**
* Merge multiple Completables (complete when all complete)
* @param sources array of Completable sources
* @return Completable that completes when all sources complete
*/
public static Completable merge(CompletableSource... sources);
/**
* Merge multiple Completables from an Iterable
* @param sources Iterable of Completable sources
* @return Completable that completes when all sources complete
*/
public static Completable merge(Iterable<? extends CompletableSource> sources);
/**
* Concatenate multiple Completables in sequence
* @param sources array of Completable sources
* @return Completable that executes all sources in order
*/
public static Completable concat(CompletableSource... sources);
/**
* Merge this Completable with another
* @param other Completable to merge with this one
* @return Completable that completes when both complete
*/
public final Completable mergeWith(CompletableSource other);
/**
* Race multiple Completables (complete with the first one)
* @param sources array of Completable sources
* @return Completable that completes with the first source
*/
public static Completable amb(CompletableSource... sources);
/**
* Race this Completable with another
* @param other Completable to race with this one
* @return Completable that completes with the first one
*/
public final Completable ambWith(CompletableSource other);Control subscription behavior and execution context.
/**
* Subscribe with a simple onComplete callback
* @param onComplete action called when completed
* @return Disposable for managing the subscription
*/
public final Disposable subscribe(Action onComplete);
/**
* Subscribe with onComplete and onError callbacks
* @param onComplete action called when completed
* @param onError action called on error
* @return Disposable for managing the subscription
*/
public final Disposable subscribe(Action onComplete, Consumer<? super Throwable> onError);
/**
* Subscribe with full CompletableObserver interface
* @param observer the CompletableObserver to receive signals
*/
public final void subscribe(CompletableObserver observer);
/**
* Subscribe and block until completion
*/
public final void blockingAwait();
/**
* Subscribe and block until completion with timeout
* @param timeout the timeout duration
* @param unit the time unit
* @return true if completed within timeout, false if timeout occurred
*/
public final boolean blockingAwait(long timeout, TimeUnit unit);
/**
* Specify the Scheduler for subscription operations
* @param scheduler the Scheduler to use for subscriptions
* @return Completable operating on the specified scheduler
*/
public final Completable subscribeOn(Scheduler scheduler);
/**
* Specify the Scheduler for observation operations
* @param scheduler the Scheduler to use for observations
* @return Completable observing on the specified scheduler
*/
public final Completable observeOn(Scheduler scheduler);
/**
* Add a delay before completing
* @param delay the delay duration
* @param unit the time unit
* @return Completable that completes after the delay
*/
public final Completable delay(long delay, TimeUnit unit);Handle errors in the Completable stream.
/**
* Resume with another Completable when an error occurs
* @param resumeSource Completable to switch to on error
* @return Completable that switches to resumeSource on error
*/
public final Completable onErrorResumeNext(CompletableSource resumeSource);
/**
* Complete normally instead of error when an error occurs
* @return Completable that completes on any error
*/
public final Completable onErrorComplete();
/**
* Complete normally for specific errors using a predicate
* @param predicate function to test if error should be converted to completion
* @return Completable that completes for matching errors
*/
public final Completable onErrorComplete(Predicate<? super Throwable> predicate);
/**
* Retry the subscription when an error occurs
* @param times maximum number of retry attempts
* @return Completable that retries up to the specified times
*/
public final Completable retry(long times);
/**
* Perform side-effect action when an error occurs
* @param onError action to perform on error
* @return Completable that performs the action on error
*/
public final Completable doOnError(Consumer<? super Throwable> onError);Perform side-effect actions without modifying the stream.
/**
* Perform an action when Completable completes
* @param onComplete action to perform on completion
* @return Completable that performs the action on completion
*/
public final Completable doOnComplete(Action onComplete);
/**
* Perform an action when subscription occurs
* @param onSubscribe action to perform on subscription
* @return Completable that performs the action on subscription
*/
public final Completable doOnSubscribe(Consumer<? super Disposable> onSubscribe);
/**
* Perform an action when disposal occurs
* @param onDispose action to perform on disposal
* @return Completable that performs the action on disposal
*/
public final Completable doOnDispose(Action onDispose);
/**
* Perform an action on any terminal event (complete or error)
* @param onTerminate action to perform on terminal event
* @return Completable that performs the action on terminal events
*/
public final Completable doOnTerminate(Action onTerminate);
/**
* Perform an action after any terminal event (complete or error)
* @param onAfterTerminate action to perform after terminal event
* @return Completable that performs the action after terminal events
*/
public final Completable doAfterTerminate(Action onAfterTerminate);
/**
* Perform an action on any event (success, error, disposal)
* @param onFinally action to perform on any final event
* @return Completable that performs the action on final events
*/
public final Completable doFinally(Action onFinally);Convert to other reactive types.
/**
* Convert to Observable (completes empty or errors)
* @return Observable that completes empty or errors
*/
public final <T> Observable<T> toObservable();
/**
* Convert to Flowable (completes empty or errors)
* @return Flowable that completes empty or errors
*/
public final <T> Flowable<T> toFlowable();
/**
* Convert to Single with provided value on completion
* @param completionValue value to emit on successful completion
* @return Single that emits the value on completion
*/
public final <T> Single<T> toSingle(Supplier<? extends T> completionValue);
/**
* Convert to Maybe (completes empty or errors)
* @return Maybe that completes empty or errors
*/
public final <T> Maybe<T> toMaybe();/**
* Interface for creating custom Completable sources
*/
public interface CompletableOnSubscribe {
void subscribe(CompletableEmitter emitter) throws Throwable;
}
/**
* Emitter interface for custom Completable creation
*/
public interface CompletableEmitter {
void onComplete();
void onError(Throwable error);
boolean isDisposed();
}
/**
* Observer interface for Completable
*/
public interface CompletableObserver {
void onSubscribe(Disposable d);
void onComplete();
void onError(Throwable e);
}
/**
* Base interface for Completable sources
*/
public interface CompletableSource {
void subscribe(CompletableObserver observer);
}Usage Examples:
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import java.util.concurrent.TimeUnit;
// Basic Completable operations
Completable.fromRunnable(() -> System.out.println("Task executed"))
.subscribe(
() -> System.out.println("Completed"),
error -> System.err.println("Error: " + error)
);
// Chaining operations
Completable.fromAction(() -> System.out.println("First task"))
.andThen(Completable.fromAction(() -> System.out.println("Second task")))
.andThen(Completable.fromAction(() -> System.out.println("Third task")))
.subscribe(() -> System.out.println("All tasks completed"));
// Async execution with scheduling
Completable.fromCallable(() -> {
Thread.sleep(1000);
System.out.println("Background task completed");
return null;
})
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.single())
.subscribe(() -> System.out.println("Notified on main thread"));
// Error handling
Completable.fromAction(() -> {
if (Math.random() > 0.5) {
throw new RuntimeException("Random failure");
}
System.out.println("Task succeeded");
})
.onErrorComplete() // Convert errors to completion
.subscribe(() -> System.out.println("Always completes"));
// Combining multiple Completables
Completable task1 = Completable.fromAction(() -> System.out.println("Task 1"));
Completable task2 = Completable.fromAction(() -> System.out.println("Task 2"));
Completable task3 = Completable.fromAction(() -> System.out.println("Task 3"));
// Run all in parallel
Completable.merge(task1, task2, task3)
.subscribe(() -> System.out.println("All parallel tasks completed"));
// Run in sequence
Completable.concat(task1, task2, task3)
.subscribe(() -> System.out.println("All sequential tasks completed"));
// Timeout and retry
Completable.fromAction(() -> {
Thread.sleep(2000); // Simulate slow operation
System.out.println("Slow task completed");
})
.timeout(1, TimeUnit.SECONDS)
.retry(2)
.onErrorComplete()
.subscribe(() -> System.out.println("Task handled"));
// Continue with other reactive types
Completable.complete()
.andThen(Single.just("Result after completion"))
.subscribe(System.out::println);Install with Tessl CLI
npx tessl i tessl/maven-io-reactivex-rxjava3--rxjava