I'm trying to understand what is the right way of implementing post_construct methods in Spring Webflux. On startup of an application I need to read data from the DB (I have an R2dbcRepository configured), and then perform some logic and save result as Bean's fields).
So I have a findAll() method returning Flux. How should it be done?
I tried using .block(), AtomicBoolean flag, none of these worked
First of all, never use
block()method. Use it for tests at most, but there is a better solution out there than StepVerifier. (If you use Kotlin there are await prefixed methods that work like block but not blocking.)If you need data at launch, that says it is bad design to me because if there is no user, what do you do with it? I think it's illogical. What happens when you use query when you need it, add to cache and reuse it when you need it again. In the case of WebFlux, you can prepare a Mono object that uses a query from the database and use
.cache()end of chain. So Spring Bean can contain this Mono object that will be run when you subscribe.Ofc below example,
repo.findwill never call if function of Service won't run.https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html#cache--