在 Java EE (EJB 3.0) 环境中从 DatagramSocket 读取
在我基于 JBoss(版本 5)的应用程序中,我需要侦听 UDP 数据包。
最简单的方法(可能也是唯一的方法)是在某个端口上打开 DatagramSocket(使用 JBoss @Service 单例 bean)并调用 receive(...) 方法。第一部分非常简单 - 我已经对其进行了测试并且按预期工作。第二部分——接收数据包更加复杂。
我想知道这样的代码的设计。正如 DatagramSocket javadoc 中所述,接收是一个阻塞操作 - 它会阻塞直到检索到数据包。如何对其进行编码以确保其快速、可扩展且线程安全?
我考虑过编写无状态 bean,它保存对打开连接的单例的引用,并且这些 bean 正在调用 receive 方法 - 但我不太确定 JBoss 将如何管理它们(一个 bean 调用 receive() ,直到数据包到达它才会被阻止) 。 有什么模式可以用吗?
In my application based on JBoss (version 5) I need to listen for UDP packets.
The easiest way (and probably the only one ) is to open DatagramSocket on certain port (using JBoss @Service singleton bean) and call receive(...) method. The first part is pretty easy - I've tested it and works as expected. Second part - receiving packets is more complicated.
I am wondering about design of such a code. As it is stated in DatagramSocket javadoc, receive is a blocking operation - it blocks until packet is retrieved. How to code it to make sure that it is fast, scalable and thread-safe?
I thought about writing stateless beans that holds a reference to singleton with opened connection, and these beans are calling receive method - but I am not very sure how JBoss would manage them (one bean calls receive() and until packet arrives it is blocked).
Is there any pattern to use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Apache Camel 等开源框架旨在支持此类用例。它们经过充分测试并证明可以在生产中正常工作。我的建议是依赖这些库而不是创建自己的库。
Open source frameworks like Apache Camel etc are meant to support these kind of use cases. They are well tested and proven to be working in production. My suggestion is to depend on those libraries than creating your own.