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 <grussell@vmware.com> --------- Co-authored-by: Gary Russell <grussell@vmware.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2023 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.
|
||||
@@ -20,6 +20,7 @@ import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.context.Lifecycle;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.log.LogMessage;
|
||||
import org.springframework.integration.IntegrationPatternType;
|
||||
import org.springframework.integration.MessageRejectedException;
|
||||
import org.springframework.integration.core.MessageSelector;
|
||||
@@ -33,11 +34,11 @@ import org.springframework.util.Assert;
|
||||
/**
|
||||
* Message Handler that delegates to a {@link MessageSelector}. If and only if
|
||||
* the selector {@link MessageSelector#accept(Message) accepts} the Message, it
|
||||
* will be passed to this filter's output channel. Otherwise the message will
|
||||
* either be silently dropped (the default) or will trigger the throwing of a
|
||||
* {@link MessageRejectedException} depending on the value of its
|
||||
* {@link #throwExceptionOnRejection} property. If a discard channel is
|
||||
* provided, the rejected Messages will be sent to that channel.
|
||||
* will be passed to this filter's output channel. Otherwise, the message will
|
||||
* either be silently dropped (the default) with a warning into logs,
|
||||
* or will trigger the throwing of a {@link MessageRejectedException}
|
||||
* depending on the value of its {@link #throwExceptionOnRejection} property.
|
||||
* If a discard channel is provided, the rejected Messages will be sent to that channel.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @author Oleg Zhurakousky
|
||||
@@ -71,7 +72,7 @@ public class MessageFilter extends AbstractReplyProducingPostProcessingMessageHa
|
||||
* {@link MessageRejectedException} when its selector does not accept a
|
||||
* Message. The default value is <code>false</code> meaning that rejected
|
||||
* Messages will be quietly dropped or sent to the discard channel if
|
||||
* available. Typically this value would not be <code>true</code> when
|
||||
* available. Typically, this value would not be <code>true</code> when
|
||||
* a discard channel is provided, but if so, it will still apply
|
||||
* (in such a case, the Message will be sent to the discard channel,
|
||||
* and <em>then</em> the exception will be thrown).
|
||||
@@ -179,13 +180,16 @@ public class MessageFilter extends AbstractReplyProducingPostProcessingMessageHa
|
||||
@Override
|
||||
public Object postProcess(Message<?> message, Object result) {
|
||||
if (result == null) {
|
||||
MessageChannel channel = getDiscardChannel();
|
||||
if (channel != null) {
|
||||
this.messagingTemplate.send(channel, message);
|
||||
MessageChannel channelToDiscard = getDiscardChannel();
|
||||
if (channelToDiscard != null) {
|
||||
this.messagingTemplate.send(channelToDiscard, message);
|
||||
}
|
||||
if (this.throwExceptionOnRejection) {
|
||||
throw new MessageRejectedException(message, "message has been rejected in filter: " + this);
|
||||
}
|
||||
else if (channelToDiscard == null) {
|
||||
logger.warn(LogMessage.format("The message [%s] has been rejected in filter: %s", message, this));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -88,6 +88,10 @@ If you want rejected messages to be routed to a specific channel, provide that r
|
||||
----
|
||||
====
|
||||
|
||||
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.
|
||||
To drop the message with no warning in the logs, a `NullChannel` can be configured as the `discardChannel` on the filter.
|
||||
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.
|
||||
|
||||
See also <<./handler-advice.adoc#advising-filters,Advising Filters>>.
|
||||
|
||||
NOTE: Message filters are commonly used in conjunction with a publish-subscribe channel.
|
||||
|
||||
@@ -30,6 +30,8 @@ See <<./zip.adoc#zip,Zip Support>> for more information.
|
||||
- Added support for transforming to/from Protocol Buffers.
|
||||
See <<./transformer.adoc#Protobuf-transformers, Protocol Buffers Transformers>> for more information.
|
||||
|
||||
- The `MessageFilter` now emits a warning into logs when message is silently discarded and dropped.
|
||||
See <<./filter.adoc#filter, Filter>> for more information.
|
||||
|
||||
[[x6.1-web-sockets]]
|
||||
=== Web Sockets Changes
|
||||
|
||||
Reference in New Issue
Block a user