I am creating a Java EE application that allows users to add/remove "socketinfo" tables (stored in a database) from a web interface. If the user enables a "socketinfo" from the web interface, the application server must create a socket listener for the incoming packets and process the data. If the user disables or deletes the "socketinfo" the socket listener must be removed. The entire product must be contained in a single ear, and preferrably compliant. Some approaches I have considered but ran into problems with are:
Create a JCA resource adaptor for sockets and use MDBs as the listeners. The problem I ran into here was that I cannot figure out how to programmatically deploy MDBs for different sockets when the user adds them.
Create a @Singleton/@Service ejb that manages the daemon threads with careful synchronization. The singleton ejb can be injected into the business layer so that CRUD operations and socket manipulation happen in the right workflow. The problem here was that supposedly creating threads from EJBs is considered a bad practice and is not spec compliant (even if the singleton life cycle is correctly handled and proper synchronization mechanisms are in place?).
Put the threads in the domain model (another singleton?) and have the EJBs use the model. This was the worst of all of them since application servers tend to have multiple classloaders, less container support in general, plus this suffers from everything 2. suffers from.
Any idea how to correctly handle this situation in Java EE?
EDIT: An extension to this question: Assuming I decide to approach this problem like ewernli suggests in his solution 3, what do I gain by doing this in JCA (with a custom interface to add internal threads) that I would not get from a (well-designed) singleton? While creating a resource adapter does not look like a monstrous task, it does not seem entirely trivial and could be a little time consuming (and maybe even harder to follow for other developers).