From 4e31d2041ad54e32b131c8b2c33211f724a2999e Mon Sep 17 00:00:00 2001 From: Arlo O'Keeffe Date: Fri, 10 Nov 2017 11:03:38 +0100 Subject: [PATCH] Make RabbitTemplate exchange and routingKey configurable See gh-10978 --- .../amqp/RabbitAutoConfiguration.java | 3 ++ .../autoconfigure/amqp/RabbitProperties.java | 39 +++++++++++++++++++ .../amqp/RabbitAutoConfigurationTests.java | 15 +++++++ 3 files changed, 57 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java index 17b4f4fcde..5343f65a49 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java @@ -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; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java index d3e5458a8b..76699ae5a5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java @@ -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 { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java index 71b6545b1e..0b6f39acf3 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java @@ -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)