【问题标题】:Vertx java - sequencing code using composeVertx java - 使用 compose 对代码进行排序
【发布时间】:2021-02-25 03:53:32
【问题描述】:

我有一个逻辑,我需要将数据保存在两个表中(一对多)。我在我的 Java 中创建了两个方法,我正在尝试使用 Vertx Future 和 compose 来按顺序实现逻辑。但是我已经完成了一半,不明白当第一个未来完成时如何实现组合。我的意思是代码为第一个未来 anAsyncAction_1(materialToAdd); 运行,记录保存在数据库中,但现在我如何在 compose 中调用我的第二个方法

public Future<Void> anAsyncAction_2(final SendToCompanyFromSupplier rawmaterialToAdd, Integer id)
{
    //code to get the id from the first future and save data in the table
}

下面是我的代码

public Future<Void> adddetails(final Supplier materialToAdd)
    {
        final Promise<Void> added = Promise.promise();
        
        Future<Integer> fut1 = anAsyncAction_1(materialToAdd);
        
        LOG.debug(" future.result()  "+fut1.result());
        
        fut1.compose((outcome) -> {
            LOG.debug(" future.result()  "+outcome);
             
        });

        CompositeFuture.all(fut1, fut2).onComplete(ar ->
        {
            System.out.println("BOTH OPERATION COMPLETED!! 1 " + ar.succeeded());
            try
            {
                System.out.println("BOTH OPERATION COMPLETED!! 2 " + ar.result().list());
                added.complete();
                System.out.println("BOTH OPERATION COMPLETED!!");
            } catch (Exception e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        });

        return added.future();

    }

【问题讨论】:

    标签: vert.x vertx-verticle vertx-httpclient


    【解决方案1】:

    如果您只想编写两个期货,可以在不使用CompositeFuturePromise 的情况下简化实现。

    示例代码:

    public Future<Void> adddetails(final Object materialToAdd) {
        Object rawMaterialToAdd = new Object();
    
        return anAsyncAction_1(materialToAdd).compose(i -> anAsyncAction_2(rawMaterialToAdd, i))
                                             .onComplete(ar -> {
                                               if (ar.succeeded()) {
                                                 System.out.println("Both operations completed");
                                               } else {
                                                 ar.cause()
                                                   .printStackTrace();
                                               }
                                             });
    
    
      }
    
      private Future<Integer> anAsyncAction_1(Object materialToAdd) {
        Promise<Integer> promise = Promise.promise();
        Vertx.currentContext()
             .runOnContext(v -> promise.complete(1)); //Async Call. Replace with async DB call 1
        return promise.future();
      }
    
      public Future<Void> anAsyncAction_2(final Object rawmaterialToAdd, Integer id) {
        Promise<Void> promise = Promise.promise();
        Vertx.currentContext()
             .runOnContext(v -> {
               System.out.println("Id received:" + id);
               promise.complete();
             }); //Async Call. Replace it with the async DB call 2
        return promise.future();
      }
    

    下面是顺序

    1. anAsyncAction_1 内的异步 DB 调用将完成。返回的 id 将用于完成承诺。承诺将反过来完成第一个未来。
    2. 未来将触发带有 id 的anAsyncAction_2。完成后的异步数据库调用 2 将完成第二个未来。发布第二个未来完成后,onComplete 处理程序将被执行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 2019-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多