简体   繁体   中英

Parallel/concurrent calls in vertx

I am reading data from database in backend(vertx code)and each row has a url on which I will do a "GET" http request for the status field in response which i am storing back into database and sending to UI.

CreateRequests method makes http requests.I want to do this concurrently.

        List<Future> toComplete=new ArrayList<>();
        Vertx vertx = Vertx.vertx();int i;
        for(i=0;i<requestsListInDept.size();i++)
        {
            String reqtype = requestsListInDept.getString(i);
            JsonObject requestProperties = dataReader.getRequestProperties(dept,reqtype);
           toComplete.add(Future.future());
           int currrent=i;
            vertx.executeBlocking(future->{
                System.out.println("calling for ");
                String individualResponse  = accReq.createRequests(requestContext,reqtype,empid,requestProperties);
               toComplete.get(currrent).complete(individualResponse);
                future.complete(individualResponse);
            },false,res->{
                JsonArray obj=new JsonArray( (res.result()).toString() );
                for(int index=0;index<obj.size();index++)
                {
                    JsonObject requestResponse=obj.getJsonObject(index);
                    response.add(requestResponse);

                }
                toComplete.get(currrent).complete(res.result());
            });
        }
      CompositeFuture.all(toComplete).setHandler(e -> {
                String collect = e.result()
                        .list()
                        .stream()
                        .map(Object::toString)
                        .collect(Collectors.joining(" ------- "));
                System.out.println(collect);
            });

return ;

How can I efficiently make concurrent get calls and combine responses(which i will be sending to UI) and store them back to DB

I am looping all the urls and making get requests in loop.If there is large data I am getting Timeout(That's obvious)

Vert.x use the reactor pattern, so you could achieve a similar result using the pattern rather than multithreading and executeBlocking. Using multithreading and blocking calls in Vert.x is recommended only when there is no way to support the pattern (meaning, there is no async way of doing it).

Maybe this example can help you

  public void doCall() {
    List<Future> toComplete = new ArrayList<>();
    WebClientOptions options = new WebClientOptions().setSsl(true);
    WebClient webClient = WebClient.create(Vertx.vertx(), options);
    IntStream.range(0, 15).forEach(counter -> {
      toComplete.add(Future.future());
      int current = counter;
      webClient.get(443, "google.com", "/")
               .send(httpResponseAsyncResult -> {
                 String googleResult = httpResponseAsyncResult.result().bodyAsString();
                 toComplete.get(current).complete(googleResult);
               });
      System.out.println("Calling google: times " + ++counter);
    });
    CompositeFuture.all(toComplete).setHandler(e -> {
      String collect = e.result()
                        .list()
                        .stream()
                        .map(Object::toString)
                        .collect(Collectors.joining(" ------- "));
      System.out.println(collect);
    });
  }

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM