java - vertx 中的并行/并发调用

标签 java asynchronous vert.x

我正在从后端数据库(vertx代码)读取数据,每行都有一个url,我将在该url上对状态字段执行“GET”http请求作为响应,我将其存储回数据库并发送到UI。

CreateRequests 方法发出 http 请求。我想同时执行此操作。

        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 ;

如何有效地进行并发获取调用并组合响应(我将发送到 UI)并将它们存储回数据库

我正在循环所有网址并在循环中发出获取请求。如果有大量数据,我会超时(这是显而易见的)

最佳答案

Vert.x 使用 react 器模式,因此您可以使用该模式而不是多线程和executeBlocking 获得类似的结果。仅当无法支持该模式时才建议在 Vert.x 中使用多线程和阻塞调用(这意味着没有异步方法可以做到这一点)。

也许这个例子可以帮助你

  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);
    });
  }

希望这有帮助

关于java - vertx 中的并行/并发调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56987876/

相关文章:

java - 在java中显示总物理内存大小的错误答案

java - JFrame 重绘();如何使用线程信息更新 JFrame?

javascript - node.js async.eachSeries 过早调用最终回调

c# - 递归 WinRT 异步问题

javascript - 如何在 Javascript 中从 Vert.x 服务器接收 websocket 消息?

java - 检测用户何时使用打印屏幕在 Windows 中拍摄屏幕截图?

java - ArrayIndexOutOfBoundsException : 4096 while reading gif file

ios - iOS 中的 NSOperation - 如何处理循环和嵌套 NSOperation 调用以获取图像

java - 如何在 Vertx Kafka 客户端中使用自定义序列化器?

javascript - 如果我在vertx中使用post方法如何获取json参数?