Allow to configure ActiveMQ Artemis with a broker url

See gh-24302
This commit is contained in:
Justin Bertram
2020-11-30 12:48:33 -06:00
committed by Stephane Nicoll
parent 01cfb9e79c
commit 99b43cb690
4 changed files with 54 additions and 14 deletions

View File

@@ -40,6 +40,7 @@ import org.springframework.util.StringUtils;
* @author Eddú Meléndez
* @author Phillip Webb
* @author Stephane Nicoll
* @author Justin Bertram
*/
class ArtemisConnectionFactoryFactory {
@@ -127,13 +128,21 @@ class ArtemisConnectionFactoryFactory {
private <T extends ActiveMQConnectionFactory> T createNativeConnectionFactory(Class<T> factoryClass)
throws Exception {
T connectionFactory;
Map<String, Object> params = new HashMap<>();
params.put(TransportConstants.HOST_PROP_NAME, this.properties.getHost());
params.put(TransportConstants.PORT_PROP_NAME, this.properties.getPort());
TransportConfiguration transportConfiguration = new TransportConfiguration(
NettyConnectorFactory.class.getName(), params);
Constructor<T> constructor = factoryClass.getConstructor(boolean.class, TransportConfiguration[].class);
T connectionFactory = constructor.newInstance(false, new TransportConfiguration[] { transportConfiguration });
String url = this.properties.getBrokerUrl();
if (StringUtils.hasText(url)) {
Constructor<T> constructor = factoryClass.getConstructor(String.class);
connectionFactory = constructor.newInstance(url);
}
else {
params.put(TransportConstants.HOST_PROP_NAME, this.properties.getHost());
params.put(TransportConstants.PORT_PROP_NAME, this.properties.getPort());
TransportConfiguration transportConfiguration = new TransportConfiguration(
NettyConnectorFactory.class.getName(), params);
Constructor<T> constructor = factoryClass.getConstructor(boolean.class, TransportConfiguration[].class);
connectionFactory = constructor.newInstance(false, new TransportConfiguration[] { transportConfiguration });
}
String user = this.properties.getUser();
if (StringUtils.hasText(user)) {
connectionFactory.setUser(user);

View File

@@ -32,6 +32,7 @@ import org.springframework.boot.context.properties.NestedConfigurationProperty;
*
* @author Eddú Meléndez
* @author Stephane Nicoll
* @author Justin Bertram
* @since 1.3.0
*/
@ConfigurationProperties(prefix = "spring.artemis")
@@ -44,14 +45,25 @@ public class ArtemisProperties {
/**
* Artemis broker host.
*
* This property is deprecated. Use <code>brokerUrl</code> instead.
*/
@Deprecated
private String host = "localhost";
/**
* Artemis broker port.
*
* This property is deprecated. Use <code>brokerUrl</code> instead.
*/
@Deprecated
private int port = 61616;
/**
* Artemis broker port.
*/
private String brokerUrl = "tcp://localhost:61616";
/**
* Login user of the broker.
*/
@@ -91,6 +103,14 @@ public class ArtemisProperties {
this.port = port;
}
public String getBrokerUrl() {
return this.brokerUrl;
}
public void setBrokerUrl(String brokerUrl) {
this.brokerUrl = brokerUrl;
}
public String getUser() {
return this.user;
}

View File

@@ -127,7 +127,15 @@ class ArtemisAutoConfigurationTests {
void nativeConnectionFactoryCustomHost() {
this.contextRunner.withUserConfiguration(EmptyConfiguration.class)
.withPropertyValues("spring.artemis.mode:native", "spring.artemis.host:192.168.1.144",
"spring.artemis.port:9876")
"spring.artemis.port:9876", "spring.artemis.broker-url: ")
.run((context) -> assertNettyConnectionFactory(
getActiveMQConnectionFactory(getConnectionFactory(context)), "192.168.1.144", 9876));
}
@Test
void nativeConnectionFactoryCustomUrl() {
this.contextRunner.withUserConfiguration(EmptyConfiguration.class)
.withPropertyValues("spring.artemis.mode:native", "spring.artemis.broker-url:tcp://192.168.1.144:9876")
.run((context) -> assertNettyConnectionFactory(
getActiveMQConnectionFactory(getConnectionFactory(context)), "192.168.1.144", 9876));
}
@@ -377,7 +385,11 @@ class ArtemisAutoConfigurationTests {
TransportConfiguration transportConfig = getSingleTransportConfiguration(connectionFactory);
assertThat(transportConfig.getFactoryClassName()).isEqualTo(NettyConnectorFactory.class.getName());
assertThat(transportConfig.getParams().get("host")).isEqualTo(host);
assertThat(transportConfig.getParams().get("port")).isEqualTo(port);
Object transportConfigPort = transportConfig.getParams().get("port");
if (transportConfigPort instanceof String) {
transportConfigPort = Integer.parseInt((String) transportConfigPort);
}
assertThat(transportConfigPort).isEqualTo(port);
return transportConfig;
}