vendredi 14 juin 2019

Proper way to return data from a reactive DAO to the controller while using MongoDB reactive driver for Java

I am using MongoDB reactive driver for Java. I have a DAO where I query the DB using the find().first() function chain which return a Publisher<Document>. I have to call subscribe() (or else the query will not actually execute) on this Publisher instance where I do some comparisons in the onNext(Document) and onError(Throwable) callbacks. As per my logic, I have to return some values from my function but both of these callback methods are voids.

I can create an ugly looking logic whereby I block inside the callback and set my return value as a value of AtomicReference, which I declare outside the callbacks, and then I return the value in the AtomicReference. But in doing so, I'm blocking and blocking is what I do not want to do.

How can we query for values, compare the result and then return our own kind of result based on the comparison (all from the DAO to the Controller) in a reactive way? If a design pattern that exists to do such type of operation, what is it?

I am using Micronaut as my web framework.

Aucun commentaire:

Enregistrer un commentaire