Skip to content

Commit cf96793

Browse files
authored
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**
1 parent d3b93e9 commit cf96793

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-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.
@@ -48,6 +48,8 @@ public class RabbitMessagingTemplate extends AbstractMessagingTemplate<String>
4848

4949
private boolean converterSet;
5050

51+
private boolean useTemplateDefaultReceiveQueue;
52+
5153

5254
/**
5355
* Constructor for use with bean properties.
@@ -71,6 +73,7 @@ public RabbitMessagingTemplate(RabbitTemplate rabbitTemplate) {
7173
* @param rabbitTemplate the template.
7274
*/
7375
public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
76+
Assert.notNull(rabbitTemplate, "'rabbitTemplate' must not be null");
7477
this.rabbitTemplate = rabbitTemplate;
7578
}
7679

@@ -107,6 +110,18 @@ public MessageConverter getAmqpMessageConverter() {
107110
return this.amqpMessageConverter;
108111
}
109112

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

243+
@Override
244+
@Nullable
245+
public Message<?> receive() {
246+
return doReceive(resolveDestination());
247+
}
248+
249+
@Override
250+
@Nullable
251+
public <T> T receiveAndConvert(Class<T> targetClass) {
252+
return receiveAndConvert(resolveDestination(), targetClass);
253+
}
254+
255+
private String resolveDestination() {
256+
String dest = null;
257+
if (this.useTemplateDefaultReceiveQueue) {
258+
dest = this.rabbitTemplate.getDefaultReceiveQueue();
259+
}
260+
if (dest == null) {
261+
dest = getRequiredDefaultDestination();
262+
}
263+
return dest;
264+
}
228265

229266
@Override
230267
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-2021 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.
@@ -348,6 +348,16 @@ public void setDefaultReceiveQueue(String queue) {
348348
this.defaultReceiveQueue = queue;
349349
}
350350

351+
/**
352+
* Return the configured default receive queue.
353+
* @return the queue or null if not configured.
354+
* @since 2.2.22
355+
*/
356+
@Nullable
357+
public String getDefaultReceiveQueue() {
358+
return this.defaultReceiveQueue;
359+
}
360+
351361
/**
352362
* The encoding to use when converting between byte arrays and Strings in message properties.
353363
*

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-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.
@@ -256,6 +256,20 @@ public void receiveDefaultDestination() {
256256
assertTextMessage(message);
257257
}
258258

259+
@Test
260+
public void receiveDefaultDestinationOverride() {
261+
messagingTemplate.setDefaultDestination("defaultDest");
262+
messagingTemplate.setUseTemplateDefaultReceiveQueue(true);
263+
264+
org.springframework.amqp.core.Message amqpMsg = createAmqpTextMessage();
265+
given(rabbitTemplate.getDefaultReceiveQueue()).willReturn("default");
266+
given(rabbitTemplate.receive("default")).willReturn(amqpMsg);
267+
268+
Message<?> message = messagingTemplate.receive();
269+
verify(rabbitTemplate).receive("default");
270+
assertTextMessage(message);
271+
}
272+
259273
@Test
260274
public void receiveNoDefaultSet() {
261275
assertThatIllegalStateException()

0 commit comments

Comments
 (0)