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
So if you have ConnectionFactory
objects in a map, you can create similar map of JmsTemplate
s. Use this JmsTempalte contructor and after one loop (or stream) and you can have map of JmsTemplate
s. 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).