简体   繁体   中英

EJB forcing single-threaded access to individual members of a resource collection

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?

Are you sure that you want to use EJB for that ? Mixing standard java concurrent mechanisms and JavaEE is rather discouraged...

See: why-spawning-threads-in-java-ee-container-is-discouraged

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM