Reinstate auto-configuration support for embedded ActiveMQ

This commit restores auto-configuration for using an Embedded broker
with ActiveMQ classic.

Contrary to its 2.7.x version, "spring-boot-starter-activemq" no longer
adds the broker for consistency with Artemis, and to keep the existing
3.x behavior. Rather than "inMemory", a "s.a.embedded.enabled"
property has been reintroduced that matches the name used by Artemis.

The documentation has been updated to mention that the broker
dependency must be added to use it.

Closes gh-38404
This commit is contained in:
Stéphane Nicoll
2024-03-19 14:17:11 -05:00
parent 1f7e7738e8
commit 3651ff87cd
12 changed files with 249 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -37,6 +37,8 @@ import org.springframework.boot.context.properties.NestedConfigurationProperty;
@ConfigurationProperties(prefix = "spring.activemq")
public class ActiveMQProperties {
private static final String DEFAULT_EMBEDDED_BROKER_URL = "vm://localhost?broker.persistent=false";
private static final String DEFAULT_NETWORK_BROKER_URL = "tcp://localhost:61616";
/**
@@ -54,6 +56,8 @@ public class ActiveMQProperties {
*/
private String password;
private final Embedded embedded = new Embedded();
/**
* Time to wait before considering a close complete.
*/
@@ -99,6 +103,10 @@ public class ActiveMQProperties {
this.password = password;
}
public Embedded getEmbedded() {
return this.embedded;
}
public Duration getCloseTimeout() {
return this.closeTimeout;
}
@@ -135,9 +143,32 @@ public class ActiveMQProperties {
if (this.brokerUrl != null) {
return this.brokerUrl;
}
if (this.embedded.isEnabled()) {
return DEFAULT_EMBEDDED_BROKER_URL;
}
return DEFAULT_NETWORK_BROKER_URL;
}
/**
* Configuration for an embedded ActiveMQ broker.
*/
public static class Embedded {
/**
* Whether to enable embedded mode if the ActiveMQ Broker is available.
*/
private boolean enabled = true;
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
public static class Packages {
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -48,14 +48,14 @@ class ActiveMQAutoConfigurationTests {
.withConfiguration(AutoConfigurations.of(ActiveMQAutoConfiguration.class, JmsAutoConfiguration.class));
@Test
void brokerIsLocalhostByDefault() {
void brokerIsEmbeddedByDefault() {
this.contextRunner.withUserConfiguration(EmptyConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(CachingConnectionFactory.class).hasBean("jmsConnectionFactory");
CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class);
assertThat(context.getBean("jmsConnectionFactory")).isSameAs(connectionFactory);
assertThat(connectionFactory.getTargetConnectionFactory()).isInstanceOf(ActiveMQConnectionFactory.class);
assertThat(((ActiveMQConnectionFactory) connectionFactory.getTargetConnectionFactory()).getBrokerURL())
.isEqualTo("tcp://localhost:61616");
.isEqualTo("vm://localhost?broker.persistent=false");
});
}

View File

@@ -31,13 +31,15 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
class ActiveMQPropertiesTests {
private static final String DEFAULT_EMBEDDED_BROKER_URL = "vm://localhost?broker.persistent=false";
private static final String DEFAULT_NETWORK_BROKER_URL = "tcp://localhost:61616";
private final ActiveMQProperties properties = new ActiveMQProperties();
@Test
void getBrokerUrlIsLocalhostByDefault() {
assertThat(this.properties.determineBrokerUrl()).isEqualTo(DEFAULT_NETWORK_BROKER_URL);
void getBrokerUrlIsEmbeddedByDefault() {
assertThat(this.properties.determineBrokerUrl()).isEqualTo(DEFAULT_EMBEDDED_BROKER_URL);
}
@Test
@@ -46,6 +48,19 @@ class ActiveMQPropertiesTests {
assertThat(this.properties.determineBrokerUrl()).isEqualTo("tcp://activemq.example.com:71717");
}
@Test
void getBrokerUrlWithEmbeddedSetToFalse() {
this.properties.getEmbedded().setEnabled(false);
assertThat(this.properties.determineBrokerUrl()).isEqualTo(DEFAULT_NETWORK_BROKER_URL);
}
@Test
void getExplicitBrokerUrlAlwaysWins() {
this.properties.setBrokerUrl("tcp://activemq.example.com:71717");
this.properties.getEmbedded().setEnabled(false);
assertThat(this.properties.determineBrokerUrl()).isEqualTo("tcp://activemq.example.com:71717");
}
@Test
void setTrustAllPackages() {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();