如何在Vertx的Future.compose链中间中断链式调用

huangapple go评论64阅读模式
英文:

How to break from Future.compose chain in-between in Vertx

问题

我正在尝试在 Vertx 中使用 Future.Compose 功能来执行一些连续的代码。但是我卡在了一个地方,我想从链条中间断开。

Future<JsonObject> fut1 = func1(abc, xyz);
fut1.compose(fut1Result -> {
  LOGGER.debug("Success");
  return fut2();
}).compose(fut2Result -> {
  if (fut2Result.containsKey("detail")) {
    // 即使未来已成功,逻辑上却失败了。
    // 所以在这里我想要从 Future.compose 链条中断开?如何中断。
    LOGGER.error("Errorfailed.");
    promise.tryFail(new Throwable(fut2Result.toString()));
  }
  LOGGER.debug("Success");
  return fut3();
}).compose(fut3Result -> {
  LOGGER.debug("Success");
  return fut4();
}).onSuccess(success -> {
  LOGGER.debug("Success");
  promise.complete();
}).onFailure(failure -> {
  LOGGER.error("Error");
  promise.fail(failure.getCause());
});

一种方法是重构我的 fut2() 函数。是否有一种方法可以从 Future.compose 链条中断开呢?
我尝试过 promise.fail()promise.tryFail(),还试图直接抛出一个 Throwable,但都没有奏效。
指向正确方向的提示将非常有帮助。

英文:

I am trying to use Future.Compose functionality in Vertx to execute some sequential code.
but I am stuck in-between where i want to break from chain.

    Future&lt;JsonObject&gt; fut1 = func1(abc, xyz);
    fut1.compose(fut1Result -&gt; {
      LOGGER.debug(&quot;Success&quot;);
      return fut2();
    }).compose(fut2Result -&gt; {
      if (fut2Result.containsKey(&quot;detail&quot;)) {
	    // although future is succedded, but it logically failed.
        // So here i am trying yo break from this Future.compose chain ? how to break.
        LOGGER.error(&quot;Errorfailed. &quot;);
        promise.tryFail(new Throwable(fut2Result.toString()));
      }
      LOGGER.debug(&quot;Success&quot;);
      return fut3();
    }).compose(fut3Result -&gt; {
      LOGGER.debug(&quot;Success&quot;);
      return fut4();
    }).onSuccess(success -&gt; {
      LOGGER.debug(&quot;Success&quot;);
      promise.complete();
    }).onFailure(failure -&gt; {
      LOGGER.error(&quot;Error&quot;);
      promise.fail(failure.getCause());
    });

One way is to refactor my fut2() function. Is there any method to break from Future.compose chain.
I have tried promise.fail() , promise.tryFail() & tried to throw a Throwable directly but nothing worked.

hints in right direction will be very helpful.

答案1

得分: 1

在发生故障时不要返回`fut3`,而是返回一个[失败的`Future`][1]

    Future<JsonObject> fut1 = func1(abc, xyz);
    fut1.compose(fut1Result -> {
      LOGGER.debug("成功");
      return fut2();
    }).compose(fut2Result -> {
      if (fut2Result.containsKey("detail")) {
        LOGGER.error("错误失败。");
        return Future.failedFuture(fut2Result.toString());
      }
      LOGGER.debug("成功");
      return fut3();
    }).compose(fut3Result -> {
      LOGGER.debug("成功");
      return fut4();
    }).onSuccess(success -> {
      LOGGER.debug("成功");
      promise.complete();
    }).onFailure(failure -> {
      LOGGER.error("错误");
      promise.fail(failure.getCause());
    });

  [1]: https://vertx.io/docs/apidocs/io/vertx/core/Future.html#failedFuture-java.lang.String-
英文:

When there is a failure, instead of returning fut3, return a failed Future:

Future&lt;JsonObject&gt; fut1 = func1(abc, xyz);
fut1.compose(fut1Result -&gt; {
  LOGGER.debug(&quot;Success&quot;);
  return fut2();
}).compose(fut2Result -&gt; {
  if (fut2Result.containsKey(&quot;detail&quot;)) {
    LOGGER.error(&quot;Errorfailed. &quot;);
    return Future.failedFuture(fut2Result.toString());
  }
  LOGGER.debug(&quot;Success&quot;);
  return fut3();
}).compose(fut3Result -&gt; {
  LOGGER.debug(&quot;Success&quot;);
  return fut4();
}).onSuccess(success -&gt; {
  LOGGER.debug(&quot;Success&quot;);
  promise.complete();
}).onFailure(failure -&gt; {
  LOGGER.error(&quot;Error&quot;);
  promise.fail(failure.getCause());
});

huangapple
  • 本文由 发表于 2020年9月17日 00:12:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63924080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定