mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Add virtual host to Rabbit config
This commit is contained in:
parent
ecc5463a76
commit
276f9b784c
@ -23,7 +23,7 @@ import org.springframework.amqp.rabbit.core.RabbitAdmin;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.amqp.RabbitTemplateAutoConfiguration.RabbitConnectionFactoryProperties;
|
||||
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration.RabbitConnectionFactoryProperties;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
@ -42,7 +42,7 @@ import com.rabbitmq.client.Channel;
|
||||
@Configuration
|
||||
@ConditionalOnClass({ RabbitTemplate.class, Channel.class })
|
||||
@EnableConfigurationProperties(RabbitConnectionFactoryProperties.class)
|
||||
public class RabbitTemplateAutoConfiguration {
|
||||
public class RabbitAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnExpression("${spring.rabbitmq.dynamic:true}")
|
||||
@ -76,6 +76,9 @@ public class RabbitTemplateAutoConfiguration {
|
||||
if (config.getPassword() != null) {
|
||||
connectionFactory.setPassword(config.getPassword());
|
||||
}
|
||||
if (config.getVirtualHost() != null) {
|
||||
connectionFactory.setVirtualHost(config.getVirtualHost());
|
||||
}
|
||||
return connectionFactory;
|
||||
}
|
||||
}
|
||||
@ -91,6 +94,8 @@ public class RabbitTemplateAutoConfiguration {
|
||||
|
||||
private String password;
|
||||
|
||||
private String virtualHost;
|
||||
|
||||
private boolean dynamic = true;
|
||||
|
||||
public String getHost() {
|
||||
@ -133,5 +138,16 @@ public class RabbitTemplateAutoConfiguration {
|
||||
this.dynamic = dynamic;
|
||||
}
|
||||
|
||||
public String getVirtualHost() {
|
||||
return this.virtualHost;
|
||||
}
|
||||
|
||||
public void setVirtualHost(String virtualHost) {
|
||||
while (virtualHost.startsWith("/") && virtualHost.length() > 0) {
|
||||
virtualHost = virtualHost.substring(1);
|
||||
}
|
||||
this.virtualHost = "/" + virtualHost;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -32,19 +32,18 @@ import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Tests for {@link RabbitTemplateAutoConfiguration}.
|
||||
* Tests for {@link RabbitAutoConfiguration}.
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
*/
|
||||
public class RabbitTemplateAutoconfigurationTests {
|
||||
public class RabbitAutoconfigurationTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void testDefaultRabbitTemplate() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfiguration.class,
|
||||
RabbitTemplateAutoConfiguration.class);
|
||||
this.context.register(TestConfiguration.class, RabbitAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
|
||||
CachingConnectionFactory connectionFactory = this.context
|
||||
@ -60,23 +59,55 @@ public class RabbitTemplateAutoconfigurationTests {
|
||||
@Test
|
||||
public void testRabbitTemplateWithOverrides() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfiguration.class,
|
||||
RabbitTemplateAutoConfiguration.class);
|
||||
this.context.register(TestConfiguration.class, RabbitAutoConfiguration.class);
|
||||
TestUtils.addEnviroment(this.context, "spring.rabbitmq.host:remote-server",
|
||||
"spring.rabbitmq.port:9000", "spring.rabbitmq.username:alice",
|
||||
"spring.rabbitmq.password:secret");
|
||||
"spring.rabbitmq.password:secret", "spring.rabbitmq.virtual_host:/vhost");
|
||||
this.context.refresh();
|
||||
CachingConnectionFactory connectionFactory = this.context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
assertEquals(connectionFactory.getHost(), "remote-server");
|
||||
assertEquals(connectionFactory.getPort(), 9000);
|
||||
assertEquals(connectionFactory.getVirtualHost(), "/vhost");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRabbitTemplateEmptyVirtualHost() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfiguration.class, RabbitAutoConfiguration.class);
|
||||
TestUtils.addEnviroment(this.context, "spring.rabbitmq.virtual_host:");
|
||||
this.context.refresh();
|
||||
CachingConnectionFactory connectionFactory = this.context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
assertEquals(connectionFactory.getVirtualHost(), "/");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRabbitTemplateVirtualHostMissingSlash() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfiguration.class, RabbitAutoConfiguration.class);
|
||||
TestUtils.addEnviroment(this.context, "spring.rabbitmq.virtual_host:foo");
|
||||
this.context.refresh();
|
||||
CachingConnectionFactory connectionFactory = this.context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
assertEquals(connectionFactory.getVirtualHost(), "/foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRabbitTemplateDefaultVirtualHost() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfiguration.class, RabbitAutoConfiguration.class);
|
||||
TestUtils.addEnviroment(this.context, "spring.rabbitmq.virtual_host:/");
|
||||
this.context.refresh();
|
||||
CachingConnectionFactory connectionFactory = this.context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
assertEquals(connectionFactory.getVirtualHost(), "/");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConnectionFactoryBackoff() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfiguration2.class,
|
||||
RabbitTemplateAutoConfiguration.class);
|
||||
this.context.register(TestConfiguration2.class, RabbitAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
|
||||
CachingConnectionFactory connectionFactory = this.context
|
||||
@ -89,8 +120,7 @@ public class RabbitTemplateAutoconfigurationTests {
|
||||
@Test
|
||||
public void testStaticQueues() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfiguration.class,
|
||||
RabbitTemplateAutoConfiguration.class);
|
||||
this.context.register(TestConfiguration.class, RabbitAutoConfiguration.class);
|
||||
TestUtils.addEnviroment(this.context, "spring.rabbitmq.dynamic:false");
|
||||
this.context.refresh();
|
||||
try {
|
Loading…
Reference in New Issue
Block a user