问题描述
我正在使用一个具有一个目的地和一个消费者(消息监听器)的消息监听器容器:
I'm using a message listener container with one destination and one Consumer (message listener):
<bean id="msgListenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer"
p:connectionFactory-ref="connectionFactory"
p:destination-ref="destination"
p:messageListener-ref="messageHandler"
p:concurrentConsumers="10"
p:maxConcurrentConsumers="50"
p:receiveTimeout="5000"
p:idleTaskExecutionLimit="10"
p:idleConsumerLimit="5" />
如果我想要多个目的地,并且对于每个目的地一个消息侦听器,我该怎么办?而且,如果我想在一个目的地使用多个侦听器,该怎么办?
If i want multiple destinations and for each destination one message listener, what should i do?And if i want multiple listener for one destination, what should i do?
推荐答案
1)您需要在Spring应用程序上下文中将每个消息侦听器和生产者定义为bean.像这样:
1) You need to define each Message Listener and producer in your spring application context as beans. Something like this:
<!-- MESSAGE LISTENER CONTAINER -->
<bean id="ListListenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="messageListener" ref="messageListener" />
<property name="connectionFactory" ref="qcf" />
<property name="destinationResolver" ref="JmsDestinationResolver" />
<property name="receiveTimeout" value="${jms-timeout}" />
<property name="destinationName" value="${jms-list-topic}" />
<property name="concurrency" value="1" />
<property name="pubSubDomain" value="true" />
<property name="subscriptionDurable" value="${jms-durable-flag}"/>
<property name="durableSubscriptionName" value="${jms-list-durable-name}" />
<property name="clientId" value="${jms-list-client-id}"/>
<property name="sessionTransacted" value="true"/>
</bean>
<bean id="publisher-1" class="com.stack.overflow.JmsPublisherImpl">
<constructor-arg ref="jmsTemplate" />
</bean>
2)然后,您可以使用自动装配或在将处理消息的类上在应用上下文中定义的相关生产者(请参见下文).即上面的消息侦听器bean引用指向的类:
2) Then you can set the relevant producers using Autowiring or defined in app context(see below) on the class that will handle the message. i.e the class that the Message Listener bean ref above points to:
<bean id="messageListener" class="com.stack.overflow.MessageHandler">
<property name="publisher" ref="publisher-1" />
</bean>
这只是1-2-1映射.对于任何其他路由,您可以添加多个发布者(如上),然后由您决定如何实现所需的路由逻辑,以决定哪个主题/队列应该发布从使用者1等接收的消息
This is just 1-2-1 mapping. For any other routing you can add more than one publisher (like above), then it is up to you how you implement the required routing logic to decide which topic/Queues should publish a Message received from Consumer 1 etc etc
这篇关于用于多个目标和使用者的Spring配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!