How to add multiple JMS MessageListners in a single MessageListenerContainer for Spring Java Config

二次信任 提交于 2019-12-19 16:35:26

问题


I had the following xml code in my spring-config.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>

Now, I was converting my spring xml configuration file to Java configuration.

I translated it like

@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;
}

What I need to know is, if there were more than one MessageListner in the Message Container like

<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>

How could I supposed to convert this xml code into Java config?


回答1:


The namespace is just a convenience - each <jms:listener/> element gets its own DMLC; the outer (container) element is just a vehicle to supply common attributes.




回答2:


You can add container.setConcurrentConsumers(10);, where number of consumers is 10



来源:https://stackoverflow.com/questions/21881657/how-to-add-multiple-jms-messagelistners-in-a-single-messagelistenercontainer-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!