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?