From 16f81bc5890314c2186048dcbe098e50a45568db Mon Sep 17 00:00:00 2001 From: abilan Date: Mon, 20 Mar 2023 11:42:52 -0400 Subject: [PATCH 1/3] 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 --- .../integration/filter/MessageFilter.java | 24 +++++++++++-------- src/reference/asciidoc/filter.adoc | 2 ++ src/reference/asciidoc/whats-new.adoc | 2 ++ 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/filter/MessageFilter.java b/spring-integration-core/src/main/java/org/springframework/integration/filter/MessageFilter.java index 049018db24f..697ecf293ee 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/filter/MessageFilter.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/filter/MessageFilter.java @@ -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.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 @@ /** * 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 MessageFilter(MessageSelector selector) { * {@link MessageRejectedException} when its selector does not accept a * Message. The default value is false meaning that rejected * Messages will be quietly dropped or sent to the discard channel if - * available. Typically this value would not be true when + * available. Typically, this value would not be true 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 then the exception will be thrown). @@ -179,13 +180,16 @@ protected Object doHandleRequestMessage(Message message) { @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; } diff --git a/src/reference/asciidoc/filter.adoc b/src/reference/asciidoc/filter.adoc index eb2f9c44295..6ffd0dbe74a 100644 --- a/src/reference/asciidoc/filter.adoc +++ b/src/reference/asciidoc/filter.adoc @@ -88,6 +88,8 @@ If you want rejected messages to be routed to a specific channel, provide that r ---- ==== +If the `throwExceptionOnRejection == false` and no `discardChannel` provided, the message is silently dropped and an `o.s.i.filter.MessageFilter` instance just emits a warning into logs (starting with version 6.1) about this discarded message. + See also <<./handler-advice.adoc#advising-filters,Advising Filters>>. NOTE: Message filters are commonly used in conjunction with a publish-subscribe channel. diff --git a/src/reference/asciidoc/whats-new.adoc b/src/reference/asciidoc/whats-new.adoc index 6f952ebabf5..34bc21f6a97 100644 --- a/src/reference/asciidoc/whats-new.adoc +++ b/src/reference/asciidoc/whats-new.adoc @@ -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 From 571543a00fd82232747917c6894a96eb95c33a52 Mon Sep 17 00:00:00 2001 From: abilan Date: Mon, 20 Mar 2023 12:42:21 -0400 Subject: [PATCH 2/3] * Mention `nullChannel` variant to ignore even warn --- src/reference/asciidoc/filter.adoc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/reference/asciidoc/filter.adoc b/src/reference/asciidoc/filter.adoc index 6ffd0dbe74a..8e4612e72ac 100644 --- a/src/reference/asciidoc/filter.adoc +++ b/src/reference/asciidoc/filter.adoc @@ -89,6 +89,8 @@ If you want rejected messages to be routed to a specific channel, provide that r ==== If the `throwExceptionOnRejection == false` and no `discardChannel` provided, the message is silently dropped and an `o.s.i.filter.MessageFilter` instance just emits a warning into logs (starting with version 6.1) about this discarded message. +To fully drop the message off and even lose the mentioned warning in logs, a `nullChannel` can be configured as a `discardChannel` on the filter. +This way, the goal of the framework is to never be silent by default until an explicit option is set. See also <<./handler-advice.adoc#advising-filters,Advising Filters>>. From 6a436fb1c2c1aca71473b52f9449c28d24ce287e Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Mon, 20 Mar 2023 12:54:16 -0400 Subject: [PATCH 3/3] Fix language in docs Co-authored-by: Gary Russell --- src/reference/asciidoc/filter.adoc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/reference/asciidoc/filter.adoc b/src/reference/asciidoc/filter.adoc index 8e4612e72ac..76948533e1d 100644 --- a/src/reference/asciidoc/filter.adoc +++ b/src/reference/asciidoc/filter.adoc @@ -88,9 +88,9 @@ If you want rejected messages to be routed to a specific channel, provide that r ---- ==== -If the `throwExceptionOnRejection == false` and no `discardChannel` provided, the message is silently dropped and an `o.s.i.filter.MessageFilter` instance just emits a warning into logs (starting with version 6.1) about this discarded message. -To fully drop the message off and even lose the mentioned warning in logs, a `nullChannel` can be configured as a `discardChannel` on the filter. -This way, the goal of the framework is to never be silent by default until an explicit option is set. +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>>.