简体   繁体   English

EJB强制对资源集合的各个成员进行单线程访问

[英]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? 在EJB环境中,如何在不限制对集合本身的访问的情况下,强制与资源集合中的任何给定资源进行单线程交互? I want to allow one thread to interact with resource A while another is interacting with resource B; 我想允许一个线程与资源A交互,而另一个线程与资源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. 我可以滚动自己的类来管理单个设备的并发性,为每个设备实例化一个类,然后有一个Singleton bean来管理集合并将请求分派到各个实例。

But most or all of this must be already taken care of, somewhere in Java's concurrency mechanisms? 但是大多数或所有这些必须已经在Java的并发机制中得到了解决?

Is there a well-established pattern here that I should be using? 我在这里应该使用一种完善的模式吗?

Are you sure that you want to use EJB for that ? 您确定要为此使用EJB吗? Mixing standard java concurrent mechanisms and JavaEE is rather discouraged... 不建议将标准的Java并发机制与JavaEE混合使用。

See: why-spawning-threads-in-java-ee-container-is-discouraged 请参阅: 为什么在Java-ee-container中生成线程不清晰

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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