GH-1412: Fix Messaging Template
* GH-1412: Fix Messaging Template Resolves https://github.com/spring-projects/spring-amqp/issues/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
This commit is contained in:
committed by
Artem Bilan
parent
d1e3e6dcd6
commit
04ddbbdbcb
@@ -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<String>
|
||||
|
||||
private boolean converterSet;
|
||||
|
||||
private boolean useTemplateDefaultReceiveQueue;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor for use with bean properties.
|
||||
@@ -69,6 +71,7 @@ public class RabbitMessagingTemplate extends AbstractMessagingTemplate<String>
|
||||
* @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<String>
|
||||
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<String>
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public Message<?> receive() {
|
||||
return doReceive(resolveDestination());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public <T> T receiveAndConvert(Class<T> 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) {
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user