Add virtual host support for Rabbit Stream

Add a new property 'spring.rabbitmq.stream.virtual-host' which can be
used to set a custom virtual host for streams.

See gh-37189
pull/37211/head
anessi 1 year ago committed by Moritz Halbritter
parent db73e071cc
commit 320dd0e24e

@ -1206,6 +1206,12 @@ public class RabbitProperties {
*/
private int port = DEFAULT_STREAM_PORT;
/**
* Virtual host of a RabbitMQ instance with the Stream plugin enabled. When not
* set, spring.rabbitmq.virtual-host is used.
*/
private String virtualHost;
/**
* Login user to authenticate to the broker. When not set,
* spring.rabbitmq.username is used.
@ -1239,6 +1245,14 @@ public class RabbitProperties {
this.port = port;
}
public String getVirtualHost() {
return this.virtualHost;
}
public void setVirtualHost(String virtualHost) {
this.virtualHost = virtualHost;
}
public String getUsername() {
return this.username;
}

@ -102,6 +102,10 @@ class RabbitStreamConfiguration {
PropertyMapper map = PropertyMapper.get();
map.from(stream.getHost()).to(builder::host);
map.from(stream.getPort()).to(builder::port);
map.from(stream.getVirtualHost())
.as(withFallback(properties::getVirtualHost))
.whenNonNull()
.to(builder::virtualHost);
map.from(stream.getUsername()).as(withFallback(properties::getUsername)).whenNonNull().to(builder::username);
map.from(stream.getPassword()).as(withFallback(properties::getPassword)).whenNonNull().to(builder::password);
return builder;

@ -143,6 +143,24 @@ class RabbitStreamConfigurationTests {
then(builder).should().host("stream.rabbit.example.com");
}
@Test
void whenStreamVirtualHostIsSetThenEnvironmentUsesCustomVirtualHost() {
EnvironmentBuilder builder = mock(EnvironmentBuilder.class);
RabbitProperties properties = new RabbitProperties();
properties.getStream().setVirtualHost("stream-virtual-host");
RabbitStreamConfiguration.configure(builder, properties);
then(builder).should().virtualHost("stream-virtual-host");
}
@Test
void whenStreamVirtualHostIsNotSetButDefaultVirtualHostIsSetThenEnvironmentUsesDefaultVirtualHost() {
EnvironmentBuilder builder = mock(EnvironmentBuilder.class);
RabbitProperties properties = new RabbitProperties();
properties.setVirtualHost("default-virtual-host");
RabbitStreamConfiguration.configure(builder, properties);
then(builder).should().virtualHost("default-virtual-host");
}
@Test
void whenStreamCredentialsAreNotSetThenEnvironmentUsesRabbitCredentials() {
EnvironmentBuilder builder = mock(EnvironmentBuilder.class);

Loading…
Cancel
Save