gpt4 book ai didi

groovy - Vertx 和 Redis : I cannot make them working together

转载 作者:IT王子 更新时间:2023-10-29 06:15:01 25 4
gpt4 key购买 nike

我在 Groovy 中有我的简单 Vertx 脚本,它应该向 Redis 发送请求以取回值:

def eb = vertx.eventBus
def config = [:]

def address = 'vertx.mod-redis-io'

config.address = address
config.host = 'localhost'
config.port = 6379

container.deployModule("io.vertx~mod-redis~1.1.4", config)

eb.send(address, [command: 'get', args: ['mykey']]) { reply ->
if (reply.body.status.equals('ok')) {
println 'ok'
// do something with reply.body.value
} else {
println("Error ${reply.body.message}")
}
}

“mykey”的值定期存储在我的 Redis (localhost:6379) 上:

127.0.0.1:6379> get mykey
"Hello"

脚本正确启动但没有返回任何值(回复)。

我错过了什么吗?

最佳答案

问题是您部署模块并按顺序发送到 EventBus,即使调用是异步的。

因此,当您调用 deployModule 时,模块部署会被触发,但不能保证在调用 eb.send 之前。通过这种方式,您发送了正确的命令,但由于模块不存在,因此无法计算它。

尝试以下将测试命令添加到 deployModule 的 AsyncHandler

container.deployModule("io.vertx~mod-redis~1.1.4", config) { asyncResult ->
if(asyncResult.succeeded) {
eb.send(address, [command: 'get', args: ['mykey']]) { reply ->
if (reply.body.status.equals('ok')) {
println 'ok'
// do something with reply.body.value
} else {
println("Error ${reply.body.message}")
}
}
} else {
println 'Deployment broken!'
}
}

例子来自 https://github.com/vert-x/mod-redis可能不是最好的,因为它只是指明方向的片段。

这是有效的,因为它只在模块部署后立即向总线发送请求,并且有人正在监听它。我在使用 Redis 的 Vagrant 分期付款上对其进行了本地测试。

总的来说,由于 Vert.x 的关键概念,Vert.x 中的开发几乎总是异步的。熟悉它需要一些时间,但它有它的好处:)

希望这对您有所帮助。

最佳

关于groovy - Vertx 和 Redis : I cannot make them working together,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24881708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com