3

我正在阅读JMS 的 JEE5 教程

而且我很难理解这个“一般规则”的原因:

Java EE 平台规范中的一般规则适用于在 EJB 或 Web 容器中使用 JMS API 的所有 Java EE 组件:

Web 和 EJB 容器中的应用程序组件不得尝试为每个连接创建多个活动(未关闭)会话对象。

此规则不适用于应用程序客户端。

为什么它不适用于应用程序客户端而适用于 Web/EJB 组件。?

4

1 回答 1

2

在独立客户端和 Java EE 环境中使用 JMS 时,有很多关于 JMS 的规则是不同的。想到的一件事是创建 JMS 侦听器的能力。

和旧的(但仍然是来自 1.3 规范的相关引用):

... Note that the JMS API creates threads to deliver messages to 
message listeners. The use of this message listener facility may be 
limited by the restrictions on the use of threads in various 
containers. In EJB containers, for instance, it is typically not 
possible to create threads. The following methods must not be used by 
application components executing in containers that prevent them 
from creating threads: 
. 
- javax.jms.Session method setMessageListener 
- javax.jms.Session method getMessageListener 
- javax.jms.Session method run 
- javax.jms.QueueConnection method createConnectionConsumer 
- javax.jms.TopicConnection method createConnectionConsumer 
- javax.jms.TopicConnection method createDurableConnectionConsumer 
- javax.jms.MessageConsumer method getMessageListener 
- javax.jms.MessageConsumer method setMessageListener 
. 
In addition, use of the following methods on javax.jms.Connection
objects by applications in Web and EJB containers may interfere with the
connection management functions of the container and must not be used:
- setExceptionListener 
- stop
- setClientID 
A J2EE container may throw a JMSException if the application component 
violates these restrictions. ....

我猜在 Java EE 环境中不允许为每个连接创建多个会话,因为这些连接是受管理的,并且很难以这种方式将它们分开。但希望有人能给出更明确的答案。

请注意,JMS for Java EE 7 的新 2.0 版本在核心 JMS 规范和 Java EE 之间具有更好、更清晰的一致性,这是其目标之一。见这里:http: //jcp.org/en/jsr/detail ?id=343

于 2011-04-14T21:14:59.170 回答