Groovy中未调用Vert.x Future/Promise处理程序
我得到了一个相当小的Verticle,它应该连接到数据库,定期轮询一个表,并将对象发送到事件总线。到目前为止,我可以连接到数据库,但之后处理程序没有执行,轮询计时器也没有启动。我想这是显而易见的,感谢你的帮助 我使用Vert.x3.8承诺(就像链接后面的示例一样)。正如您在我的代码中所看到的,使用不推荐的期货效果很好!但是谁想使用不推荐的代码呢?要么我做错了什么,要么那是Vert.x中的一个bug,我不这么认为 我的x顶点是3.8.1Groovy中未调用Vert.x Future/Promise处理程序,groovy,vert.x,vertx-verticle,Groovy,Vert.x,Vertx Verticle,我得到了一个相当小的Verticle,它应该连接到数据库,定期轮询一个表,并将对象发送到事件总线。到目前为止,我可以连接到数据库,但之后处理程序没有执行,轮询计时器也没有启动。我想这是显而易见的,感谢你的帮助 我使用Vert.x3.8承诺(就像链接后面的示例一样)。正如您在我的代码中所看到的,使用不推荐的期货效果很好!但是谁想使用不推荐的代码呢?要么我做错了什么,要么那是Vert.x中的一个bug,我不这么认为 我的x顶点是3.8.1 class JdbcVerticle extends Abs
class JdbcVerticle extends AbstractVerticle {
private SQLConnection connection
@Override
void start(Promise<Void> startPromise) {
def jdbcParams = config().getJsonObject('connection')
// This gets executed:
testFuture().handler = { println "Test Future handler runs!" }
// This is never executed :-(
connect(jdbcParams).handler = { println "Connected..." }
}
Future<Void> connect(JsonObject jdbcParams) {
def promise = Promise.<Void>promise()
def client = JDBCClient.createShared(vertx, jdbcParams)
client.getConnection() { connection ->
if(connection.failed()) {
promise.fail(connection.cause())
} else {
this.connection = connection.result()
promise.complete()
}
}
return promise.future()
}
Future<Void> testFuture() {
def future = Future.<Void>future()
vertx.setTimer(200) { future.complete() }
return future
}
}
类JdbcVerticle扩展了AbstractVerticle{
专用SQLConnection连接
@凌驾
无效开始(承诺开始建议){
def jdbcParams=config().getJsonObject('连接')
//这将被执行:
testFuture().handler={println“测试未来处理程序运行!”}
//这是永远不会执行的:-(
connect(jdbcParams).handler={println“Connected…”
}
未来连接(JsonObject jdbcParams){
def promise=promise.promise()
def client=JDBCClient.createShared(vertx,jdbcParams)
client.getConnection(){connection->
if(connection.failed()){
promise.fail(connection.cause())
}否则{
this.connection=connection.result()
承诺,完成
}
}
回报承诺未来
}
未来测试未来(){
def future=future.future()
setTimer(200){future.complete()}
回归未来
}
}
为什么您的connect()
返回的是未来而不是承诺?您确定它正在连接吗?是否调用了未来的处理程序?是否使用failed()调用它
设置为返回true?@injecteer因为Future是Promise的读者端@tim_yates非常确定,因为client.getConnection()
中的一个简单的println
语句被执行。无论哪种方式,failed()
或complete()
,应该调用该处理程序。我同意应该调用该处理程序,但如果它失败()
,则返回将阻止您到达标记为/的println:-(