Skip to content

Commit 04ddbbd

Browse files
garyrussellartembilan
authored andcommitted
GH-1412: Fix Messaging Template
* GH-1412: Fix Messaging Template Resolves #1412 The use of the `defaultDestination` is not correct when the template is used for both sends and receives because it is a queue name for receives and a routing key for sends. Add a new option to use the template's configured default receive queue for receive only methods. False by default to avoid a breaking change; it should be true by default in a future release. * Fix Javadocs **cherry-pick to 2.3.x, 2.2.x** # Conflicts: # spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitMessagingTemplate.java # spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitTemplate.java # spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitMessagingTemplateTests.java
1 parent d1e3e6d commit 04ddbbd

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitMessagingTemplate.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,6 +46,8 @@ public class RabbitMessagingTemplate extends AbstractMessagingTemplate<String>
4646

4747
private boolean converterSet;
4848

49+
private boolean useTemplateDefaultReceiveQueue;
50+
4951

5052
/**
5153
* Constructor for use with bean properties.
@@ -69,6 +71,7 @@ public RabbitMessagingTemplate(RabbitTemplate rabbitTemplate) {
6971
* @param rabbitTemplate the template.
7072
*/
7173
public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
74+
Assert.notNull(rabbitTemplate, "'rabbitTemplate' must not be null");
7275
this.rabbitTemplate = rabbitTemplate;
7376
}
7477

@@ -105,6 +108,18 @@ public MessageConverter getAmqpMessageConverter() {
105108
return this.amqpMessageConverter;
106109
}
107110

111+
/**
112+
* When true, use the underlying {@link RabbitTemplate}'s defaultReceiveQueue property
113+
* (if configured) for receive only methods instead of the {@code defaultDestination}
114+
* configured in this template. Set this to true to use the template's queue instead.
115+
* Default false, but will be true in a future release.
116+
* @param useTemplateDefaultReceiveQueue true to use the template's queue.
117+
* @since 2.2.22
118+
*/
119+
public void setUseTemplateDefaultReceiveQueue(boolean useTemplateDefaultReceiveQueue) {
120+
this.useTemplateDefaultReceiveQueue = useTemplateDefaultReceiveQueue;
121+
}
122+
108123
@Override
109124
public void afterPropertiesSet() {
110125
Assert.notNull(getRabbitTemplate(), "Property 'rabbitTemplate' is required");
@@ -211,6 +226,28 @@ protected void doSend(String exchange, String routingKey, Message<?> message) {
211226
}
212227
}
213228

229+
@Override
230+
@Nullable
231+
public Message<?> receive() {
232+
return doReceive(resolveDestination());
233+
}
234+
235+
@Override
236+
@Nullable
237+
public <T> T receiveAndConvert(Class<T> targetClass) {
238+
return receiveAndConvert(resolveDestination(), targetClass);
239+
}
240+
241+
private String resolveDestination() {
242+
String dest = null;
243+
if (this.useTemplateDefaultReceiveQueue) {
244+
dest = this.rabbitTemplate.getDefaultReceiveQueue();
245+
}
246+
if (dest == null) {
247+
dest = getRequiredDefaultDestination();
248+
}
249+
return dest;
250+
}
214251

215252
@Override
216253
protected Message<?> doReceive(String destination) {

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitTemplate.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -336,6 +336,16 @@ public void setDefaultReceiveQueue(String queue) {
336336
this.defaultReceiveQueue = queue;
337337
}
338338

339+
/**
340+
* Return the configured default receive queue.
341+
* @return the queue or null if not configured.
342+
* @since 2.2.22
343+
*/
344+
@Nullable
345+
public String getDefaultReceiveQueue() {
346+
return this.defaultReceiveQueue;
347+
}
348+
339349
/**
340350
* The encoding to use when converting between byte arrays and Strings in message properties.
341351
*

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitMessagingTemplateTests.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -230,6 +230,20 @@ public void receiveDefaultDestination() {
230230
assertTextMessage(message);
231231
}
232232

233+
@Test
234+
public void receiveDefaultDestinationOverride() {
235+
messagingTemplate.setDefaultDestination("defaultDest");
236+
messagingTemplate.setUseTemplateDefaultReceiveQueue(true);
237+
238+
org.springframework.amqp.core.Message amqpMsg = createAmqpTextMessage();
239+
given(rabbitTemplate.getDefaultReceiveQueue()).willReturn("default");
240+
given(rabbitTemplate.receive("default")).willReturn(amqpMsg);
241+
242+
Message<?> message = messagingTemplate.receive();
243+
verify(rabbitTemplate).receive("default");
244+
assertTextMessage(message);
245+
}
246+
233247
@Test
234248
public void receiveNoDefaultSet() {
235249
assertThatIllegalStateException()

0 commit comments

Comments
 (0)