pull/64/head
Dave Syer 11 years ago
parent 941d163709
commit 9bb6e0f497

@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.amqp;
import org.springframework.amqp.core.AmqpAdmin; import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin; import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -32,7 +33,7 @@ import org.springframework.context.annotation.Configuration;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for {@link RabbitTemplate}. * {@link EnableAutoConfiguration Auto-configuration} for {@link RabbitTemplate}.
* *
* @author Greg Turnquist * @author Greg Turnquist
*/ */
@Configuration @Configuration
@ -40,101 +41,102 @@ import org.springframework.context.annotation.Configuration;
@EnableConfigurationProperties @EnableConfigurationProperties
public class RabbitTemplateAutoConfiguration { public class RabbitTemplateAutoConfiguration {
@Bean @Bean
@ConditionalOnExpression("${spring.rabbitmq.dynamic:true}") @ConditionalOnExpression("${spring.rabbitmq.dynamic:true}")
@ConditionalOnMissingBean(AmqpAdmin.class) @ConditionalOnMissingBean(AmqpAdmin.class)
public AmqpAdmin amqpAdmin(CachingConnectionFactory connectionFactory) { public AmqpAdmin amqpAdmin(CachingConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory); return new RabbitAdmin(connectionFactory);
} }
@Configuration @Configuration
@ConditionalOnMissingBean(RabbitTemplate.class) @ConditionalOnMissingBean(RabbitTemplate.class)
protected static class RabbitTemplateCreator { protected static class RabbitTemplateCreator {
@Autowired @Autowired
CachingConnectionFactory connectionFactory; CachingConnectionFactory connectionFactory;
@Bean @Bean
public RabbitTemplate rabbitTemplate() { public RabbitTemplate rabbitTemplate() {
return new RabbitTemplate(this.connectionFactory); return new RabbitTemplate(this.connectionFactory);
} }
} }
@Configuration @Configuration
@ConditionalOnMissingBean(CachingConnectionFactory.class) @ConditionalOnMissingBean(ConnectionFactory.class)
@EnableConfigurationProperties(RabbitConnectionFactoryProperties.class) @EnableConfigurationProperties(RabbitConnectionFactoryProperties.class)
protected static class RabbitConnectionFactoryCreator { protected static class RabbitConnectionFactoryCreator {
@Autowired @Autowired
private RabbitConnectionFactoryProperties config; private RabbitConnectionFactoryProperties config;
@Bean @Bean
public CachingConnectionFactory connectionFactory() { public CachingConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(this.config.getHost()); CachingConnectionFactory connectionFactory = new CachingConnectionFactory(
connectionFactory.setPort(this.config.getPort()); this.config.getHost());
if (this.config.getUsername() != null) { connectionFactory.setPort(this.config.getPort());
connectionFactory.setUsername(this.config.getUsername()); if (this.config.getUsername() != null) {
} connectionFactory.setUsername(this.config.getUsername());
if (this.config.getPassword() != null) { }
connectionFactory.setPassword(this.config.getPassword()); if (this.config.getPassword() != null) {
} connectionFactory.setPassword(this.config.getPassword());
return connectionFactory; }
} return connectionFactory;
} }
}
@ConfigurationProperties(name = "spring.rabbitmq") @ConfigurationProperties(name = "spring.rabbitmq")
public static class RabbitConnectionFactoryProperties { public static class RabbitConnectionFactoryProperties {
private String host = "localhost"; private String host = "localhost";
private int port = 5672; private int port = 5672;
private String username; private String username;
private String password; private String password;
private boolean dynamic = true; private boolean dynamic = true;
public String getHost() { public String getHost() {
return host; return this.host;
} }
public void setHost(String host) { public void setHost(String host) {
this.host = host; this.host = host;
} }
public int getPort() { public int getPort() {
return port; return this.port;
} }
public void setPort(int port) { public void setPort(int port) {
this.port = port; this.port = port;
} }
public String getUsername() { public String getUsername() {
return username; return this.username;
} }
public void setUsername(String username) { public void setUsername(String username) {
this.username = username; this.username = username;
} }
public String getPassword() { public String getPassword() {
return password; return this.password;
} }
public void setPassword(String password) { public void setPassword(String password) {
this.password = password; this.password = password;
} }
public boolean isDynamic() { public boolean isDynamic() {
return dynamic; return this.dynamic;
} }
public void setDynamic(boolean dynamic) { public void setDynamic(boolean dynamic) {
this.dynamic = dynamic; this.dynamic = dynamic;
} }
} }
} }

@ -27,80 +27,90 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import static junit.framework.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static junit.framework.Assert.fail; import static org.junit.Assert.fail;
/** /**
* Tests for {@link RabbitTemplateAutoConfiguration}. * Tests for {@link RabbitTemplateAutoConfiguration}.
* *
* @author Greg Turnquist * @author Greg Turnquist
*/ */
public class RabbitTemplateAutoconfigurationTests { public class RabbitTemplateAutoconfigurationTests {
private AnnotationConfigApplicationContext context; private AnnotationConfigApplicationContext context;
@Test @Test
public void testDefaultRabbitTemplate() { public void testDefaultRabbitTemplate() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration.class, RabbitTemplateAutoConfiguration.class); this.context.register(TestConfiguration.class,
this.context.refresh(); RabbitTemplateAutoConfiguration.class);
RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class); this.context.refresh();
CachingConnectionFactory connectionFactory = this.context.getBean(CachingConnectionFactory.class); RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
RabbitAdmin amqpAdmin = this.context.getBean(RabbitAdmin.class); CachingConnectionFactory connectionFactory = this.context
assertNotNull(rabbitTemplate); .getBean(CachingConnectionFactory.class);
assertNotNull(connectionFactory); RabbitAdmin amqpAdmin = this.context.getBean(RabbitAdmin.class);
assertNotNull(amqpAdmin); assertNotNull(rabbitTemplate);
assertEquals(rabbitTemplate.getConnectionFactory(), connectionFactory); assertNotNull(connectionFactory);
assertEquals(connectionFactory.getHost(), "localhost"); assertNotNull(amqpAdmin);
} assertEquals(rabbitTemplate.getConnectionFactory(), connectionFactory);
assertEquals(connectionFactory.getHost(), "localhost");
}
@Test @Test
public void testRabbitTemplateWithOverrides() { public void testRabbitTemplateWithOverrides() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration.class, RabbitTemplateAutoConfiguration.class); this.context.register(TestConfiguration.class,
TestUtils.addEnviroment(this.context, "spring.rabbitmq.host:remote-server", RabbitTemplateAutoConfiguration.class);
"spring.rabbitmq.port:9000", "spring.rabbitmq.username:alice", "spring.rabbitmq.password:secret"); TestUtils.addEnviroment(this.context, "spring.rabbitmq.host:remote-server",
this.context.refresh(); "spring.rabbitmq.port:9000", "spring.rabbitmq.username:alice",
CachingConnectionFactory connectionFactory = this.context.getBean(CachingConnectionFactory.class); "spring.rabbitmq.password:secret");
assertEquals(connectionFactory.getHost(), "remote-server"); this.context.refresh();
assertEquals(connectionFactory.getPort(), 9000); CachingConnectionFactory connectionFactory = this.context
} .getBean(CachingConnectionFactory.class);
assertEquals(connectionFactory.getHost(), "remote-server");
assertEquals(connectionFactory.getPort(), 9000);
}
@Test @Test
public void testConnectionFactoryBackoff() { public void testConnectionFactoryBackoff() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration2.class, RabbitTemplateAutoConfiguration.class); this.context.register(TestConfiguration2.class,
this.context.refresh(); RabbitTemplateAutoConfiguration.class);
RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class); this.context.refresh();
CachingConnectionFactory connectionFactory = this.context.getBean(CachingConnectionFactory.class); RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
assertEquals(rabbitTemplate.getConnectionFactory(), connectionFactory); CachingConnectionFactory connectionFactory = this.context
assertEquals(connectionFactory.getHost(), "otherserver"); .getBean(CachingConnectionFactory.class);
assertEquals(connectionFactory.getPort(), 8001); assertEquals(rabbitTemplate.getConnectionFactory(), connectionFactory);
} assertEquals(connectionFactory.getHost(), "otherserver");
assertEquals(connectionFactory.getPort(), 8001);
}
@Test @Test
public void testStaticQueues() { public void testStaticQueues() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration.class, RabbitTemplateAutoConfiguration.class); this.context.register(TestConfiguration.class,
TestUtils.addEnviroment(this.context, "spring.rabbitmq.dynamic:false"); RabbitTemplateAutoConfiguration.class);
this.context.refresh(); TestUtils.addEnviroment(this.context, "spring.rabbitmq.dynamic:false");
try { this.context.refresh();
this.context.getBean(AmqpAdmin.class); try {
fail("There should NOT be an AmqpAdmin bean when dynamic is switch to false"); this.context.getBean(AmqpAdmin.class);
} catch (Exception e) {} fail("There should NOT be an AmqpAdmin bean when dynamic is switch to false");
} }
catch (Exception e) {
}
}
@Configuration @Configuration
protected static class TestConfiguration { protected static class TestConfiguration {
} }
@Configuration @Configuration
protected static class TestConfiguration2 { protected static class TestConfiguration2 {
@Bean @Bean
ConnectionFactory aDifferentConnectionFactory() { ConnectionFactory aDifferentConnectionFactory() {
return new CachingConnectionFactory("otherserver", 8001); return new CachingConnectionFactory("otherserver", 8001);
} }
} }
} }

Loading…
Cancel
Save