Skip to content

Commit b6a3808

Browse files
committed
AbstractMessageChannel triggers afterSendCompletion with previously resolved non-null Message in case of preSend returning null
Issue: SPR-12295
1 parent 9d96958 commit b6a3808

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

spring-messaging/src/main/java/org/springframework/messaging/support/AbstractMessageChannel.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -37,7 +37,7 @@
3737
* @author Rossen Stoyanchev
3838
* @since 4.0
3939
*/
40-
public abstract class AbstractMessageChannel implements MessageChannel, BeanNameAware, InterceptableChannel {
40+
public abstract class AbstractMessageChannel implements MessageChannel, InterceptableChannel, BeanNameAware {
4141

4242
protected final Log logger = LogFactory.getLog(getClass());
4343

@@ -50,22 +50,23 @@ public AbstractMessageChannel() {
5050
this.beanName = getClass().getSimpleName() + "@" + ObjectUtils.getIdentityHexString(this);
5151
}
5252

53+
5354
/**
54-
* {@inheritDoc}
55-
* <p>Used primarily for logging purposes.
55+
* A message channel uses the bean name primarily for logging purposes.
5656
*/
5757
@Override
5858
public void setBeanName(String name) {
5959
this.beanName = name;
6060
}
6161

6262
/**
63-
* @return the name for this channel.
63+
* Return the bean name for this message channel.
6464
*/
6565
public String getBeanName() {
6666
return this.beanName;
6767
}
6868

69+
6970
@Override
7071
public void setInterceptors(List<ChannelInterceptor> interceptors) {
7172
this.interceptors.clear();
@@ -82,10 +83,6 @@ public void addInterceptor(int index, ChannelInterceptor interceptor) {
8283
this.interceptors.add(index, interceptor);
8384
}
8485

85-
/**
86-
* {@inheritDoc}
87-
* <p>The returned list is read-only.
88-
*/
8986
@Override
9087
public List<ChannelInterceptor> getInterceptors() {
9188
return Collections.unmodifiableList(this.interceptors);
@@ -101,6 +98,7 @@ public ChannelInterceptor removeInterceptor(int index) {
10198
return this.interceptors.remove(index);
10299
}
103100

101+
104102
@Override
105103
public final boolean send(Message<?> message) {
106104
return send(message, INDEFINITE_TIMEOUT);
@@ -129,7 +127,8 @@ public final boolean send(Message<?> message, long timeout) {
129127
throw new MessageDeliveryException(message,"Failed to send message to " + this, ex);
130128
}
131129
catch (Error ex) {
132-
MessageDeliveryException ex2 = new MessageDeliveryException(message, "Failed to send message to " + this, ex);
130+
MessageDeliveryException ex2 =
131+
new MessageDeliveryException(message, "Failed to send message to " + this, ex);
133132
chain.triggerAfterSendCompletion(message, this, sent, ex2);
134133
throw ex2;
135134
}
@@ -153,21 +152,22 @@ protected class ChannelInterceptorChain {
153152

154153
private int receiveInterceptorIndex = -1;
155154

156-
157155
public Message<?> applyPreSend(Message<?> message, MessageChannel channel) {
156+
Message<?> messageToUse = message;
158157
for (ChannelInterceptor interceptor : interceptors) {
159-
message = interceptor.preSend(message, channel);
160-
if (message == null) {
158+
Message<?> resolvedMessage = interceptor.preSend(messageToUse, channel);
159+
if (resolvedMessage == null) {
161160
String name = interceptor.getClass().getSimpleName();
162161
if (logger.isDebugEnabled()) {
163162
logger.debug(name + " returned null from preSend, i.e. precluding the send.");
164163
}
165-
triggerAfterSendCompletion(message, channel, false, null);
164+
triggerAfterSendCompletion(messageToUse, channel, false, null);
166165
return null;
167166
}
167+
messageToUse = resolvedMessage;
168168
this.sendInterceptorIndex++;
169169
}
170-
return message;
170+
return messageToUse;
171171
}
172172

173173
public void applyPostSend(Message<?> message, MessageChannel channel, boolean sent) {
@@ -216,11 +216,12 @@ public void triggerAfterReceiveCompletion(Message<?> message, MessageChannel cha
216216
interceptor.afterReceiveCompletion(message, channel, ex);
217217
}
218218
catch (Throwable ex2) {
219-
logger.error("Exception from afterReceiveCompletion in " + interceptor, ex2);
219+
if (logger.isErrorEnabled()) {
220+
logger.error("Exception from afterReceiveCompletion in " + interceptor, ex2);
221+
}
220222
}
221223
}
222224
}
223-
224225
}
225226

226-
}
227+
}

0 commit comments

Comments
 (0)