I'm starting SpringBoot WebFlux project, but what if I use usual ( non-reactive) jdbc driver? Will the whole application stop to be reactive?
CodePudding user response:
Yes.
You will either lead to blocking due to threadpool exhaustion, or if its unbounded, resource exhaustion.
In addition, there is no such thing as backpressure in non reactive drivers a key tenet of reactive programming.
CodePudding user response:
No.
C.1. How Do I Wrap a Synchronous, Blocking Call?
It is often the case that a source of information is synchronous and blocking. To deal with such sources in your Reactor applications, apply the following pattern:
Mono blockingWrapper = Mono.fromCallable(() -> {
return /* make a remote synchronous call */
});
blockingWrapper = blockingWrapper.subscribeOn(Schedulers.boundedElastic());
- Create a new Mono by using fromCallable.
- Return the asynchronous, blocking resource.
- Ensure each subscription happens on a dedicated single-threaded worker from Schedulers.boundedElastic().
You should use a Mono, because the source returns one value. You should use Schedulers.boundedElastic, because it creates a dedicated thread to wait for the blocking resource without impacting other non-blocking processing, while also ensuring that there is a limit to the amount of threads that can be created, and blocking tasks that can be enqueued and deferred during a spike.
Note that subscribeOn does not subscribe to the Mono. It specifies what kind of Scheduler to use when a subscribe call happens.