Luisao
Luisao

Reputation: 505

Quarkus asynchronous and paralell requests with vert.x

I have a Quarkus App that reads a file and sends requests (to another Quarkus microservice). At the moment, I send one request for each line of the file. But the process takes so long because I have thousand of lines.

I've read about vert.x and I've got an asynchronous request to the API, thanks to this link:

https://quarkus.io/blog/mutiny-concurrent-uni/

But my problem is that I have to save the response of the requests to an Array and at the end, save the file. So the problem is that the process finishes and the result of the file is empty (because the request are async and they are invoking in that moment)

This is my code to make async requests:

 this.client = WebClient.create(vertx,
              new WebClientOptions().setSsl(true).setTrustAll(true));
            
 client.postAbs("http://localhost:8082/myApi")
      .sendJson(myDto)
      .subscribe().with(a->this.processRecord(a.bodyAsString());        

I know that I have to wait until the async process with the ArrayList with all records is finished and then, when everything is complete save the file. And I've read about CompletableFuture and I think it may works but I don't know where I have to wait and how.

PS: Probably there could be another way to do that. I'd like to know anyway.

Thanks.

Upvotes: 0

Views: 1831

Answers (1)

Clement
Clement

Reputation: 3222

Instead of subscribe, use await().atMost(Duration.ofSeconds(...)), or await().indefinitely() (not recommended as it may block forever). Both will return the HTTPResponse.

Now, without much context, it's hard to understand how your method is used. In general, we don't use await(), because the call is also asynchronous and so expect to receive a Uni.

Upvotes: 1

Related Questions