I am looking for solution for converting the Uni<List> to regular List object. Unfortunately am unable to find any solution anywhere or within the mutiny documentation. If anyone can help to point to right direction that will be helpful.
Asked
Active
Viewed 69 times
1 Answers
1
The method you are looking for is await()
. See the documentation here. Here's an example:
// create a uni
Uni<List<Integer>> myUni = Uni.createFrom().item(List.of(1,2,3));
// wait for the uni to produce a list (will fire immediately in this case)
List<Integer> myList = myUni.await().indefinitely();

arov00
- 169
- 1
- 8
-
Thanks, but I think this throws the exception as the thread is blocked when we put it on indefinitely() – Sam Jun 30 '23 at 18:20
-
As per quarkus documentation, you are not allowed to block IO threads. If you wish to block, you must annotate your endpoint with @Blocking to tell quarkus to run it in a separate thread. A Uni is a handle for a potentially still pending asynchronous operation. There is no way to synchronously extract its result without waiting for it to complete. What you can do, however, is add a callback that will be called when the Uni has completed (see ‘onItem’) – arov00 Jun 30 '23 at 19:59
-
Yap I agree. In addition I tried adding this .convert().toCompletableFuture() .get(); to the list of uni and it did retrieved back the non uni list object without any issues. – Sam Jun 30 '23 at 20:17
-
You can do that, as long as you understand that `CompletableFuture.get()` is also a blocking method, so you're not supposed to call it on an I/O thread. Unlike with `await` there's just no exception that tells you that. – arov00 Jun 30 '23 at 23:27