Connect MyRequestService to Reactive REST GET endpoint with Quarkus/Mutiny

Last week i had my first ๐Ÿ‘Šโœจ๐Ÿ’ช rounds with the Mutiny library because i needed a Reactive REST endpoint in my Quarkus project. This was not as obvious as it sounds so i thought i’ll share my new fresh insights about the Mutiny library in Quarkus;

Quarkus documentation specifies the Mutiny library as the preferred library for Reactive use-cases;

Going forward, Mutiny will be the preferred library within Quarkus for all things reactive.

For example;

enter image description here

what stood out is that most Mutiny examples are using a new String as an example. So, my question remained;

how do i connect MyRequestService with Mutiny in Quarkus

This would be something as:

Uni<MyRequestService> lMyRequestServiceUni = Uni.createFrom().item( ... ) ...

MyRequestService already uses a callback structure, so I tried a callback method towards Mutiny.

Answer

Mutiny uses an Emitter when integrating with callback-based APIs;

You can create a Uni using an emitter. This approach is useful when integrating callback-based APIs

https://smallrye.io/smallrye-mutiny/getting-started/creating-unis

So MyRequestService, or an underlying callback object, needs to implement an UniEmitter Consumer. But first, to become Reactive, my old blocking REST endpoint needs to return Uni<MyJsonResult> instead of MyJsonResult;

Uni instead of MyJsonResult

The ServiceResource just forwards the call to the Service.

MyRequestService creates a MyJsonResultConsumer and delivers this to the Mutiny emitter. The resulting lMyJsonResultUni is returned to the ServiceResource

MyRequestService creates a MyJsonResultConsumer and delivers this to the Mutiny Emitter. The resulting Uni<MyJsonResult> is returned to the ServiceResource.

Finally MyJsonResultConsumer is the actual callback object; method ready() calls complete() on the UniEmitter concluding the callback towards Mutiny

Finally MyJsonResultConsumer is here the actual callback object; method ready() calls complete() on the UniEmitter concluding the callback towards Mutiny.

Remember that Mutiny needs to provide the UniEmitter with a call(back) to accept(), so you should check for null pointers (or use a Semaphore oid). ๐Ÿ‘ฎ๐Ÿปโ€โ™‚๏ธ

Have fun with the Mutiny library in Quarkus and feel free to question, update and/or improve (i already spotted a ๐Ÿ in the code) ๐Ÿค“

Leave a Reply

Your email address will not be published. Required fields are marked *