Make RabbitTemplate exchange and routingKey configurable

See gh-10978
This commit is contained in:
Arlo O'Keeffe 2017-11-10 11:03:38 +01:00 committed by Stephane Nicoll
parent 4eda29a42e
commit 4e31d2041a
3 changed files with 57 additions and 0 deletions

View File

@ -188,6 +188,9 @@ public class RabbitAutoConfiguration {
if (templateProperties.getReplyTimeout() != null) {
rabbitTemplate.setReplyTimeout(templateProperties.getReplyTimeout());
}
rabbitTemplate.setExchange(templateProperties.getExchange());
rabbitTemplate.setRoutingKey(templateProperties.getRoutingKey());
rabbitTemplate.setChannelTransacted(templateProperties.isChannelTransacted());
return rabbitTemplate;
}

View File

@ -703,6 +703,21 @@ public class RabbitProperties {
*/
private Long replyTimeout;
/**
* Name of the default exchange to use for send operations.
*/
private String exchange = "";
/**
* Value of a default routing key to use for send operations.
*/
private String routingKey = "";
/**
* Enable transactional channels.
*/
private boolean channelTransacted;
public Retry getRetry() {
return this.retry;
}
@ -731,6 +746,30 @@ public class RabbitProperties {
this.replyTimeout = replyTimeout;
}
public String getExchange() {
return this.exchange;
}
public void setExchange(String exchange) {
this.exchange = exchange;
}
public String getRoutingKey() {
return this.routingKey;
}
public void setRoutingKey(String routingKey) {
this.routingKey = routingKey;
}
public boolean isChannelTransacted() {
return this.channelTransacted;
}
public void setChannelTransacted(boolean channelTransacted) {
this.channelTransacted = channelTransacted;
}
}
public static class Retry {

View File

@ -103,6 +103,21 @@ public class RabbitAutoConfigurationTests {
});
}
@Test
public void testDefaultRabbitTemplateConfiguration() {
this.contextRunner.withUserConfiguration(TestConfiguration.class)
.run((context) -> {
RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);
RabbitTemplate defaultRabbitTemplate = new RabbitTemplate();
assertThat(rabbitTemplate.getRoutingKey())
.isEqualTo(defaultRabbitTemplate.getRoutingKey());
assertThat(rabbitTemplate.getExchange())
.isEqualTo(defaultRabbitTemplate.getExchange());
assertThat(rabbitTemplate.isChannelTransacted())
.isEqualTo(defaultRabbitTemplate.isChannelTransacted());
});
}
@Test
public void testConnectionFactoryWithOverrides() {
this.contextRunner.withUserConfiguration(TestConfiguration.class)