Skip to content

Commit 8f13344

Browse files
committed
Implement URL support for ActiveMQ Artemis
See spring-projectsgh-10739
1 parent bd7e89b commit 8f13344

File tree

4 files changed

+56
-17
lines changed

4 files changed

+56
-17
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* @author Eddú Meléndez
4141
* @author Phillip Webb
4242
* @author Stephane Nicoll
43+
* @author Justin Bertram
4344
*/
4445
class ArtemisConnectionFactoryFactory {
4546

@@ -127,13 +128,20 @@ private <T extends ActiveMQConnectionFactory> T createEmbeddedConnectionFactory(
127128

128129
private <T extends ActiveMQConnectionFactory> T createNativeConnectionFactory(Class<T> factoryClass)
129130
throws Exception {
131+
T connectionFactory;
130132
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+
String url = this.properties.getBrokerUrl();
134+
if (StringUtils.hasText(url)) {
135+
Constructor<T> constructor = factoryClass.getConstructor(String.class);
136+
connectionFactory = constructor.newInstance(url);
137+
} else {
138+
params.put(TransportConstants.HOST_PROP_NAME, this.properties.getHost());
139+
params.put(TransportConstants.PORT_PROP_NAME, this.properties.getPort());
140+
TransportConfiguration transportConfiguration = new TransportConfiguration(
141+
NettyConnectorFactory.class.getName(), params);
142+
Constructor<T> constructor = factoryClass.getConstructor(boolean.class, TransportConfiguration[].class);
143+
connectionFactory = constructor.newInstance(false, new TransportConfiguration[] { transportConfiguration });
144+
}
137145
String user = this.properties.getUser();
138146
if (StringUtils.hasText(user)) {
139147
connectionFactory.setUser(user);

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
*
3333
* @author Eddú Meléndez
3434
* @author Stephane Nicoll
35+
* @author Justin Bertram
3536
* @since 1.3.0
3637
*/
3738
@ConfigurationProperties(prefix = "spring.artemis")
@@ -44,14 +45,25 @@ public class ArtemisProperties {
4445

4546
/**
4647
* Artemis broker host.
48+
*
49+
* This property is deprecated. Use <code>brokerUrl</code> instead.
4750
*/
51+
@Deprecated
4852
private String host = "localhost";
4953

5054
/**
5155
* Artemis broker port.
56+
*
57+
* This property is deprecated. Use <code>brokerUrl</code> instead.
5258
*/
59+
@Deprecated
5360
private int port = 61616;
5461

62+
/**
63+
* Artemis broker port.
64+
*/
65+
private String brokerUrl = "tcp://localhost:61616";
66+
5567
/**
5668
* Login user of the broker.
5769
*/
@@ -91,6 +103,14 @@ public void setPort(int port) {
91103
this.port = port;
92104
}
93105

106+
public String getBrokerUrl() {
107+
return this.brokerUrl;
108+
}
109+
110+
public void setBrokerUrl(String brokerUrl) {
111+
this.brokerUrl = brokerUrl;
112+
}
113+
94114
public String getUser() {
95115
return this.user;
96116
}

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,18 @@ void nativeConnectionFactory() {
126126
@Test
127127
void nativeConnectionFactoryCustomHost() {
128128
this.contextRunner.withUserConfiguration(EmptyConfiguration.class)
129-
.withPropertyValues("spring.artemis.mode:native", "spring.artemis.host:192.168.1.144",
130-
"spring.artemis.port:9876")
131-
.run((context) -> assertNettyConnectionFactory(
132-
getActiveMQConnectionFactory(getConnectionFactory(context)), "192.168.1.144", 9876));
129+
.withPropertyValues("spring.artemis.mode:native", "spring.artemis.host:192.168.1.144",
130+
"spring.artemis.port:9876", "spring.artemis.broker-url: ") // unset the url so the default doesn't override
131+
.run((context) -> assertNettyConnectionFactory(
132+
getActiveMQConnectionFactory(getConnectionFactory(context)), "192.168.1.144", 9876));
133+
}
134+
135+
@Test
136+
void nativeConnectionFactoryCustomUrl() {
137+
this.contextRunner.withUserConfiguration(EmptyConfiguration.class)
138+
.withPropertyValues("spring.artemis.mode:native", "spring.artemis.broker-url:tcp://192.168.1.144:9876")
139+
.run((context) -> assertNettyConnectionFactory(
140+
getActiveMQConnectionFactory(getConnectionFactory(context)), "192.168.1.144", 9876));
133141
}
134142

135143
@Test
@@ -377,7 +385,11 @@ private TransportConfiguration assertNettyConnectionFactory(ActiveMQConnectionFa
377385
TransportConfiguration transportConfig = getSingleTransportConfiguration(connectionFactory);
378386
assertThat(transportConfig.getFactoryClassName()).isEqualTo(NettyConnectorFactory.class.getName());
379387
assertThat(transportConfig.getParams().get("host")).isEqualTo(host);
380-
assertThat(transportConfig.getParams().get("port")).isEqualTo(port);
388+
Object transportConfigPort = transportConfig.getParams().get("port"); // may be an int or a String
389+
if (transportConfigPort instanceof String) {
390+
transportConfigPort = Integer.parseInt((String) transportConfigPort);
391+
}
392+
assertThat(transportConfigPort).isEqualTo(port);
381393
return transportConfig;
382394
}
383395

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
@@ -5709,25 +5709,24 @@ By default, ActiveMQ creates a destination if it does not yet exist so that dest
57095709

57105710

57115711
[[boot-features-artemis]]
5712-
==== Artemis Support
5713-
Spring Boot can auto-configure a `ConnectionFactory` when it detects that https://activemq.apache.org/components/artemis/[Artemis] is available on the classpath.
5712+
==== ActiveMQ Artemis Support
5713+
Spring Boot can auto-configure a `ConnectionFactory` when it detects that https://activemq.apache.org/components/artemis/[ActiveMQ Artemis] is available on the classpath.
57145714
If the broker is present, an embedded broker is automatically started and configured (unless the mode property has been explicitly set).
57155715
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).
57165716
When the latter is configured, Spring Boot configures a `ConnectionFactory` that connects to a broker running on the local machine with the default settings.
57175717

5718-
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.
5718+
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.
57195719
Adding `org.apache.activemq:artemis-jms-server` to your application lets you use embedded mode.
57205720

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

57245724
[source,yaml,indent=0,configprops,configblocks]
57255725
----
57265726
spring:
57275727
artemis:
57285728
mode: native
5729-
host: "192.168.1.210"
5730-
port: 9876
5729+
broker-url: "tcp://192.168.1.210:9876"
57315730
user: "admin"
57325731
password: "secret"
57335732
----

0 commit comments

Comments
 (0)