问题
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:
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) 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>
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
来源:https://stackoverflow.com/questions/12814547/spring-configuration-for-multiple-destinations-and-consumers