英文:
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<JsonObject> fut1 = func1(abc, xyz);
fut1.compose(fut1Result -> {
LOGGER.debug("Success");
return fut2();
}).compose(fut2Result -> {
if (fut2Result.containsKey("detail")) {
// 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("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());
});
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<JsonObject> fut1 = func1(abc, xyz);
fut1.compose(fut1Result -> {
LOGGER.debug("Success");
return fut2();
}).compose(fut2Result -> {
if (fut2Result.containsKey("detail")) {
LOGGER.error("Errorfailed. ");
return Future.failedFuture(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());
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论