Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebFlux and Kotlin corountines without ReactiveCrudRepository

I'm working on a project which is using Kotlin, Spring Boot, Hibernate (all on latest version) and I would like to make it reactive with WebFlux framework from Spring.

Problem is that I can't use ReactiveCrudRepository because web app have to use Oracle database and therefore Hibernate. So I couldn't figured out a way how to use non blocking access to Oracle SQL database (only free frameworks).

My question is:

Is it possible to use this like that:

  • Casual CrudRepository which is blocking
  • Service which use corountines and returns everything as Mono

Service example code:

fun getAllLanguages(): Mono<Collection<ProgrammingLanguage>> = async { repository.findAll() }.asMono()

Afterwards there will be controller with:

fun listProgrammingLanguagesReactive() = mono(Unconfined) {
    service.also { logger.info { "requesting list of programming languages" } }
            .getAllLanguages()
            .also { logger.info { "responding with list of programming languages" } }
}

This approach works but I'm not sure whether it will work all the time and whether this is not terrible practice and so on.

like image 899
Lukas Forst Avatar asked May 18 '18 09:05

Lukas Forst


People also ask

Is kotlin coroutine reactive?

Utilities for Reactive Streams. If these adapters are used along with kotlinx-coroutines-reactor in the classpath, then Reactor's Context is properly propagated as coroutine context element ( ReactorContext ) and vice versa.

Is coroutines reactive programming?

This post shows steps to build an app using coroutines and MVVM architecture. In the end, you will have an app that fetches users from web API and display it in a RecyclerView . We will use web API https://jsonplaceholder.typicode.com/users to fetch users.

What is a mono in Kotlin?

It is a specialization of Flux that can emit at most 1 <T> element: a Mono is either valued (complete with element), empty (complete without element) or failed (error).

What are coroutines Kotlin?

A coroutine is a concurrency design pattern that you can use on Android to simplify code that executes asynchronously. Coroutines were added to Kotlin in version 1.3 and are based on established concepts from other languages.


1 Answers

The problem with synchronous blocking API is that there will be a thread blocked for each API call. There is simply no way around it, coroutines or not.

Your approach is as good as any for providing asynchronous adapter to blocking API.

However, please consider following:

You may want to confine async { repository.findAll() } and similar blocking calls to a dedicated fixed ThreadPool/Dispatcher. While coroutines are cheap, remember, that repository.findAll() blocks actual underlying thread and you don't want to exhaust all thread in the CommonPool (which is used by async by default).

This is a useful practice, as you're limiting the number of threads/simultaneous blocking calls. If your fixed pool gets exhausted at some point, then incoming requests will be suspended, without blocking threads, until there are available threads in the pool to process them.

like image 161
Aivean Avatar answered Sep 26 '22 06:09

Aivean