Make RabbitTemplate default receive queue configurable

See gh-13930
This commit is contained in:
artsiom 2018-07-27 20:40:43 +03:00 committed by Stephane Nicoll
parent f1e2abce2d
commit d7621261b2
4 changed files with 28 additions and 0 deletions

View File

@ -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;
}

View File

@ -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 {

View File

@ -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)

View File

@ -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.