We have an application which was written by a different team with Webflux. As I can see there are some refinement points in the code, but my other team members doesn’t agree on the refactor.
It is said that blocking functions should be called with Mono.fromCallable
, now in the codebase Mono.just
is used for calling blocking codes, like
.flatMap(something -> Mono.just(repository.get(id)))
After refactoring the code would be like this:
// the beginning of the chain doesn't matter
.flatMap(k ->
Mono.fromCallable(() -> repository.get(id)).subscribeOn(Schedulers.boundedElastic())
)
// here we manipulate the p, that part is missing
//
.flatMap(p ->
Mono.fromCallable(() -> repository.save(p)).subscribeOn(Schedulers.boundedElastic())
.thenReturn(something)
)
.flatMap(something ->
Mono.fromCallable(() -> repository.get(id)).subscribeOn(Schedulers.boundedElastic())
)
.flatMap(p -> doSomething);
All the repository calls are blocking calls.
In the chain we do the following
-
we get an object by an id
-
do some manipulation on the object
-
save the object
-
get the object from the database again.
Does the object be persisted before we get it from the database or we could get outdated data?
My understanding is that the chain goes further only when the operation finishes, but I like to verify this.