Mutiny: processing slow and fast items

241 views
Skip to first unread message

Den

unread,
Jun 30, 2021, 7:30:24 AM6/30/21
to Quarkus Development mailing list
I have a Multi with items some of which are processed fast, but some of them are slow. What is the best way to let the fast items not be blocked by the slow ones. e.g. 'a' are fast and 'b' are slow

        Multi.createFrom()
                .items("a", "b","b","b","b","b","a","b","b","b","b", "a")
                .onItem().transformToUniAndConcatenate(s -> s.equals("b") ? slow(s) : Uni.createFrom().item(s))
                .subscribe()
                .asStream()
               .forEach(System.out::println);

    private Uni<?> slow(String s) {
        return Uni.createFrom()
                .item(() -> {
                    try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}
                    return s;
                })
                .runSubscriptionOn(Infrastructure.getDefaultWorkerPool());
    }

clement escoffier

unread,
Jun 30, 2021, 8:05:42 AM6/30/21
to denni...@gmail.com, Quarkus Development mailing list
Hello,

As soon as ordering is not important for you (if that’s the case, you can’t do much) replace `transformToUniAndConcatenate` to `transformToUniAndMerge`. 
It will consume the items concurrently. 

You can configure the level of concurrency using:

.transformToUni(…..).merge(levelOfConcurrency);


Clement

--
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/b6c6eda1-3183-4bbe-8221-79760170276fn%40googlegroups.com.

Den

unread,
Jun 30, 2021, 8:33:50 AM6/30/21
to Quarkus Development mailing list
thanks! 'transformToUniAndMerge' didn't help, but 'transformToUni(…..).merge(levelOfConcurrency)' did!
Reply all
Reply to author
Forward
0 new messages