Merge pull request #10978 from ArloL:pr-rabbit-properties

* pr/10978:
  Polish "Make RabbitTemplate exchange and routingKey configurable"
  Make RabbitTemplate exchange and routingKey configurable
pull/11330/head
Stephane Nicoll 7 years ago
commit 99d467e93c

@ -188,6 +188,8 @@ public class RabbitAutoConfiguration {
if (templateProperties.getReplyTimeout() != null) {
rabbitTemplate.setReplyTimeout(templateProperties.getReplyTimeout());
}
rabbitTemplate.setExchange(templateProperties.getExchange());
rabbitTemplate.setRoutingKey(templateProperties.getRoutingKey());
return rabbitTemplate;
}

@ -703,6 +703,16 @@ 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 = "";
public Retry getRetry() {
return this.retry;
}
@ -731,6 +741,22 @@ 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 static class Retry {

@ -103,6 +103,19 @@ 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());
});
}
@Test
public void testConnectionFactoryWithOverrides() {
this.contextRunner.withUserConfiguration(TestConfiguration.class)
@ -224,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)

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

Loading…
Cancel
Save