Singleton Bean UDP 监听器
我对 Java EE 开发非常陌生,我正在尝试在 GlassFish 中创建 UDP 侦听器。这将始终需要运行。因此,我相信 Singleton bean 能够完成这项任务。
问题就在这里。该代码可以工作,但会导致 GlassFish 运行缓慢。尽管应用程序已部署,但 GlassFish 的管理页面只是挂起。我也无法访问已部署的 WAR 应用程序的其他元素,导致我相信存在线程问题。然而,我始终假设 EJB 不存在线程问题。我在 Eclipse 中做了这个。
@Singleton
@LocalBean
public class UDPListener {
public UDPListener()
{
DatagramSocket datagramSocket = null;
try
{
datagramSocket = new DatagramSocket(9090);
} catch (SocketException e) { e.printStackTrace(); }
byte[] buffer = new byte[100];
// Create a datagram packet.
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while(true)
{
// Receive the packet.
try {
datagramSocket.receive(packet);
} catch (IOException e) { e.printStackTrace(); }
buffer = packet.getData();
// Print the data:
System.out.println(new String(buffer));
}
}
}
我有什么遗漏的吗?我一直在回顾 Java EE 6 教程,它提到了一些关于并发访问的内容。但是,我不确定这是否是问题所在。
谢谢
编辑:只是为了添加更多信息,我本质上需要创建一个始终运行、侦听和响应传入的 UDP 数据包的 Bean。我该如何做以不杀死主线程的方式实例化这个bean?
I am incredibly new to Java EE development, and I am attempting to create a UDP Listener in GlassFish. This will always need to be running. Therefore, I believe a Singleton bean will accomplish this task.
Here is the problem. The code works, but it causes GlassFish to slug up. Despite the application getting deployed, the admin page for GlassFish just simply hangs. I also cannot access other elements of the deployed WAR application leading me to believe that there is a threading issue. However, I was always under the assumption that EJB's don't have threading problems. I have made this in Eclipse.
@Singleton
@LocalBean
public class UDPListener {
public UDPListener()
{
DatagramSocket datagramSocket = null;
try
{
datagramSocket = new DatagramSocket(9090);
} catch (SocketException e) { e.printStackTrace(); }
byte[] buffer = new byte[100];
// Create a datagram packet.
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while(true)
{
// Receive the packet.
try {
datagramSocket.receive(packet);
} catch (IOException e) { e.printStackTrace(); }
buffer = packet.getData();
// Print the data:
System.out.println(new String(buffer));
}
}
}
Is there something I'm missing? I have been reviewing the Java EE 6 Tutorial, and it mentions something about concurrent access. However, I am not sure if that is the problem.
Thank You
EDIT: Just to add some more information, I need to essentially create a Bean that will always run, listen to and respond to UDP packets that come in. How do I instantiate this bean in a way that does not kill the main thread?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 DatagramSocket.receive() javadocs:
通过在无限循环 (
while(true)
) 中调用accept
,您会主动等待传入数据,从而阻塞系统。编辑:
如果您的 UDP 侦听器不应阻塞主线程,则它必须在不同的线程中运行。只需使用您的侦听代码启动一个新线程,然后执行主线程中必须执行的操作即可。像这样的事情:
From the DatagramSocket.receive() javadocs:
By calling
accept
within an infinie loop (while(true)
), you are actively waiting for incoming data and thereby blocking your system.EDIT:
If your UDP listener should not block the main thread, it has to run in a different Thread. Just launch a new Thread with your listening code and do whatever has to be done in main thread. Something like this: