简体   繁体   English

为什么JMS会话对象不是线程安全的?

[英]Why JMS Session Object is not thread safe?

为什么JMS规范的设计方式使得当创建Sessions的Connection对象是线程安全的(由线程共享)时,并发线程不能使用Session对象?

JMS 2.0 Spec JMS 2.0规范

page 25 第25页

There are two reasons for restricting concurrent access to sessions. 限制对会话的并发访问有两个原因。

First, sessions are the JMS entity that supports transactions. 首先,会话是支持事务的JMS实体。 It is very difficult to implement transactions that are multi-threaded. 实现多线程的事务非常困难。

Second, sessions support asynchronous message consumption. 其次,会话支持异步消息消费。 It is important that JMS not require that client code used for asynchronous message consumption be capable of handling multiple, concurrent messages. 重要的是JMS不要求用于异步消息消费的客户端代码能够处理多个并发消息。

In addition, if a session has been set up with multiple, asynchronous consumers, it is important that the client is not forced to handle the case where these separate consumers are concurrently executing. 此外,如果已经与多个异步使用者建立了会话,则不必强制客户端处理这些单独的使用者同时执行的情况。 These restrictions make JMS easier to use for typical clients. 这些限制使JMS更易于用于典型客户端。 More sophisticated clients can get the concurrency they desire by using multiple sessions. 更复杂的客户端可以通过使用多个会话来获得所需的并发性。 In the classic API and the domain-specific APIs this means using multiple session objects. 在经典API和特定于域的API中,这意味着使用多个会话对象。 In the simplified API this means using multiple JMSContext objects. 在简化的API中,这意味着使用多个JMSContext对象。

JMS Session object is typically used for Transactions. JMS会话对象通常用于事务。 And we all know transactions are thread specific and can't span across threads. 我们都知道事务是特定于线程的,不能跨越线程。 Hence a transacted JMS session can't be used across threads. 因此,不能跨线程使用事务处理的JMS会话。 I believe for this reason JMS specification says session is not thread safe. 我相信由于这个原因,JMS规范说会话不是线程安全的。

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

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