From 4e31d2041ad54e32b131c8b2c33211f724a2999e Mon Sep 17 00:00:00 2001 From: Arlo O'Keeffe Date: Fri, 10 Nov 2017 11:03:38 +0100 Subject: [PATCH 1/2] 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 17b4f4fcde9..5343f65a494 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 d3e5458a8b6..76699ae5a5c 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 71b6545b1e6..0b6f39acf3c 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) From 6a4a0e3f2926826d19a6d425c50de5e6fcd0cd3e Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 12 Dec 2017 09:08:22 +0100 Subject: [PATCH 2/2] Polish "Make RabbitTemplate exchange and routingKey configurable" Closes gh-10978 --- .../amqp/RabbitAutoConfiguration.java | 1 - .../boot/autoconfigure/amqp/RabbitProperties.java | 13 ------------- .../amqp/RabbitAutoConfigurationTests.java | 14 ++++++++++++-- .../asciidoc/appendix-application-properties.adoc | 2 ++ 4 files changed, 14 insertions(+), 16 deletions(-) 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 5343f65a494..a818a76680f 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 @@ -190,7 +190,6 @@ public class RabbitAutoConfiguration { } 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 76699ae5a5c..305ee0fcc2e 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 @@ -713,11 +713,6 @@ public class RabbitProperties { */ private String routingKey = ""; - /** - * Enable transactional channels. - */ - private boolean channelTransacted; - public Retry getRetry() { return this.retry; } @@ -762,14 +757,6 @@ public class RabbitProperties { 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 0b6f39acf3c..86437c4a253 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 @@ -113,8 +113,6 @@ public class RabbitAutoConfigurationTests { .isEqualTo(defaultRabbitTemplate.getRoutingKey()); assertThat(rabbitTemplate.getExchange()) .isEqualTo(defaultRabbitTemplate.getExchange()); - assertThat(rabbitTemplate.isChannelTransacted()) - .isEqualTo(defaultRabbitTemplate.isChannelTransacted()); }); } @@ -239,6 +237,18 @@ public class RabbitAutoConfigurationTests { }); } + @Test + public void testRabbitTemplateExchangeAndRoutingKey() { + this.contextRunner.withUserConfiguration(TestConfiguration.class) + .withPropertyValues("spring.rabbitmq.template.exchange:my-exchange", + "spring.rabbitmq.template.routing-key:my-routing-key") + .run((context) -> { + RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class); + assertThat(rabbitTemplate.getExchange()).isEqualTo("my-exchange"); + assertThat(rabbitTemplate.getRoutingKey()).isEqualTo("my-routing-key"); + }); + } + @Test public void testRabbitTemplateMandatory() { this.contextRunner.withUserConfiguration(TestConfiguration.class) diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index bea1dd81f8b..a6afafdc394 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -1058,6 +1058,7 @@ content into your application. Rather, pick only the properties that you need. spring.rabbitmq.ssl.trust-store-password= # Password used to access the trust store. spring.rabbitmq.ssl.trust-store-type=JKS # Trust store type. spring.rabbitmq.ssl.algorithm= # SSL algorithm to use. By default, configured by the Rabbit client library. + spring.rabbitmq.template.exchange= # Name of the default exchange to use for send operations. spring.rabbitmq.template.mandatory=false # Whether to enable mandatory messages. spring.rabbitmq.template.receive-timeout=0 # Timeout for `receive()` methods. spring.rabbitmq.template.reply-timeout=5000 # Timeout for `sendAndReceive()` methods. @@ -1066,6 +1067,7 @@ content into your application. Rather, pick only the properties that you need. spring.rabbitmq.template.retry.max-attempts=3 # Maximum number of attempts to publish a message. spring.rabbitmq.template.retry.max-interval=10000 # Maximum number of attempts to publish a message. spring.rabbitmq.template.retry.multiplier=1.0 # Multiplier to apply to the previous publishing retry interval. + spring.rabbitmq.template.routing-key= # Value of a default routing key to use for send operations. spring.rabbitmq.username= # Login user to authenticate to the broker. spring.rabbitmq.virtual-host= # Virtual host to use when connecting to the broker.