Kotlin coroutines allow to execute non-blocking code by returning Deferred
value. This is really useful to make non-blocking code while using blocking methods (from a library for example).
Spring 5 allows to use Mono
and Flux
into the framework. The big interest that I see is the ability to serialize instances of these two types and send it back as response when someone call a controller endpoint.
One of the big point of Spring 5 is to have an specific support for Kotlin (router, bean declaration, ...), but I cannot find informations about the possible interactions between Kotlin coroutines and Spring 5 reactive types.
Is there any way to combine the advantages of these features?
Deferred
into Mono
/Flux
?Deferred
as response type of Spring controller methods ?If no, in which cases do coroutine make sense if we have Spring 5 reactive types?
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.
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.
Basically, there are two types of Coroutines: Stackless. Stackful.
The spring-web-reactive module contains the Spring Web Reactive framework that supports the @Controller programming model. It re-defines many of the Spring MVC contracts such as HandlerMapping and HandlerAdapter to be asynchronous and non-blocking and to operate on the reactive HTTP request and response.
As of version 5.2 (still work in progress), Spring Framework provides official support for coroutines. I have written a detailed blog post that explains how Mono
and Flux
types map to suspending functions, Deferred
and Kotlin Flow
types. You can also find more details in the coroutines section of Spring Framework 5.2 reference documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With