Merge pull request #13930 from ayudovin:make-rabbitTemplate-default-receive-queue-configurable

* pr/13930:
  Polish "Make RabbitTemplate default receive queue configurable"
  Make RabbitTemplate default receive queue configurable
pull/13966/head
Stephane Nicoll 6 years ago
commit 3aef6016e2

@ -79,6 +79,7 @@ import org.springframework.context.annotation.Import;
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Gary Russell * @author Gary Russell
* @author Phillip Webb * @author Phillip Webb
* @author Artsiom Yudovin
*/ */
@Configuration @Configuration
@ConditionalOnClass({ RabbitTemplate.class, Channel.class }) @ConditionalOnClass({ RabbitTemplate.class, Channel.class })
@ -190,6 +191,7 @@ public class RabbitAutoConfiguration {
.to(template::setReplyTimeout); .to(template::setReplyTimeout);
map.from(properties::getExchange).to(template::setExchange); map.from(properties::getExchange).to(template::setExchange);
map.from(properties::getRoutingKey).to(template::setRoutingKey); map.from(properties::getRoutingKey).to(template::setRoutingKey);
map.from(properties::getQueue).whenNonNull().to(template::setQueue);
return template; return template;
} }

@ -37,6 +37,7 @@ import org.springframework.util.StringUtils;
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Josh Thornhill * @author Josh Thornhill
* @author Gary Russell * @author Gary Russell
* @author Artsiom Yudovin
*/ */
@ConfigurationProperties(prefix = "spring.rabbitmq") @ConfigurationProperties(prefix = "spring.rabbitmq")
public class RabbitProperties { public class RabbitProperties {
@ -713,6 +714,12 @@ public class RabbitProperties {
*/ */
private String routingKey = ""; private String routingKey = "";
/**
* Name of the default queue to receive messages from when none is specified
* explicitly.
*/
private String queue;
public Retry getRetry() { public Retry getRetry() {
return this.retry; return this.retry;
} }
@ -757,6 +764,14 @@ public class RabbitProperties {
this.routingKey = routingKey; this.routingKey = routingKey;
} }
public String getQueue() {
return this.queue;
}
public void setQueue(String queue) {
this.queue = queue;
}
} }
public static class Retry { public static class Retry {

@ -62,6 +62,7 @@ import org.springframework.retry.interceptor.MethodInvocationRecoverer;
import org.springframework.retry.policy.NeverRetryPolicy; import org.springframework.retry.policy.NeverRetryPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy; import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate; import org.springframework.retry.support.RetryTemplate;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString; 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 @Test
public void testRabbitTemplateMandatory() { public void testRabbitTemplateMandatory() {
this.contextRunner.withUserConfiguration(TestConfiguration.class) this.contextRunner.withUserConfiguration(TestConfiguration.class)

@ -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.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.exchange= # Name of the default exchange to use for send operations.
spring.rabbitmq.template.mandatory= # Whether to enable mandatory messages. 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.receive-timeout= # Timeout for `receive()` operations.
spring.rabbitmq.template.reply-timeout= # Timeout for `sendAndReceive()` operations. spring.rabbitmq.template.reply-timeout= # Timeout for `sendAndReceive()` operations.
spring.rabbitmq.template.retry.enabled=false # Whether publishing retries are enabled. spring.rabbitmq.template.retry.enabled=false # Whether publishing retries are enabled.

Loading…
Cancel
Save