如何在Spring Java Config的单个MessageListenerContainer中添加多个JMS MessageListner

时间:2014-02-19 13:17:07

标签: java xml spring jms spring-jms

我的spring-config.xml

中有以下xml代码
<jms:listener-container acknowledge="auto"
        connection-factory="cachedConnectionFactory" container-type="default"
        error-handler="consumerErrorHandler" concurrency="20-25">
        <jms:listener destination="#{TaskFinished.destination}"
            method="onMessage" ref="taskFinished" />
</jms:listener-container>

现在,我正在将spring xml配置文件转换为Java配置。

我将其翻译为

@Bean(name = "consumerJmsListenerContainer")
public DefaultMessageListenerContainer consumerJmsListenerContainer() {
    DefaultMessageListenerContainer messageListenerContainer = new DefaultMessageListenerContainer();
    messageListenerContainer
            .setConnectionFactory(cachingConnectionFactory());
    messageListenerContainer.setConcurrency("20-25");
    messageListenerContainer.setErrorHandler(new ConsumerErrorHandler());
    messageListenerContainer
            .setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
    messageListenerContainer.setMessageListener(new TaskFinished());
    return messageListenerContainer;
}

我需要知道的是,如果消息容器中有多个MessageListner,如

<jms:listener-container acknowledge="auto"
        connection-factory="cachedConnectionFactory" container-type="default"
        error-handler="consumerErrorHandler" concurrency="20-25">
        <jms:listener destination="#{questionGeneration.destination}"
            method="onMessage" ref="questionGeneration" />
        <jms:listener destination="#{friendShipLogic.destination}"
            method="onMessage" ref="friendShipLogic" />
        <jms:listener destination="#{postAvailabilityChecker.destination}"
            method="onMessage" ref="postAvailabilityChecker" />
        <jms:listener destination="#{playOn.destination}" method="onMessage"
            ref="playOn" />
</jms:listener-container>

我怎么能将这个xml代码转换为Java配置?

3 个答案:

答案 0 :(得分:8)

命名空间只是一种便利 - 每个<jms:listener/>元素都有自己的DMLC;外(容器)元素只是提供共同属性的工具。

答案 1 :(得分:0)

您可以添加container.setConcurrentConsumers(10);,其中消费者数量为10

答案 2 :(得分:0)

您可以添加多个注释,每个注释具有自己的并发性。

@JmsListener(id="1", destination = "mydestination", containerFactory = "myfactory", concurrency ="1-5")

@JmsListener(id="2", destination = "mydestination", containerFactory = "myfactory", concurrency = "3-5")