How to configure embedded ActiveMQ Broker URL with Spring Boot

对着背影说爱祢 提交于 2019-12-04 03:16:10
Aliyu Fonyuy

I believe I figured this out after playing around with this for sometime. I thought two instances were running because I was trying to create a connection to the DEFAULT embedded broker (in an attempt to figure out if it was created/existed) like below:

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");

But apparently Spring Boot figures one doesn't exist and creates it at that time.

So in order to have only my created instance running, all I needed to do was provide a URL I added to the connector when I created the instance (in this case tcp://localhost:61616) in the application.properties file as below

spring.activemq.broker-url=tcp://localhost:61616

and Spring Boot will connect to this instance and not create another one. In the absence of the above entry in the properties file (or if you make an attempt to connect to an embedded instance using vm://localhost?... as I did above), Spring Boot will go ahead and instantiate one for you.

I did also read this in the documentation:

Spring Boot can also configure a ConnectionFactory when it detects that ActiveMQ is available on the classpath. If the broker is present, an embedded broker is started and configured automatically (as long as no broker URL is specified through configuration).

But in my opinion, it is not well spelled out (but it did get me thinking in the right direction though).

Please, do let know if you had different findings or if my conclusion isn't right. Thanks!!!

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