Skip to content

Commit ec3b05f

Browse files
garyrussellartembilan
authored andcommitted
GH-1359: After Receive Post Processor Invoked x2
Resolves #1359 When the `RabbitTemplate` is configured with `afterReceivePostProcessors` and uses the default internal `DirectReplyToMessageListenerContainer`, the postprocessors are applied twice, once by the container and once by the template. The template should not propagate the post processors into the container. **cherry-pick to 2.3.x, 2.2.x**
1 parent 58a51b5 commit ec3b05f

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

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

-4
Original file line numberDiff line numberDiff line change
@@ -1980,10 +1980,6 @@ private DirectReplyToMessageListenerContainer createReplyToContainer(ConnectionF
19801980
if (this.taskExecutor != null) {
19811981
container.setTaskExecutor(this.taskExecutor);
19821982
}
1983-
if (this.afterReceivePostProcessors != null) {
1984-
container.setAfterReceivePostProcessors(this.afterReceivePostProcessors
1985-
.toArray(new MessagePostProcessor[this.afterReceivePostProcessors.size()]));
1986-
}
19871983
container.setNoLocal(this.noLocalReplyConsumer);
19881984
if (this.replyErrorHandler != null) {
19891985
container.setErrorHandler(this.replyErrorHandler);

Diff for: spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplateMPPIntegrationTests.java

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019 the original author or authors.
2+
* Copyright 2017-2021 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.
@@ -59,43 +59,46 @@ public class RabbitTemplateMPPIntegrationTests {
5959
@Autowired
6060
private Config config;
6161

62-
@Test // 2.0.x only
62+
@Test
6363
public void testMPPsAppliedDirectReplyToContainerTests() {
64+
this.config.afterMppCalled = 0;
6465
this.template.sendAndReceive(new Message("foo".getBytes(), new MessageProperties()));
6566
assertThat(this.config.beforeMppCalled).as("before MPP not called").isTrue();
66-
assertThat(this.config.afterMppCalled).as("after MPP not called").isTrue();
67+
assertThat(this.config.afterMppCalled).as("after MPP not called").isEqualTo(1);
6768
}
6869

6970
@Test
7071
public void testMPPsAppliedDirectReplyToTests() {
72+
this.config.afterMppCalled = 0;
7173
this.template.setUseDirectReplyToContainer(false);
7274
this.template.sendAndReceive(new Message("foo".getBytes(), new MessageProperties()));
7375
assertThat(this.config.beforeMppCalled).as("before MPP not called").isTrue();
74-
assertThat(this.config.afterMppCalled).as("after MPP not called").isTrue();
76+
assertThat(this.config.afterMppCalled).as("after MPP not called").isEqualTo(1);
7577
}
7678

7779
@Test
7880
public void testMPPsAppliedTemporaryReplyQueueTests() {
81+
this.config.afterMppCalled = 0;
7982
this.template.setUseDirectReplyToContainer(false);
8083
this.template.setUseTemporaryReplyQueues(true);
8184
this.template.sendAndReceive(new Message("foo".getBytes(), new MessageProperties()));
8285
assertThat(this.config.beforeMppCalled).as("before MPP not called").isTrue();
83-
assertThat(this.config.afterMppCalled).as("after MPP not called").isTrue();
86+
assertThat(this.config.afterMppCalled).as("after MPP not called").isEqualTo(1);
8487
}
8588

8689
@Test
8790
public void testMPPsAppliedReplyContainerTests() {
91+
this.config.afterMppCalled = 0;
8892
this.template.setReplyAddress(REPLIES);
8993
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(this.config.cf());
9094
try {
9195
container.setQueueNames(REPLIES);
9296
container.setMessageListener(this.template);
93-
container.setAfterReceivePostProcessors(this.config.afterMPP());
9497
container.afterPropertiesSet();
9598
container.start();
9699
this.template.sendAndReceive(new Message("foo".getBytes(), new MessageProperties()));
97100
assertThat(this.config.beforeMppCalled).as("before MPP not called").isTrue();
98-
assertThat(this.config.afterMppCalled).as("after MPP not called").isTrue();
101+
assertThat(this.config.afterMppCalled).as("after MPP not called").isEqualTo(1);
99102
}
100103
finally {
101104
container.stop();
@@ -106,9 +109,9 @@ public void testMPPsAppliedReplyContainerTests() {
106109
@EnableRabbit
107110
public static class Config {
108111

109-
private boolean beforeMppCalled;
112+
boolean beforeMppCalled;
110113

111-
private boolean afterMppCalled;
114+
int afterMppCalled;
112115

113116
@Bean
114117
public CachingConnectionFactory cf() {
@@ -131,7 +134,7 @@ public RabbitTemplate template() {
131134
@Bean
132135
public MessagePostProcessor afterMPP() {
133136
return m -> {
134-
this.afterMppCalled = true;
137+
this.afterMppCalled++;
135138
return m;
136139
};
137140
}

0 commit comments

Comments
 (0)