Spring / Spring Webflux Interview questions
Difference between Mono.fromCallable and Mono.fromRunnable.
In Project Reactor, the primary difference is that Mono.fromCallable creates a Mono that can return a value (or throw an exception), while Mono.fromRunnable creates a Mono that only signals completion without emitting any value (a Mono
| Feature | Mono.fromCallable | Mono.fromRunnable |
|---|---|---|
| Return Value | Emits at most one value (T). | Emits no value (Mono<Void>), only a completion signal. |
| Input Type | Takes a java.util.concurrent.Callable (has a call() method). |
Takes a java.lang.Runnable (has a run() method). |
| Exception Handling | Can throw checked exceptions, which are captured and propagated as onError. |
Cannot throw checked exceptions (only RuntimeExceptions are propagated). |
| Use Case | Wrapping blocking operations that return data (e.g., DB calls). | "Fire and forget" tasks like logging or status updates with no result needed. |
| Laziness | Lazy: Executed only upon subscription. | Lazy: Executed only upon subscription. |
In essence, fromCallable is for tasks that compute a result, and fromRunnable is for tasks that perform an action.
More Related questions...