使用 nio 的客户端套接字的套接字管理器
我是一名 Scala 开发新手,试图弄清楚如何使用 Java nio 设计套接字管理器。
我希望管理器能够处理“连接”、“断开连接”和“发送”等消息,并能够通过向它们发送带有接收到的数据的“onData”消息来管理多个客户端套接字。
据我了解,选择器必须在自己的线程中运行。
因此,我可以将选择器选择放在自己的线程中,但是当选择器可能正在调用 select 时,如何向选择器注册新的套接字通道(据我所知,选择器不是线程安全的)?
在 Scala 中组合 actor 和线程时,什么是好的设计?
I am a newbie Scala developer trying to figure out how to design a socket manager using Java nio.
I would like the manager to handle messages such as 'connect', 'disconnect' and 'send' etc and be able to manage several client sockets by sending them 'onData' messages with received data.
To my understanding, the selector has to run in a thread of its own.
So I can put the selector selecting in its own thread, but how can I register new socket channels with the selector (to my understanding a selector is not thread safe) while the selector may be in a call to select?
What is a good design when combining actors and threads in Scala?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非你这样做纯粹是为了说教,否则我的建议是“不要重新发明轮子”。 :)
演员 + NIO = Naggati。
或者,如果您想要较低级别的东西,我建议 Netty (Naggati 使用)
Unless you're doing this for purely didactic purposes, my advice is "Don't reinvent the wheel." :)
Actors + NIO = Naggati.
Or, if you want something lower-level, I'd suggest Netty (which Naggati uses)