请查看以下位于扩展 AbstractVerticle
的类内部的代码:
@Override
public void start(Future<Void> serverStartFuture) throws Exception {
log.info("Deploying " + this.getClass().toString() + " verticle...");
//TODO: Handler is not calling.
serverStartFuture.setHandler(event -> {
if(event.succeeded()){
log.info("Deploying " + this.getClass().toString() + " verticle SUCCESS");
} else if (event.failed()){
log.error("Deploying " + this.getClass().toString() + " verticle FAIL:");
log.error(event.cause());
}
});
/* To follow future compose pattern in future */
Future<Void> initSteps = this.initHttpServ();
initSteps.setHandler((AsyncResult<Void> asyncResult) -> {
if(asyncResult.succeeded()){
serverStartFuture.complete();
}else if(asyncResult.failed()){
serverStartFuture.fail(asyncResult.cause());
}
});
}
假设 initHttpServ
总是返回完整的 future:
private Future<Void> initHttpServ(){
Future<Void> httpServerFuture = Future.future();
httpServerFuture.complete();
return httpServerFuture;
}
为什么在我的情况下从未调用serverStartFuture.setHandler
?
我是这样理解这个概念的:
- 创造 future
f
- 设置
f
future 处理程序 - 忘记它
- 代码中的其他位置将
f
设置为complete
/fail
- 设置
f
结果后,处理程序将调用
但是我的一段代码似乎否定了这种方法。 我做错了什么吗?
最佳答案
您不应该设置 serverStartFuture
处理程序。它是在部署 verticle 时由 Vert.x 设置的。当你的verticle成功启动时,你应该完成future,否则失败。
参见Asynchronous Verticle start and stop在 Vert.x 核心文档中。
关于java - Vert.x future 处理程序设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45816191/