quarkusvert.xmutinysmallrye

How to run event sequentially in Mutiny


I am using the Mutiny library within the Quarkus framework in Java 11.

I am wonderring which is the best way of running several events sequentially by storing them into a Multi object. I am going to describe my issue in the following java-like pseudocode:

for(P1 p1 : params1){
  for(P2 p2 : params2){
    multiObject.add(functionThatRetunsUni(p1, p2))
  }
}
multiObject.runAll().sequentially();

I need to develop the actions sequentially since the function described in the pseudocode persist entities in a DB, so it maybe the case that two of the calls to the method need to persist the same entity.


Solution

  • I don't know about the best way, but I tend to use a builder object for running several Uni sequentially.

    // I'm just assuming the return type of functionThatReturnsUni is Uni<String> for this brief example
    UniJoin.Builder<String> builder = Uni.join().builder();
    
    for (P1 p1 : params1){
        for (P2 p2 : params2){
            builder.add(functionThatReturnsUni(p1, p2));
        }
    }
    
    return builder.joinAll().andFailFast();