2

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?

Rolintocour
  • 2,934
  • 4
  • 32
  • 63

1 Answers1

0

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

Clement
  • 2,817
  • 1
  • 12
  • 11
  • Have you tested this? In my experience `Uni` still executes everything in one thread. It returns immediately, but the actual execution is in series. – Sean Sep 22 '20 at 15:10
  • Depends on how the requests are executed, but yes it runs them concurrently. See https://gist.github.com/cescoffier/1ed68bef12b798529e10350f77686e9a as an example. If request() are synchronous then yes, of course, it won't work, they need to be asynchronous to be run concurrently. – Clement Sep 23 '20 at 13:22