等待sql i/o完成的正确方法
有没有人有一个简单的代码示例来展示 netty 3.2.7 如何 应正确处理以下场景中的sql查询:
调用方法将消息传递到服务器messageReceived 方法。 messageRecieved 方法执行 sql 查询并获取 结果。调用方法不应继续处理,直到 获取sql查询的结果。
特别是 netty 等待的方式的示例 i/o(sql查询)来完成会非常有用。
Does anyone have a simple code example showing how netty 3.2.7
should correctly handle an sql query in the following scenario:
The calling method passes the message to the server messageReceived
method. The messageRecieved method does the sql query and gets the
result. The calling method should not continue processing until it
gets the result of the sql query.
In particular, an example of the way that netty waits for the
i/o (sql query) to complete would be very useful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在执行 sql 查询的处理程序前面添加一个 ExecutionHandler。就是这样...
否则你会阻塞你的工作线程
You need to add an ExecutionHandler in front of your handler that does the sql query. Thats it...
Otherwise you will block your worker-thread
诺曼感谢您提供额外信息。我认为这可以解决问题,但它涉及添加一个额外的处理程序,这并不是真正需要的,并且添加纯粹是为了确保 sql 查询已完成(没有功能)。
我寻找“更标准”的方式并找到了链接
“http://docs.jboss.org/netty/3.2/api/org/jboss/netty/channel/ChannelFuture.html”其中写着“提供了各种方法来让您检查I/O操作是否已完成,等待完成,并检索 I/O 操作的结果。它还允许您添加 ChannelFutureListener,以便在 I/O 操作完成时收到通知......”
它还表示 Prefer。 addListener(ChannelFutureListener) 到await() 但我不知道如何实现它。
如果有人知道如何实现它来等待 i/o sql 查询的结果完成,它将非常有用。
为了解决 sendMessage 方法中的问题,我使用了
此方法 - 它等待 sql 查询完成,然后返回给调用者。
Norman thanks for the extra information. This I think this could solve the issue, but it involves adding an extra handler which is not really required and would be added purely to ensure that the sql query has completed (with no functionality).
I looked for a "more standard" way and found the link
"http://docs.jboss.org/netty/3.2/api/org/jboss/netty/channel/ChannelFuture.html" which says "Various methods are provided to let you check if the I/O operation has been completed, wait for the completion, and retrieve the result of the I/O operation. It also allows you to add ChannelFutureListeners so you can get notified when the I/O operation is completed. ...."
It also says Prefer addListener(ChannelFutureListener) to await() but I don't know how to implement this.
If anyone knows how to implement this to wait for the result of an i/o sql query to complete it would be very useful.
To solve the issue in the sendMessage method I used
This works - it waits for the sql query to complete and then retuns to the caller.