How, in an EJB environment, can I force interaction with any given resource in a collection of resources to be single threaded, without limiting access to the collection itself? I want to allow one thread to interact with resource A while another is interacting with resource B; but prohibit two threads from interacting with the same resource at the same time.
The back story: I've got multiple devices on a network, and multiple threads within my application that might want to talk to any given device. The devices themselves are dumb and have no concept of a session, which means that, without an access control mechanism, it's likely that messages from different threads to the same device will interleave.
The pool of devices is potentially hundreds, but not thousands. It is also somewhat dynamic - devices appear and drop.
I could roll my own class that manages concurrency for a single device, instantiate one for each device, and then have a singleton bean that manages the collection and dispatches requests to the individual instances.
But most or all of this must be already taken care of, somewhere in Java's concurrency mechanisms?
Is there a well-established pattern here that I should be using?