Connect MyRequestService to Reactive REST GET endpoint with Quarkus/Mutiny through callback method
Asked Answered
R

1

3

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 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.

Ribbonfish answered 23/3, 2021 at 10:43 Comment(0)
R
2

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). ๐Ÿ‘ฎ๐Ÿปโ€โ™‚๏ธ

Ribbonfish answered 23/3, 2021 at 10:43 Comment(2)
For a blocking example check; #66945035 โ€“ Ribbonfish
For a CompletionStage / CompletableFuture API version check; #67039037 โ€“ Ribbonfish

© 2022 - 2024 โ€” McMap. All rights reserved.