0

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.

Shankha057
  • 1,296
  • 1
  • 20
  • 38
  • 1
    Transform your Publisher into an Observable/Mono, and use `.map(value -> decideWhatToReturnBaseOnValue(value))`? Which reactive library are you using? – JB Nizet Jun 14 '19 at 19:49
  • @JBNizet Nice, I can now get back to using Singles and Maybes. Thanks, just what I needed. – Shankha057 Jun 14 '19 at 20:06

0 Answers0