VertX 多个 Future 返回一个 Future

发布于 2025-01-10 10:10:42 字数 815 浏览 4 评论 0原文

我正在尝试调用 3 个 future,等待它们完成,处理结果,然后将所有这些也作为 Future 返回。我似乎无法弄清楚如何将 3 个期货“包装”在未来中以使其返回,这样它就不会立即返回。我是 vertx 新手。

public Future<MyObject> getProjectStatus(int projectId) {

      Future future = Future.future(ar -> {
        MyObject object = new Myobject();

        Future<x> f1 = callf1();
        Future<y> f2 = callf2();
        Future<z> f3 = callf3();

        CompositeFuture cf = CompositeFuture.all(f1, f2, f3);
        cf.onComplete(ar2 -> {
          //Check Succeeds
          if(ar2.succeeded()){
            System.out.println("Completed!");
            //DO FURTHER PROCESSING
          } else {
            System.out.println("Failure " + ar2.cause().getMessage());
          }
        }).map((object));

      });

      return future;

    }

I am trying to call 3 futures, wait for them to complete, process the results and then return all of this also as a Future. I cant seem to figure out how to "wrap" the 3 futures in a future to be returned so that it doesnt just return immediately. I am new to vertx.

public Future<MyObject> getProjectStatus(int projectId) {

      Future future = Future.future(ar -> {
        MyObject object = new Myobject();

        Future<x> f1 = callf1();
        Future<y> f2 = callf2();
        Future<z> f3 = callf3();

        CompositeFuture cf = CompositeFuture.all(f1, f2, f3);
        cf.onComplete(ar2 -> {
          //Check Succeeds
          if(ar2.succeeded()){
            System.out.println("Completed!");
            //DO FURTHER PROCESSING
          } else {
            System.out.println("Failure " + ar2.cause().getMessage());
          }
        }).map((object));

      });

      return future;

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法言说的痛 2025-01-17 10:10:42

public Future<MyObject> getProjectStatus(int projectId) {

        MyObject object = new Myobject();

        Future<x> f1 = callf1();
        Future<y> f2 = callf2();
        Future<z> f3 = callf3();

        return CompositeFuture.all(f1, f2, f3).onComplete(ar2 -> {
          //Check Succeeds
          if(ar2.succeeded()){
            System.out.println("Completed!");
            //DO FURTHER PROCESSING
          } else {
            System.out.println("Failure " + ar2.cause().getMessage());
          }
        }).map(object);

    }

或者

public Future<MyObject> getProjectStatus(int projectId) {

      Future future = Future.future(ar -> {
        MyObject object = new Myobject();

        Future<x> f1 = callf1();
        Future<y> f2 = callf2();
        Future<z> f3 = callf3();

        CompositeFuture cf = CompositeFuture.all(f1, f2, f3);
        cf.onComplete(ar2 -> {
          //Check Succeeds
          if(ar2.succeeded()){
            System.out.println("Completed!");
            //DO FURTHER PROCESSING
          } else {
            System.out.println("Failure " + ar2.cause().getMessage());
          }
          ar.complete(object);
        });

      });

      return future;

    }

public Future<MyObject> getProjectStatus(int projectId) {

        MyObject object = new Myobject();

        Future<x> f1 = callf1();
        Future<y> f2 = callf2();
        Future<z> f3 = callf3();

        return CompositeFuture.all(f1, f2, f3).onComplete(ar2 -> {
          //Check Succeeds
          if(ar2.succeeded()){
            System.out.println("Completed!");
            //DO FURTHER PROCESSING
          } else {
            System.out.println("Failure " + ar2.cause().getMessage());
          }
        }).map(object);

    }

or

public Future<MyObject> getProjectStatus(int projectId) {

      Future future = Future.future(ar -> {
        MyObject object = new Myobject();

        Future<x> f1 = callf1();
        Future<y> f2 = callf2();
        Future<z> f3 = callf3();

        CompositeFuture cf = CompositeFuture.all(f1, f2, f3);
        cf.onComplete(ar2 -> {
          //Check Succeeds
          if(ar2.succeeded()){
            System.out.println("Completed!");
            //DO FURTHER PROCESSING
          } else {
            System.out.println("Failure " + ar2.cause().getMessage());
          }
          ar.complete(object);
        });

      });

      return future;

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文