Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin coroutines and Spring Framework 5 reactive types

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?

  • converting Deferred into Mono/Flux?
  • having a Deferred as response type of Spring controller methods ?

If no, in which cases do coroutine make sense if we have Spring 5 reactive types?

like image 659
Bastien7 Avatar asked Oct 11 '17 13:10

Bastien7


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.

How many types of coroutines are there?

Basically, there are two types of Coroutines: Stackless. Stackful.

What is spring reactive framework?

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.


1 Answers

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.

like image 172
Sébastien Deleuze Avatar answered Oct 09 '22 21:10

Sébastien Deleuze