我需要向/从存储在单个 JMS 服务器上的不同主题发送/接收消息。
我想JmsTemplate
用于发送和MessageListenerContainer
注册异步监听器。
我的配置如下所示:
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">xxx</prop>
<prop key="java.naming.provider.url">yyy</prop>
</props>
</property>
</bean>
<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref ="jndiTemplate"/>
<property name="jndiName" value="TopicConnectionFactory"/>
</bean>
<bean id="singleConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg ref="connectionFactory"/>
</bean>
<bean id="tosJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="singleConnectionFactory"/>
<property name="destinationResolver" ref="destinationResolver"/>
<property name="pubSubDomain" value="true"/>
</bean>
据我了解,singleConnectionFactory
总是返回相同的连接实例,有助于减少每次jmsTemplate
需要(例如)发送/接收消息时创建和关闭连接的开销(就像使用普通的一样ConnectionFactory
)。
我的第一个问题是:如果我创建多个jmsTemplate
(s),它们都可以共享一个 refsingleConnectionFactory
吗?或者他们是否必须每个都接收一个不同的实例(singleConnectionFactory1
,,singleConnectionFactory2
等)?
阅读 API SingleConnectionFactory
,我发现:
请注意,Spring 的消息侦听器容器支持
Connection
在每个侦听器容器实例中使用共享。只有在多个侦听器容器之间共享单个 JMS 连接时,组合使用SingleConnectionFactory
才真正有意义。
这对我来说听起来有点神秘。据我所知,每个 只能注册 1 个 Listener MessageListenerContainer
,所以我不明白连接共享的程度。
假设我想注册 N 个 Listeners:我需要重复 N 次,如下所示:
<bean
class="org.springframework.jms.listener.SimpleMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destinationName" value="destX" />
<property name="messageListener" ref="listener1outOfN" />
</bean>
在这种情况下,从 connectionFactory 创建了多少个连接?每个 ListenerContainer 一个或只是一个连接池?如果我为SimpleMessageListenerContainer
-s 提供一个 refsingleConnectionFactory
怎么办?
在这种情况下,最好的方法是什么(当然,从表演的角度来看)?