2

我需要配置 DefaultMessageListenerContainer 来解析多个目标主题。我不想实例化多个容器,我想知道如何配置它,或者即使它是可能的。

谢谢

4

1 回答 1

4

If you wish to listen for messages from multiple topics using straight Spring JMS, you will need to create multiple instances of this class. The only issue is that by default each will creates its own TaskExecutor, which is a little bit wasteful. To get around this simply define your own taskExecutor bean in the context and all the message listeners will automatically use it.

Here is a task executor which works well in a standalone VM, there are alternative implementations which integrate with different containers' native work manager / thread pool capabilities.

<bean id="taskExecutor" class="org.springframework.scheduling.quartz.SimpleThreadPoolTaskExecutor">
    <property name="waitForJobsToCompleteOnShutdown" value="true"/>
    <property name="threadCount" value="20"/>
    <property name="threadNamePrefix" value="JmsConsumer"/>
</bean>

Alternatively, using Spring Integration, simply create a message channel and multiple <jms:message-driven-channel-adapter/>'s - one for each topic. Here is a sample snippet:

<bean id="connectionFactory" class="..."/>

<int:channel id="allMessages"/>

<jms:message-driven-channel-adapter channel="allMessages" connection-factory="connectionFactory" destination-name="T.topic1"/>
<jms:message-driven-channel-adapter channel="allMessages" connection-factory="connectionFactory" destination-name="T.topic2"/>
<jms:message-driven-channel-adapter channel="allMessages" connection-factory="connectionFactory" destination-name="T.topic3"/>

<int:service-activator input-channel="allMessages" output-channel="results" ref="messageProcessingBean" method="onMessage"/>

Behind the scenes, Spring Integration will automatically create a MessageListenerContainer.

Yet another alternative would be to use the routing facilities of your JMS provider/MOM to create a single destination where all relevant messages are routed. E.g. in RabbitMQ you can create a topic which is bound to multiple exchanges, and thus only one AMQP destination to consume. This is infrastructure dependent however, and will not be possible with any given JMS provider.

于 2011-12-27T13:30:28.077 回答