diff --git a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitMessagingTemplate.java b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitMessagingTemplate.java index 5db7431a..6194c66b 100644 --- a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitMessagingTemplate.java +++ b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitMessagingTemplate.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,6 +46,8 @@ public class RabbitMessagingTemplate extends AbstractMessagingTemplate private boolean converterSet; + private boolean useTemplateDefaultReceiveQueue; + /** * Constructor for use with bean properties. @@ -69,6 +71,7 @@ public class RabbitMessagingTemplate extends AbstractMessagingTemplate * @param rabbitTemplate the template. */ public void setRabbitTemplate(RabbitTemplate rabbitTemplate) { + Assert.notNull(rabbitTemplate, "'rabbitTemplate' must not be null"); this.rabbitTemplate = rabbitTemplate; } @@ -105,6 +108,18 @@ public class RabbitMessagingTemplate extends AbstractMessagingTemplate return this.amqpMessageConverter; } + /** + * When true, use the underlying {@link RabbitTemplate}'s defaultReceiveQueue property + * (if configured) for receive only methods instead of the {@code defaultDestination} + * configured in this template. Set this to true to use the template's queue instead. + * Default false, but will be true in a future release. + * @param useTemplateDefaultReceiveQueue true to use the template's queue. + * @since 2.2.22 + */ + public void setUseTemplateDefaultReceiveQueue(boolean useTemplateDefaultReceiveQueue) { + this.useTemplateDefaultReceiveQueue = useTemplateDefaultReceiveQueue; + } + @Override public void afterPropertiesSet() { Assert.notNull(getRabbitTemplate(), "Property 'rabbitTemplate' is required"); @@ -211,6 +226,28 @@ public class RabbitMessagingTemplate extends AbstractMessagingTemplate } } + @Override + @Nullable + public Message receive() { + return doReceive(resolveDestination()); + } + + @Override + @Nullable + public T receiveAndConvert(Class targetClass) { + return receiveAndConvert(resolveDestination(), targetClass); + } + + private String resolveDestination() { + String dest = null; + if (this.useTemplateDefaultReceiveQueue) { + dest = this.rabbitTemplate.getDefaultReceiveQueue(); + } + if (dest == null) { + dest = getRequiredDefaultDestination(); + } + return dest; + } @Override protected Message doReceive(String destination) { diff --git a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitTemplate.java b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitTemplate.java index 41322e49..c567d03f 100644 --- a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitTemplate.java +++ b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitTemplate.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -336,6 +336,16 @@ public class RabbitTemplate extends RabbitAccessor // NOSONAR type line count this.defaultReceiveQueue = queue; } + /** + * Return the configured default receive queue. + * @return the queue or null if not configured. + * @since 2.2.22 + */ + @Nullable + public String getDefaultReceiveQueue() { + return this.defaultReceiveQueue; + } + /** * The encoding to use when converting between byte arrays and Strings in message properties. * diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitMessagingTemplateTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitMessagingTemplateTests.java index 10ac6206..ddfe9dfc 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitMessagingTemplateTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitMessagingTemplateTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -230,6 +230,20 @@ public class RabbitMessagingTemplateTests { assertTextMessage(message); } + @Test + public void receiveDefaultDestinationOverride() { + messagingTemplate.setDefaultDestination("defaultDest"); + messagingTemplate.setUseTemplateDefaultReceiveQueue(true); + + org.springframework.amqp.core.Message amqpMsg = createAmqpTextMessage(); + given(rabbitTemplate.getDefaultReceiveQueue()).willReturn("default"); + given(rabbitTemplate.receive("default")).willReturn(amqpMsg); + + Message message = messagingTemplate.receive(); + verify(rabbitTemplate).receive("default"); + assertTextMessage(message); + } + @Test public void receiveNoDefaultSet() { assertThatIllegalStateException()