From d7621261b2ab9ab66773e0405995747cc8c86e08 Mon Sep 17 00:00:00 2001 From: artsiom Date: Fri, 27 Jul 2018 20:40:43 +0300 Subject: [PATCH 1/2] Make RabbitTemplate default receive queue configurable See gh-13930 --- .../autoconfigure/amqp/RabbitAutoConfiguration.java | 2 ++ .../boot/autoconfigure/amqp/RabbitProperties.java | 13 +++++++++++++ .../amqp/RabbitAutoConfigurationTests.java | 12 ++++++++++++ .../asciidoc/appendix-application-properties.adoc | 1 + 4 files changed, 28 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 b5610970b7..c9573b4fee 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 @@ -79,6 +79,7 @@ import org.springframework.context.annotation.Import; * @author Stephane Nicoll * @author Gary Russell * @author Phillip Webb + * @author Artsiom Yudovin */ @Configuration @ConditionalOnClass({ RabbitTemplate.class, Channel.class }) @@ -190,6 +191,7 @@ public class RabbitAutoConfiguration { .to(template::setReplyTimeout); map.from(properties::getExchange).to(template::setExchange); map.from(properties::getRoutingKey).to(template::setRoutingKey); + map.from(properties::getQueue).whenNonNull().to(template::setQueue); return template; } 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 abf24b7650..b0e057eb22 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 @@ -37,6 +37,7 @@ import org.springframework.util.StringUtils; * @author Andy Wilkinson * @author Josh Thornhill * @author Gary Russell + * @author Artsiom Yudovin */ @ConfigurationProperties(prefix = "spring.rabbitmq") public class RabbitProperties { @@ -713,6 +714,11 @@ public class RabbitProperties { */ private String routingKey = ""; + /** + * Default queue name that will be used for synchronous receives. + */ + private String queue; + public Retry getRetry() { return this.retry; } @@ -757,6 +763,13 @@ public class RabbitProperties { this.routingKey = routingKey; } + public String getQueue() { + return queue; + } + + public void setQueue(String queue) { + this.queue = queue; + } } 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 b86deccb5b..37ef790aab 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 @@ -62,6 +62,7 @@ import org.springframework.retry.interceptor.MethodInvocationRecoverer; import org.springframework.retry.policy.NeverRetryPolicy; import org.springframework.retry.policy.SimpleRetryPolicy; import org.springframework.retry.support.RetryTemplate; +import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.anyString; @@ -319,6 +320,17 @@ public class RabbitAutoConfigurationTests { }); } + @Test + public void testRabbitTemplateDefaultQueue() { + this.contextRunner.withUserConfiguration(TestConfiguration.class) + .withPropertyValues("spring.rabbitmq.template.queue:default-queue") + .run((context) -> { + RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class); + assertThat(ReflectionTestUtils.getField(rabbitTemplate, "queue")) + .isEqualTo("default-queue"); + }); + } + @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 a149580bdb..83cbfe4cda 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 @@ -1166,6 +1166,7 @@ content into your application. Rather, pick only the properties that you need. spring.rabbitmq.template.retry.max-interval=10000ms # Maximum duration between attempts. spring.rabbitmq.template.retry.multiplier=1 # Multiplier to apply to the previous retry interval. spring.rabbitmq.template.routing-key= # Value of a default routing key to use for send operations. + spring.rabbitmq.template.queue= # Value of a default queue name that will be used for synchronous receives. spring.rabbitmq.username=guest # Login user to authenticate to the broker. spring.rabbitmq.virtual-host= # Virtual host to use when connecting to the broker. From fd85cebfef08805e70149310358f3ee3ab94ec72 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 30 Jul 2018 10:53:24 +0200 Subject: [PATCH 2/2] Polish "Make RabbitTemplate default receive queue configurable" Closes gh-13930 --- .../boot/autoconfigure/amqp/RabbitProperties.java | 6 ++++-- .../src/main/asciidoc/appendix-application-properties.adoc | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) 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 b0e057eb22..02292ebecc 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 @@ -715,7 +715,8 @@ public class RabbitProperties { private String routingKey = ""; /** - * Default queue name that will be used for synchronous receives. + * Name of the default queue to receive messages from when none is specified + * explicitly. */ private String queue; @@ -764,12 +765,13 @@ public class RabbitProperties { } public String getQueue() { - return queue; + return this.queue; } public void setQueue(String queue) { this.queue = queue; } + } public static class Retry { 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 83cbfe4cda..f870f4385a 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 @@ -1158,6 +1158,7 @@ content into your application. Rather, pick only the properties that you need. 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= # Whether to enable mandatory messages. + spring.rabbitmq.template.queue= # Name of the default queue to receive messages from when none is specified explicitly. spring.rabbitmq.template.receive-timeout= # Timeout for `receive()` operations. spring.rabbitmq.template.reply-timeout= # Timeout for `sendAndReceive()` operations. spring.rabbitmq.template.retry.enabled=false # Whether publishing retries are enabled. @@ -1166,7 +1167,6 @@ content into your application. Rather, pick only the properties that you need. spring.rabbitmq.template.retry.max-interval=10000ms # Maximum duration between attempts. spring.rabbitmq.template.retry.multiplier=1 # Multiplier to apply to the previous retry interval. spring.rabbitmq.template.routing-key= # Value of a default routing key to use for send operations. - spring.rabbitmq.template.queue= # Value of a default queue name that will be used for synchronous receives. spring.rabbitmq.username=guest # Login user to authenticate to the broker. spring.rabbitmq.virtual-host= # Virtual host to use when connecting to the broker.