1

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?

Phanto
  • 1,147
  • 5
  • 16
  • 19
  • It would help to know what the sending side looks like. – Seth Noble Feb 13 '12 at 22:34
  • The sending side would just be sending in an XML string via UDP. It receives it and displays it as text ok, but the problem is that nothing else can be done while this is running. – Phanto Feb 14 '12 at 01:16

1 Answers1

1

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
TPete
  • 2,049
  • 4
  • 24
  • 26
  • How do I listen to UDP packets while not blocking system? – Phanto Feb 14 '12 at 15:48
  • I am able to use a Datagram Channel which does not perform blocking, but while I am waiting for an incoming message, it still just sits there. How can I launch a bean that will do this and have the application continue loading? – Phanto Feb 14 '12 at 19:56
  • I will give this a try and let you know how it turns out. – Phanto Feb 14 '12 at 20:46
  • This does work, but is this optimal? I always thought EJB's were able to do this on their own without need for playing around with threading. What I basically want to accomplish is the creation of what is effectively a unique process that runs within the same web application container. – Phanto Feb 15 '12 at 12:43
  • How is your container supposed to handle this on its own? It is told to wait in an infinite loop for incoming packets and that's what it is doing. If it should do something else at the same time, another thread is required for that. You are probably referring to EJBs handling multiple request but that's a whole different story. – TPete Feb 15 '12 at 13:03
  • Would a resource adapter accomplish what I need to do? I really wish there was a simple tutorial that can explain how all of this works, since there really is a whole lot involved here. BTW, I*really* appreciate your help and assistance! – Phanto Feb 15 '12 at 15:58
  • 1
    I found this [thread](http://stackoverflow.com/questions/2154490/an-ear-java-ee-application-which-listen-to-a-socket-request) here on SO, which might be helpful. And, your welcome. – TPete Feb 15 '12 at 19:54
  • That thread is exactly what I needed. Thank You. Now if I only knew how to write a JCA... – Phanto Feb 15 '12 at 20:30
  • TPete, do you know of any good Resource Adapter tutorials? I am pretty much stuck looking at the spec, and it is terrible...Hardly any useful code samples. I am stuck at the activation specification. I used this article as a guidance, but it is incomplete and I am stuck: http://www.theserverside.com/news/1364656/Serve-It-Up-with-J2EE-14-Use-JCA-15-and-EJB-21-to-Extend-your-App-Server – Phanto Feb 21 '12 at 15:18
  • Sorry, can't help you with that. Never had to touch that topic. Why don't you ask a new question here? – TPete Feb 22 '12 at 07:42