Singleton Bean UDP 监听器

发布于 2025-01-05 07:55:33 字数 1189 浏览 2 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕花眠 2025-01-12 07:55:33

从 DatagramSocket.receive() javadocs

此方法会阻塞,直到收到数据报为止。

通过在无限循环 (while(true)) 中调用 accept,您会主动等待传入数据,从而阻塞系统。

编辑:
如果您的 UDP 侦听器不应阻塞主线程,则它必须在不同的线程中运行。只需使用您的侦听代码启动一个新线程,然后执行主线程中必须执行的操作即可。像这样的事情:

Thread t = new Thread(new Runnable() {

    @Override
    public void run() {
        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));
        }
    }
});

t.start();

//go on with main thread

From the DatagramSocket.receive() javadocs:

This method blocks until a datagram is received.

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:

Thread t = new Thread(new Runnable() {

    @Override
    public void run() {
        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));
        }
    }
});

t.start();

//go on with main thread
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文