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?
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.