How to set ActiveMQ port in Spring Boot?

2020-07-11 07:43发布

问题:

I have two Spring Boot applications running on one server. Both use embedded ActiveMQ JMS. I want to have separate JMS instance for each application. How could I set the port for each of them? Is there any property like spring.activemq.port? When I run second application I get the following expected error:

Failed to start JMX connector Cannot bind to URL [rmi://localhost:1099/jmxrmi]: javax.naming.NameAlreadyBoundException: jmxrmi [Root exception is java.rmi.AlreadyBoundException: jmxrmi]. Will restart management to re-create JMX connector, trying to remedy this issue.

回答1:

I have same issue, two SpringBoot process and I want to send messages through the ActiveMQ. First I got it working starting another process with the ActiveMQ, and configuring both SpringBoot process into their application.properties files with:

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

Whit this configuration you tell Springboot to connect to a external ActiveMq service. This works, but then I need to first start the ActiveMQ and after my Springboot process. In some page I have read this must be the way to use at production environments.

Another solution is to use the embedded JMS support at one of the SpringBoot process, for this way you need to configure the ActiveMQ broker service listening for connections in one Springboot process. You can do this adding a Broker bean:

@Bean
public BrokerService broker() throws Exception {
    final BrokerService broker = new BrokerService();
    broker.addConnector("tcp://localhost:61616");
    broker.addConnector("vm://localhost");
    broker.setPersistent(false);
    return broker;
}

Now this SpringBoot process with this bean do not need the previous configuration at the application.properties, and this will be the first process to start, in order to have the ActiveMQ listening for other process connections.

The other Springboot process still need to have the configuration at the application.properties in order to connect to the ActiveMq created by the first process.

Hope it helps you. Best regards.



回答2:

You can configure the broker url using the spring.activemq.broker-url property, e.g. set it to spring.activemq.broker-url=tcp://localhost:61616.

For a comprehensive reference of available properties you can check out this reference.



回答3:

spring.activemq.broker-url

Including the port according to spring boot properties