Quarkus: execute parallel unis

Viewed 1098

In a quarkus / kotlin application, I want to start multiple database requests concurrently. I am new at quarkys and I am not sure if I am doing things right:

    val uni1 = Uni.createFrom().item(repo1).onItem().apply { it.request() }
    val uni2 = Uni.createFrom().item(repo2).onItem().apply { it.request() }

    return Uni.combine().all()
      .unis(uni1, uni2)
      .asTuple()
      .onItem()
      .apply { tuple ->
        Result(tuple.item1, tuple.item2) }
      .await()
      .indefinitely()

Will the request() really be made in parallel? Is it the right way to do it in quarkus?

1 Answers

Yes, your code is right.

Uni.combine().all() runs all the passed Unis concurrently. You will get the tuple (containing the individual results) when all the Unis have completed (emitted a result).

From your code, you may remove the tuple step and use combineWith instead.

Finally, note that the await().indefinitely() blocks the caller thread, forever if one of the Uni does not complete (for whatever reason). I strongly recommend using await().atMost(...)

Related