Skip to content

Commit 769b5f0

Browse files
committed
Merge pull request #24302 from jbertram
* pr/24302: Polish "Allow to configure ActiveMQ Artemis with a broker url" Allow to configure ActiveMQ Artemis with a broker url Closes gh-24302
2 parents 01cfb9e + 338c8c4 commit 769b5f0

File tree

5 files changed

+88
-17
lines changed

5 files changed

+88
-17
lines changed

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

Lines changed: 24 additions & 8 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.
@@ -40,9 +40,12 @@
4040
* @author Eddú Meléndez
4141
* @author Phillip Webb
4242
* @author Stephane Nicoll
43+
* @author Justin Bertram
4344
*/
4445
class ArtemisConnectionFactoryFactory {
4546

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

@@ -127,13 +130,7 @@ private <T extends ActiveMQConnectionFactory> T createEmbeddedConnectionFactory(
127130

128131
private <T extends ActiveMQConnectionFactory> T createNativeConnectionFactory(Class<T> factoryClass)
129132
throws Exception {
130-
Map<String, Object> params = new HashMap<>();
131-
params.put(TransportConstants.HOST_PROP_NAME, this.properties.getHost());
132-
params.put(TransportConstants.PORT_PROP_NAME, this.properties.getPort());
133-
TransportConfiguration transportConfiguration = new TransportConfiguration(
134-
NettyConnectorFactory.class.getName(), params);
135-
Constructor<T> constructor = factoryClass.getConstructor(boolean.class, TransportConfiguration[].class);
136-
T connectionFactory = constructor.newInstance(false, new TransportConfiguration[] { transportConfiguration });
133+
T connectionFactory = newNativeConnectionFactory(factoryClass);
137134
String user = this.properties.getUser();
138135
if (StringUtils.hasText(user)) {
139136
connectionFactory.setUser(user);
@@ -142,4 +139,23 @@ private <T extends ActiveMQConnectionFactory> T createNativeConnectionFactory(Cl
142139
return connectionFactory;
143140
}
144141

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<>();
147+
params.put(TransportConstants.HOST_PROP_NAME, this.properties.getHost());
148+
params.put(TransportConstants.PORT_PROP_NAME, this.properties.getPort());
149+
TransportConfiguration transportConfiguration = new TransportConfiguration(
150+
NettyConnectorFactory.class.getName(), params);
151+
Constructor<T> constructor = factoryClass.getConstructor(boolean.class, TransportConfiguration[].class);
152+
return constructor.newInstance(false, new TransportConfiguration[] { transportConfiguration });
153+
}
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+
159+
}
160+
145161
}

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

Lines changed: 31 additions & 2 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,13 +25,15 @@
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
/**
3132
* Configuration properties for Artemis.
3233
*
3334
* @author Eddú Meléndez
3435
* @author Stephane Nicoll
36+
* @author Justin Bertram
3537
* @since 1.3.0
3638
*/
3739
@ConfigurationProperties(prefix = "spring.artemis")
@@ -42,10 +44,15 @@ public class ArtemisProperties {
4244
*/
4345
private ArtemisMode mode;
4446

47+
/**
48+
* Artemis broker port.
49+
*/
50+
private String brokerUrl;
51+
4552
/**
4653
* Artemis broker host.
4754
*/
48-
private String host = "localhost";
55+
private String host;
4956

5057
/**
5158
* Artemis broker port.
@@ -75,18 +82,40 @@ public void setMode(ArtemisMode mode) {
7582
this.mode = mode;
7683
}
7784

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")
7899
public String getHost() {
79100
return this.host;
80101
}
81102

103+
@Deprecated
82104
public void setHost(String host) {
83105
this.host = host;
84106
}
85107

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

118+
@Deprecated
90119
public void setPort(int port) {
91120
this.port = port;
92121
}

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: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ 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",
@@ -132,6 +141,16 @@ void nativeConnectionFactoryCustomHost() {
132141
getActiveMQConnectionFactory(getConnectionFactory(context)), "192.168.1.144", 9876));
133142
}
134143

144+
@Test
145+
@Deprecated
146+
void nativeConnectionFactoryCustomBrokerUrlAndHost() {
147+
this.contextRunner.withUserConfiguration(EmptyConfiguration.class)
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")
150+
.run((context) -> assertNettyConnectionFactory(
151+
getActiveMQConnectionFactory(getConnectionFactory(context)), "192.168.1.221", 6543));
152+
}
153+
135154
@Test
136155
void nativeConnectionFactoryCredentials() {
137156
this.contextRunner.withUserConfiguration(EmptyConfiguration.class)
@@ -377,7 +396,11 @@ private TransportConfiguration assertNettyConnectionFactory(ActiveMQConnectionFa
377396
TransportConfiguration transportConfig = getSingleTransportConfiguration(connectionFactory);
378397
assertThat(transportConfig.getFactoryClassName()).isEqualTo(NettyConnectorFactory.class.getName());
379398
assertThat(transportConfig.getParams().get("host")).isEqualTo(host);
380-
assertThat(transportConfig.getParams().get("port")).isEqualTo(port);
399+
Object transportConfigPort = transportConfig.getParams().get("port");
400+
if (transportConfigPort instanceof String) {
401+
transportConfigPort = Integer.parseInt((String) transportConfigPort);
402+
}
403+
assertThat(transportConfigPort).isEqualTo(port);
381404
return transportConfig;
382405
}
383406

spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5739,25 +5739,24 @@ By default, ActiveMQ creates a destination if it does not yet exist so that dest
57395739

57405740

57415741
[[boot-features-artemis]]
5742-
==== Artemis Support
5743-
Spring Boot can auto-configure a `ConnectionFactory` when it detects that https://activemq.apache.org/components/artemis/[Artemis] is available on the classpath.
5742+
==== ActiveMQ Artemis Support
5743+
Spring Boot can auto-configure a `ConnectionFactory` when it detects that https://activemq.apache.org/components/artemis/[ActiveMQ Artemis] is available on the classpath.
57445744
If the broker is present, an embedded broker is automatically started and configured (unless the mode property has been explicitly set).
57455745
The supported modes are `embedded` (to make explicit that an embedded broker is required and that an error should occur if the broker is not available on the classpath) and `native` (to connect to a broker using the `netty` transport protocol).
57465746
When the latter is configured, Spring Boot configures a `ConnectionFactory` that connects to a broker running on the local machine with the default settings.
57475747

5748-
NOTE: If you use `spring-boot-starter-artemis`, the necessary dependencies to connect to an existing Artemis instance are provided, as well as the Spring infrastructure to integrate with JMS.
5748+
NOTE: If you use `spring-boot-starter-artemis`, the necessary dependencies to connect to an existing ActiveMQ Artemis instance are provided, as well as the Spring infrastructure to integrate with JMS.
57495749
Adding `org.apache.activemq:artemis-jms-server` to your application lets you use embedded mode.
57505750

5751-
Artemis configuration is controlled by external configuration properties in `+spring.artemis.*+`.
5751+
ActiveMQ Artemis configuration is controlled by external configuration properties in `+spring.artemis.*+`.
57525752
For example, you might declare the following section in `application.properties`:
57535753

57545754
[source,yaml,indent=0,configprops,configblocks]
57555755
----
57565756
spring:
57575757
artemis:
57585758
mode: native
5759-
host: "192.168.1.210"
5760-
port: 9876
5759+
broker-url: "tcp://192.168.1.210:9876"
57615760
user: "admin"
57625761
password: "secret"
57635762
----

0 commit comments

Comments
 (0)