Mono.fromCallable executing on calling thread by default?

Viewed 8471

I'm attempting to perform an asynchronous operation using Project Reactor's Mono.fromCallable(Callable) and I noticed that, in my test case, it appears to be running on the calling thread. Is it documented somewhere that Mono.fromCallable(Callable) uses the calling thread by default?

I see from the Reactor reference guide best practices I should do something like the following:

Mono blockingWrapper = Mono.fromCallable(() -> { 
    return /* make a remote synchronous call */ 
});
blockingWrapper = blockingWrapper.subscribeOn(Schedulers.boundedElastic()); 

But I don't see anywhere that Schedulers.immediate() is used by default. Why not default to Schedulers.boundedElastic()?

1 Answers

The Threading and Schedulers section of the reactor user guide is relevant to your question (emphasis mine):

Obtaining a Flux or a Mono does not necessarily mean that it runs in a dedicated Thread. Instead, most operators continue working in the Thread on which the previous operator executed. Unless specified, the topmost operator (the source) itself runs on the Thread in which the subscribe() call was made.

The .subscribeOn(...) operator will cause the upstream Callable to be invoked on the specified Scheduler, instead of the Thread on which the subscribe() call was made.

The Callable passed to .fromCallable does not run on another Scheduler by default, because all Callables do not necessarily block. Therefore, in a lot of cases, there's no reason to execute the Callable on another Scheduler.

Related