title: Spring JMS runtime connection management
body: <p>I have an app (Spring Boot based) which use JMS to works with messages, and I need to be able to connect to more than one MQ server (ActiveMQ), to listen for messages. One more problem with that is I need to turn-off some existing connection and add some new to another servers at run-time (let's imagine I have some db that stores URLs of servers and rest endpoint that inform app to reload config (URLs) from db and reconfigure/refresh current connection set).</p>
  
  <p>After digging information about Spring, JMS and DI configuration, I understand that it's not the best way to do what I need (maybe I'm wrong), because DI doesn't fit well for dynamic scenarios like mine.</p>
  
  <p><strong>Sample app flow</strong></p>
  
  <p><strong>App start</strong>  </p>
  
  <ul>
  <li>Configure DB beans  </li>
  <li>Load MQ configurations from DB (list of URLs) </li>
  <li>(???) For each MQ config: configure ConnectionFactory -> Do connection -> attach listeners/endpoints </li>
  </ul>
  
  <p><strong>App life-cycle</strong>  </p>
  
  <ul>
  <li>Handle REST request  </li>
  <li>Load MQ configurations from DB (list of URLs)  </li>
  <li>(???) For each MQ config:  
  
  <ul>
  <li>new item: configure new ConnectionFactory -> Do connection -> attach listeners/endpoints  </li>
  <li>deleted item: drop connection  </li>
  </ul></li>
  </ul>