Skip to content

Commit 5989d03

Browse files
artembilangaryrussell
authored andcommitted
GH-712: Add RabbitTemplate.noLocalReplyConsumer
Fixes #712 For controlling `noLocal` flag for reply consumers in the `sendAndReceive()` operations introduce a `noLocalReplyConsumer` option for the `RabbitTemplate`. Make it `false` by default since it does not have any value in RabbitMQ, but matters in QPid for temporary queues
1 parent f72f2e1 commit 5989d03

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ public class RabbitTemplate extends RabbitAccessor implements BeanFactoryAware,
246246

247247
private boolean usePublisherConnection;
248248

249+
private boolean noLocalReplyConsumer;
250+
249251
/**
250252
* Convenient constructor for use with setter injection. Don't forget to set the connection factory.
251253
*/
@@ -692,6 +694,17 @@ public void setUsePublisherConnection(boolean usePublisherConnection) {
692694
this.usePublisherConnection = usePublisherConnection;
693695
}
694696

697+
/**
698+
* Set to true for a no-local consumer. Defaults to false.
699+
* @param noLocalReplyConsumer true for a no-local consumer.
700+
* @since 2.1
701+
* @see AbstractMessageListenerContainer#setNoLocal(boolean)
702+
* @see Channel#basicConsume(String, boolean, String, boolean, boolean, Map, com.rabbitmq.client.Consumer)
703+
*/
704+
public void setNoLocalReplyConsumer(boolean noLocalReplyConsumer) {
705+
this.noLocalReplyConsumer = noLocalReplyConsumer;
706+
}
707+
695708
/**
696709
* Invoked by the container during startup so it can verify the queue is correctly
697710
* configured (if a simple reply queue name is used instead of exchange/routingKey.
@@ -1573,7 +1586,7 @@ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProp
15731586
}
15741587

15751588
};
1576-
channel.basicConsume(replyTo, true, consumerTag, true, true, null, consumer);
1589+
channel.basicConsume(replyTo, true, consumerTag, this.noLocalReplyConsumer, true, null, consumer);
15771590
Message reply = null;
15781591
try {
15791592
reply = exchangeMessages(exchange, routingKey, message, correlationData, channel, pendingReply,
@@ -1629,6 +1642,7 @@ private Message doSendAndReceiveWithDirect(String exchange, String routingKey, M
16291642
container.setAfterReceivePostProcessors(this.afterReceivePostProcessors
16301643
.toArray(new MessagePostProcessor[this.afterReceivePostProcessors.size()]));
16311644
}
1645+
container.setNoLocal(this.noLocalReplyConsumer);
16321646
container.start();
16331647
this.directReplyToContainers.put(connectionFactory, container);
16341648
this.replyAddress = Address.AMQ_RABBITMQ_REPLY_TO;

src/reference/asciidoc/amqp.adoc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3077,13 +3077,15 @@ Similar request/reply methods are also available where the `MessageConverter` is
30773077
Those methods are named `convertSendAndReceive`.
30783078
See the Javadoc of `AmqpTemplate` for more detail.
30793079

3080-
Starting with _version 1.5.0_, each of the `sendAndReceive` method variants has an overloaded version that takes `CorrelationData`.
3080+
Starting with version 1.5.0, each of the `sendAndReceive` method variants has an overloaded version that takes `CorrelationData`.
30813081
Together with a properly configured connection factory, this enables the receipt of publisher confirms for the send side of the operation.
30823082
See <<template-confirms>> and the javadoc for `RabbitOperations` for more information.
30833083

3084-
Starting with __version 2.0__, there are variants of these methods (`convertSendAndReceiveAsType`) that take an additional `ParameterizedTypeReference` argument to convert complex returned types.
3084+
Starting with version 2.0, there are variants of these methods (`convertSendAndReceiveAsType`) that take an additional `ParameterizedTypeReference` argument to convert complex returned types.
30853085
The template must be configured with a `SmartMessageConverter`; see <<json-complex>> for more information.
30863086

3087+
Starting with version 2.1, the `RabbitTemplate` can be configured with the `noLocalReplyConsumer` option to control a `noLocal` flag for reply consumers.
3088+
This is `false` now by default.
30873089

30883090
[[reply-timeout]]
30893091
===== Reply Timeout
@@ -3103,26 +3105,26 @@ Also, you must not have registered your own `ReturnCallback` with the `RabbitTem
31033105
[[direct-reply-to]]
31043106
===== RabbitMQ Direct reply-to
31053107

3106-
IMPORTANT: Starting with _version 3.4.0_, the RabbitMQ server now supports http://www.rabbitmq.com/direct-reply-to.html[Direct reply-to]; this eliminates the main reason for a fixed reply queue (to avoid the need to create a temporary queue for each request).
3108+
IMPORTANT: Starting with version 3.4.0, the RabbitMQ server now supports http://www.rabbitmq.com/direct-reply-to.html[Direct reply-to]; this eliminates the main reason for a fixed reply queue (to avoid the need to create a temporary queue for each request).
31073109
Starting with *Spring AMQP version 1.4.1* Direct reply-to will be used by default (if supported by the server) instead of creating temporary reply queues.
31083110
When no `replyQueue` is provided (or it is set with the name `amq.rabbitmq.reply-to`), the `RabbitTemplate` will automatically detect whether Direct reply-to is supported and either use it or fall back to using a temporary reply queue.
31093111
When using Direct reply-to, a `reply-listener` is not required and should not be configured.
31103112

31113113
Reply listeners are still supported with named queues (other than `amq.rabbitmq.reply-to`), allowing control of reply concurrency etc.
31123114

3113-
Starting with _version 1.6_ if, for some reason, you wish to use a temporary, exclusive, auto-delete queue for each
3115+
Starting with version 1.6 if, for some reason, you wish to use a temporary, exclusive, auto-delete queue for each
31143116
reply, set the `useTemporaryReplyQueues` property to `true`.
31153117
This property is ignored if you you set a `replyAddress`.
31163118

31173119
The decision whether or not to use direct reply-to can be changed to use different criteria by subclassing
31183120
`RabbitTemplate` and overriding `useDirectReplyTo()`.
31193121
The method is called once only; when the first request is sent.
31203122

3121-
With versions earlier than _verion 2.0_, the `RabbitTemplate` created a new consumer for each request and canceled the consumer when the reply was received (or timed out).
3123+
With versions earlier than version 2.0, the `RabbitTemplate` created a new consumer for each request and canceled the consumer when the reply was received (or timed out).
31223124
Now, the template uses a `DirectReplyToMessageListenerContainer` instead, allowing the consumers to be reused; the template still takes care of correlating the replies so there is no danger of a late reply going to a different sender.
31233125
If you want to revert to the previous behavior, set property `useDirectReplyToContainer` (`direct-reply-to-container` when using XML configuration) to false.
31243126

3125-
The `AsynRabbitTemplate` has no such option - it always used a `DirectReplyToContainer` for replies when direct replyTo is being used.
3127+
The `AsyncRabbitTemplate` has no such option - it always used a `DirectReplyToContainer` for replies when direct replyTo is being used.
31263128

31273129
===== Message Correlation With A Reply Queue
31283130

src/reference/asciidoc/whats-new.adoc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,15 @@ See <<template-confirms>> for more information.
3131
The listener container factories can now be used to create any listener container, not just those for use with `@RabbitListener` s or the `@RabbitListenerEndpointRegistry`.
3232
See <<using-container-factories>> for more information.
3333

34-
`ChannelAwareMessageListener` now inherits from `MesssageListener`.
34+
`ChannelAwareMessageListener` now inherits from `MessageListener`.
3535

3636
===== RabbitAdmin Changes
3737

3838
The `RabbitAdmin` will discover of type `Declarables`, which is a container for `Declarable` (`Queue`, `Exchange`, `Binding`) objects, and declare the objects on the broker.
3939
Users are discouraged from using the old mechanism of declaring `<Collection<Queue>>` etc and should use `Declarables` beans instead.
4040
See <<collection-declaration>> for more information.
41+
42+
===== RabbitTemplate Changes
43+
44+
The `RabbitTemplate` now can be configured with the `noLocalReplyConsumer` option to control a `noLocal` flag for reply consumers in the `sendAndReceive()` operations.
45+
See <<request-reply>> for more information.

0 commit comments

Comments
 (0)