Spring - Dynamically create JMSTemplates

前端 未结 1 994
我在风中等你
我在风中等你 2021-01-03 11:49

I am using Spring boot, and I would like to dynamically create multiple JMS Templates, as I would like to connect to different JMS instances. I am aware of the standard appr

相关标签:
1条回答
  • 2021-01-03 12:19

    So if you have ConnectionFactory objects in a map, you can create similar map of JmsTemplates. Use this JmsTempalte contructor and after one loop (or stream) and you can have map of JmsTemplates. So sending is easy.

    Harder part would be listeners. If you have dynamic destinations, you need to forget about listener annotations (@JmsListener). You can make it working by creating map of DefaultMessageListenerContainer.

    Each container will be created by something like:

    DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setDestination(topic);
    container.setMessageListener(listenerInstance);
    container.start();
    

    You can also control lifecycle of each listener container via stop, shutdown, ....

    But bear in mind that handling a lot of queues this way may be resource intensive. Also you will need to take care about closing resources yourself probably (Spring wouldn't do it for you).

    0 讨论(0)
提交回复
热议问题