Skip to content

Commit fcb06ba

Browse files
Warn about dropped message in filter (#8579)
* Warn about dropped message in filter Buy default the `MessageFilter` just drops a discarded message silently. If a request-reply gateway is used upstream, then it becomes unclear why the flow sometimes doesn't work. * Add a waring log ot emit for default behavior. This still doesn't fix the request-reply problem, but at least it can give a clue what is going on * * Mention `nullChannel` variant to ignore even warn * Fix language in docs Co-authored-by: Gary Russell <[email protected]> --------- Co-authored-by: Gary Russell <[email protected]>
1 parent 8ed4e5f commit fcb06ba

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

spring-integration-core/src/main/java/org/springframework/integration/filter/MessageFilter.java

+14-10
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-2023 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.
@@ -20,6 +20,7 @@
2020
import org.springframework.beans.factory.BeanFactoryAware;
2121
import org.springframework.context.Lifecycle;
2222
import org.springframework.core.convert.ConversionService;
23+
import org.springframework.core.log.LogMessage;
2324
import org.springframework.integration.IntegrationPatternType;
2425
import org.springframework.integration.MessageRejectedException;
2526
import org.springframework.integration.core.MessageSelector;
@@ -33,11 +34,11 @@
3334
/**
3435
* Message Handler that delegates to a {@link MessageSelector}. If and only if
3536
* the selector {@link MessageSelector#accept(Message) accepts} the Message, it
36-
* will be passed to this filter's output channel. Otherwise the message will
37-
* either be silently dropped (the default) or will trigger the throwing of a
38-
* {@link MessageRejectedException} depending on the value of its
39-
* {@link #throwExceptionOnRejection} property. If a discard channel is
40-
* provided, the rejected Messages will be sent to that channel.
37+
* will be passed to this filter's output channel. Otherwise, the message will
38+
* either be silently dropped (the default) with a warning into logs,
39+
* or will trigger the throwing of a {@link MessageRejectedException}
40+
* depending on the value of its {@link #throwExceptionOnRejection} property.
41+
* If a discard channel is provided, the rejected Messages will be sent to that channel.
4142
*
4243
* @author Mark Fisher
4344
* @author Oleg Zhurakousky
@@ -71,7 +72,7 @@ public MessageFilter(MessageSelector selector) {
7172
* {@link MessageRejectedException} when its selector does not accept a
7273
* Message. The default value is <code>false</code> meaning that rejected
7374
* Messages will be quietly dropped or sent to the discard channel if
74-
* available. Typically this value would not be <code>true</code> when
75+
* available. Typically, this value would not be <code>true</code> when
7576
* a discard channel is provided, but if so, it will still apply
7677
* (in such a case, the Message will be sent to the discard channel,
7778
* and <em>then</em> the exception will be thrown).
@@ -179,13 +180,16 @@ protected Object doHandleRequestMessage(Message<?> message) {
179180
@Override
180181
public Object postProcess(Message<?> message, Object result) {
181182
if (result == null) {
182-
MessageChannel channel = getDiscardChannel();
183-
if (channel != null) {
184-
this.messagingTemplate.send(channel, message);
183+
MessageChannel channelToDiscard = getDiscardChannel();
184+
if (channelToDiscard != null) {
185+
this.messagingTemplate.send(channelToDiscard, message);
185186
}
186187
if (this.throwExceptionOnRejection) {
187188
throw new MessageRejectedException(message, "message has been rejected in filter: " + this);
188189
}
190+
else if (channelToDiscard == null) {
191+
logger.warn(LogMessage.format("The message [%s] has been rejected in filter: %s", message, this));
192+
}
189193
}
190194
return result;
191195
}

src/reference/asciidoc/filter.adoc

+4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ If you want rejected messages to be routed to a specific channel, provide that r
8888
----
8989
====
9090

91+
If the `throwExceptionOnRejection == false` and no `discardChannel` is provided, the message is silently dropped and an `o.s.i.filter.MessageFilter` instance just emits a warning log message (starting with version 6.1) about this discarded message.
92+
To drop the message with no warning in the logs, a `NullChannel` can be configured as the `discardChannel` on the filter.
93+
The goal of the framework is to not be completely silent, by default, requiring an explicit option to be set, if that is the desired behavior.
94+
9195
See also <<./handler-advice.adoc#advising-filters,Advising Filters>>.
9296

9397
NOTE: Message filters are commonly used in conjunction with a publish-subscribe channel.

src/reference/asciidoc/whats-new.adoc

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ See <<./zip.adoc#zip,Zip Support>> for more information.
3030
- Added support for transforming to/from Protocol Buffers.
3131
See <<./transformer.adoc#Protobuf-transformers, Protocol Buffers Transformers>> for more information.
3232

33+
- The `MessageFilter` now emits a warning into logs when message is silently discarded and dropped.
34+
See <<./filter.adoc#filter, Filter>> for more information.
3335

3436
[[x6.1-web-sockets]]
3537
=== Web Sockets Changes

0 commit comments

Comments
 (0)