I have a repository call which will give me Mono .
ex:
private User getUserData (User user)
{
Mono<User> monoUser=userRepository.insert(user);
User user= monoUser.block;
return user;
}
How to achieve this without blocking in spring reactive. I don't want to do monoUser.block to get User object.
After getting userObject i need to convert id to UserId via Mapstruct.Also i want to achieve this without blocking so that i will be using reactive feature.
There are a number of things you can do with a
Mono
instead of callingblock
, things that will not block. One thing you can do is attach aConsumer
to it that gets called when theMono
's action completes successfully. The signature of the method to do that (on theMono
) is:So then you can go on working, and your consumer will get called when the action completes. That call would then initiate whatever actions you want to have performed after the user is added.
You put code in your
Consumer
that writes to the database. If that object needs information, like a handle to the database to write to, you can hand it that information when you construct the object, before passing it in to thedoOnSuccess
call. -