How to reactive with quarkus-mutiny

422 views
Skip to first unread message

Daniel Borella

unread,
Oct 13, 2021, 2:28:25 AM10/13/21
to Quarkus Development mailing list
Hello Quarkus community,

I'm struggling with my first attempts to create a reactive service (preferably with Mutiny).
The use case is the following:
I get a list of items from an async api (Promise SearchResult),
then I complete (transform) each item with other information (new type),
before I send the item to storage.

This pretty much sound like a nice stream. So, my first idea was wrapping things:

public Multi<Item> getResult(){

ItemResult result = async.call().get() // get of the Promise

return Multi.createFrom().iterable(result.getItems());
}

And

public Uni<CustomType> buildCutomType(Item item){

//Do stuff to build new obj

return Uni.createFrom().item(obj);

}


The problem I have is to find the correct sequence of functions like:

adapter.getResult() //return Multi<Item>
                .onItem()
                .transform(item -> buildCutomType(item)) //as this returns Uni<CustomType> do I need to "transformToUni"?
                .onItem()
                .call(obj -> repo.store(obj)); //necessary to wait at the end for completion?

Maybe, the approach to wrap things like above is already wrong, and it's better to return a Uni<ItemResult> at the beginning, but then again I need to stream the single items out of it?? Hmmm :-/

I checked the "reactive" topics in the guides and the Mutiny docs, but there is a lack of complex examples.

A little guidance on that topic would be very much appreciated 

Regards,
Daniel

Julien Ponge

unread,
Oct 14, 2021, 12:09:25 PM10/14/21
to Quarkus Development mailing list, borella...@gmail.com
Hi Daniel,

It's a bit hard to say with just a few snippets, but note that:
  • transform models synchronous transformations (e.g., extracting a JSON entry from a payload)
  • transformToUni models asynchronous transformations (hence the mapping function returns a Uni), think of indeed making a call to another service for each item
  • if buildCustomType does not do I/O it might just be a transform
  • looks like repo.store(obj) is a candidate for another transformToUni
Hope it helps.


From: quark...@googlegroups.com <quark...@googlegroups.com> on behalf of Daniel Borella <borella...@gmail.com>
Sent: Wednesday, October 13, 2021 8:28 AM
To: Quarkus Development mailing list <quark...@googlegroups.com>
Subject: [quarkus-dev] How to reactive with quarkus-mutiny
 
--
You received this message because you are subscribed to the Google Groups "Quarkus Development mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to quarkus-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/quarkus-dev/347b6379-c3f1-4280-97d5-819bfec1a763n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages