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..76948533e1d 100644 --- a/src/reference/asciidoc/filter.adoc +++ b/src/reference/asciidoc/filter.adoc @@ -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. 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