Fed*_*ico 1 performance spring jms spring-jms
我需要向/从存储在单个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>
Run Code Online (Sandbox Code Playgroud)
据我所知singleConnectionFactory,返回总是相同的连接实例,有助于减少每次jmsTemplate需要(例如)发送/接收消息时创建和关闭连接的开销(就像使用法线时一样ConnectionFactory).
我的第一个问题是:如果我创建了多个jmsTemplate(s),他们是否可以共享一个参考singleConnectionFactory?或者,他们要接受不同的情况下,每个(singleConnectionFactory1,singleConnectionFactory2,等)?
阅读API SingleConnectionFactory,我发现了这个:
请注意,Spring的消息侦听器容器支持
Connection在每个侦听器容器实例中使用共享.SingleConnectionFactory组合使用对于跨多个侦听器容器共享单个JMS连接非常有意义.
这听起来对我来说有点神秘.据我所知,每个只能注册一个Listener MessageListenerContainer,所以我不明白连接的共享程度.
假设我要注册N个监听器:我需要重复N次这样的事情:
<bean
class="org.springframework.jms.listener.SimpleMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destinationName" value="destX" />
<property name="messageListener" ref="listener1outOfN" />
</bean>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,connectionFactory创建了多少个连接?每个ListenerContainer一个或只是一个Connections池?如果我给SimpleMessageListenerContainer-s提供参考singleConnectionFactory怎么办?
在这种情况下,最好的方法是什么(从表演的角度来看)?
如果我创建多个jmsTemplate(s),它们是否可以共享一个singleConnectionFactory的引用?
是的,这很好.javadoc SingleConnectionFactory说:
根据JMS Connection模型,这是完全线程安全的(与例如JDBC相反).
JMS Connection对象是线程安全的,并且可以由多个线程同时使用.所以不需要使用多个SingleConnectionFactorybean.
据我所知,每个只能注册一个Listener
MessageListenerContainer,所以我不明白连接的共享程度.
这是真的; 但是,每个都MessageListenerContainer可以有多个线程同时处理消息,所有这些都使用相同的MessageListener对象.在MessageListenerContainer将使用一个单一的共享Connection所有这些线程(除非进行其他配置做).
请注意,Spring的消息侦听器容器支持在每个侦听器容器实例中使用共享Connection.
SingleConnectionFactory组合使用对于跨多个侦听器容器共享单个JMS连接非常有意义.
换句话说,如果您只拥有一个MessageListenerContainer,那么SingleConnectionFactory就没有必要了,因为单个连接是内部管理的MessageListenerContainer.如果您有多个侦听器容器,并希望它们共享一个连接,则SingleConnectionFactory需要.此外,如果您想要像收听那样共享收听和发送之间的连接,那么SingleConnectionFactory也是必要的.