Skip to content

Commit 338c8c4

Browse files
committed
Polish "Allow to configure ActiveMQ Artemis with a broker url"
See gh-24302
1 parent 99b43cb commit 338c8c4

File tree

4 files changed

+70
-39
lines changed

4 files changed

+70
-39
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryFactory.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,6 +44,8 @@
4444
*/
4545
class ArtemisConnectionFactoryFactory {
4646

47+
private static final String DEFAULT_BROKER_URL = "tcp://localhost:61616";
48+
4749
static final String[] EMBEDDED_JMS_CLASSES = { "org.apache.activemq.artemis.jms.server.embedded.EmbeddedJMS",
4850
"org.apache.activemq.artemis.core.server.embedded.EmbeddedActiveMQ" };
4951

@@ -128,27 +130,32 @@ private <T extends ActiveMQConnectionFactory> T createEmbeddedConnectionFactory(
128130

129131
private <T extends ActiveMQConnectionFactory> T createNativeConnectionFactory(Class<T> factoryClass)
130132
throws Exception {
131-
T connectionFactory;
132-
Map<String, Object> params = new HashMap<>();
133-
String url = this.properties.getBrokerUrl();
134-
if (StringUtils.hasText(url)) {
135-
Constructor<T> constructor = factoryClass.getConstructor(String.class);
136-
connectionFactory = constructor.newInstance(url);
133+
T connectionFactory = newNativeConnectionFactory(factoryClass);
134+
String user = this.properties.getUser();
135+
if (StringUtils.hasText(user)) {
136+
connectionFactory.setUser(user);
137+
connectionFactory.setPassword(this.properties.getPassword());
137138
}
138-
else {
139+
return connectionFactory;
140+
}
141+
142+
@SuppressWarnings("deprecation")
143+
private <T extends ActiveMQConnectionFactory> T newNativeConnectionFactory(Class<T> factoryClass) throws Exception {
144+
// Fallback if the broker url is not set
145+
if (!StringUtils.hasText(this.properties.getBrokerUrl()) && StringUtils.hasText(this.properties.getHost())) {
146+
Map<String, Object> params = new HashMap<>();
139147
params.put(TransportConstants.HOST_PROP_NAME, this.properties.getHost());
140148
params.put(TransportConstants.PORT_PROP_NAME, this.properties.getPort());
141149
TransportConfiguration transportConfiguration = new TransportConfiguration(
142150
NettyConnectorFactory.class.getName(), params);
143151
Constructor<T> constructor = factoryClass.getConstructor(boolean.class, TransportConfiguration[].class);
144-
connectionFactory = constructor.newInstance(false, new TransportConfiguration[] { transportConfiguration });
152+
return constructor.newInstance(false, new TransportConfiguration[] { transportConfiguration });
145153
}
146-
String user = this.properties.getUser();
147-
if (StringUtils.hasText(user)) {
148-
connectionFactory.setUser(user);
149-
connectionFactory.setPassword(this.properties.getPassword());
150-
}
151-
return connectionFactory;
154+
String brokerUrl = StringUtils.hasText(this.properties.getBrokerUrl()) ? this.properties.getBrokerUrl()
155+
: DEFAULT_BROKER_URL;
156+
Constructor<T> constructor = factoryClass.getConstructor(String.class);
157+
return constructor.newInstance(brokerUrl);
158+
152159
}
153160

154161
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisProperties.java

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525

2626
import org.springframework.boot.autoconfigure.jms.JmsPoolConnectionFactoryProperties;
2727
import org.springframework.boot.context.properties.ConfigurationProperties;
28+
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
2829
import org.springframework.boot.context.properties.NestedConfigurationProperty;
2930

3031
/**
@@ -44,25 +45,19 @@ public class ArtemisProperties {
4445
private ArtemisMode mode;
4546

4647
/**
47-
* Artemis broker host.
48-
*
49-
* This property is deprecated. Use <code>brokerUrl</code> instead.
48+
* Artemis broker port.
5049
*/
51-
@Deprecated
52-
private String host = "localhost";
50+
private String brokerUrl;
5351

5452
/**
55-
* Artemis broker port.
56-
*
57-
* This property is deprecated. Use <code>brokerUrl</code> instead.
53+
* Artemis broker host.
5854
*/
59-
@Deprecated
60-
private int port = 61616;
55+
private String host;
6156

6257
/**
6358
* Artemis broker port.
6459
*/
65-
private String brokerUrl = "tcp://localhost:61616";
60+
private int port = 61616;
6661

6762
/**
6863
* Login user of the broker.
@@ -87,30 +82,44 @@ public void setMode(ArtemisMode mode) {
8782
this.mode = mode;
8883
}
8984

85+
public String getBrokerUrl() {
86+
return this.brokerUrl;
87+
}
88+
89+
public void setBrokerUrl(String brokerUrl) {
90+
this.brokerUrl = brokerUrl;
91+
}
92+
93+
/**
94+
* Return the host of the broker.
95+
* @return the host
96+
*/
97+
@Deprecated
98+
@DeprecatedConfigurationProperty(replacement = "spring.artemis.broker-url")
9099
public String getHost() {
91100
return this.host;
92101
}
93102

103+
@Deprecated
94104
public void setHost(String host) {
95105
this.host = host;
96106
}
97107

108+
/**
109+
* Return the port of the broker.
110+
* @return the port
111+
*/
112+
@Deprecated
113+
@DeprecatedConfigurationProperty(replacement = "spring.artemis.broker-url")
98114
public int getPort() {
99115
return this.port;
100116
}
101117

118+
@Deprecated
102119
public void setPort(int port) {
103120
this.port = port;
104121
}
105122

106-
public String getBrokerUrl() {
107-
return this.brokerUrl;
108-
}
109-
110-
public void setBrokerUrl(String brokerUrl) {
111-
this.brokerUrl = brokerUrl;
112-
}
113-
114123
public String getUser() {
115124
return this.user;
116125
}

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,10 @@
352352
"description": "JMX name of the application admin MBean.",
353353
"defaultValue": "org.springframework.boot:type=Admin,name=SpringApplication"
354354
},
355+
{
356+
"name": "spring.artemis.broker-url",
357+
"defaultValue": "tcp://localhost:61616"
358+
},
355359
{
356360
"name": "spring.artemis.pool.maximum-active-session-per-connection",
357361
"deprecation": {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,31 @@ void nativeConnectionFactory() {
124124
}
125125

126126
@Test
127+
void nativeConnectionFactoryCustomBrokerUrl() {
128+
this.contextRunner.withUserConfiguration(EmptyConfiguration.class)
129+
.withPropertyValues("spring.artemis.mode:native", "spring.artemis.broker-url:tcp://192.168.1.144:9876")
130+
.run((context) -> assertNettyConnectionFactory(
131+
getActiveMQConnectionFactory(getConnectionFactory(context)), "192.168.1.144", 9876));
132+
}
133+
134+
@Test
135+
@Deprecated
127136
void nativeConnectionFactoryCustomHost() {
128137
this.contextRunner.withUserConfiguration(EmptyConfiguration.class)
129138
.withPropertyValues("spring.artemis.mode:native", "spring.artemis.host:192.168.1.144",
130-
"spring.artemis.port:9876", "spring.artemis.broker-url: ")
139+
"spring.artemis.port:9876")
131140
.run((context) -> assertNettyConnectionFactory(
132141
getActiveMQConnectionFactory(getConnectionFactory(context)), "192.168.1.144", 9876));
133142
}
134143

135144
@Test
136-
void nativeConnectionFactoryCustomUrl() {
145+
@Deprecated
146+
void nativeConnectionFactoryCustomBrokerUrlAndHost() {
137147
this.contextRunner.withUserConfiguration(EmptyConfiguration.class)
138-
.withPropertyValues("spring.artemis.mode:native", "spring.artemis.broker-url:tcp://192.168.1.144:9876")
148+
.withPropertyValues("spring.artemis.mode:native", "spring.artemis.host:192.168.1.144",
149+
"spring.artemis.port:9876", "spring.artemis.broker-url=tcp://192.168.1.221:6543")
139150
.run((context) -> assertNettyConnectionFactory(
140-
getActiveMQConnectionFactory(getConnectionFactory(context)), "192.168.1.144", 9876));
151+
getActiveMQConnectionFactory(getConnectionFactory(context)), "192.168.1.221", 6543));
141152
}
142153

143154
@Test

0 commit comments

Comments
 (0)