【发布时间】:2012-02-15 02:38:09
【问题描述】:
我需要向/从存储在单个 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),它们可以共享一个singleConnectionFactory 的引用吗?还是他们必须分别接收一个不同的实例(singleConnectionFactory1、singleConnectionFactory2 等)?
阅读SingleConnectionFactory的API,我发现:
请注意,Spring 的消息侦听器容器支持使用共享的
Connection在每个侦听器容器实例中。结合使用SingleConnectionFactory只有在跨多个侦听器容器共享单个 JMS 连接时才真正有意义。
这对我来说听起来有点神秘。据我所知,每个MessageListenerContainer只能注册1个Listener,所以我不明白连接共享到什么程度。
假设我想注册 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 提供singleConnectionFactory 的引用呢?
在这种情况下,最好的方法是什么(当然从表演的角度来看)?
【问题讨论】:
标签: performance spring jms spring-jms