作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我在 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/
我是一名优秀的程序员,十分优秀!