Spring Integration-3 :Filters
Filters allow, on the basis of a message’s content or metadata (in the message header), a message to pass from one channel to the next or reject and discard the message from the system – that is, not allowing the rejected message into the next channel. The filter is a “yea or nay” component determining which messages flow through and which messages do not.
- Filters are again endpoints that sit between channels that allows or rejects messages from one message channel to other.
- Filters allows some messages to pass from one channels to another channel.
- Messages not selected are discarded
- Selection occur based on message payload or message metadata(header information)
- As with Adapters, SI provides many filters out of the box
- We can create out own filter with its own custom message selection criteria
SI Built-In Filters
- Spring Integration provides several ready to use filters with the framework , you merely have to configure them to use
- Built-In filters include Expression filter , Xpath filter and XML validation filter
Expression filter – A filter that uses SPEL against the message to select messages
Xpath filter – A filter that uses XPath against the XML payload to select messages
XML Validation filter – A filter that uses XML payload messages that validate against a given schema
Examples of Filters:
Here is a filter that accepts all messages that do not start with text “Hello”
<int:filter input-channel="inboundChannel" output-channel = "outboundChannel" expression="payload.startsWith('Hello')"/>
Optionally discard channel also can be defined so filtered messages can be route using this channel.
<int:filter input-channel="inboundChannel" output-channel = "outboundChannel" discard-channel="relookChannel" expression="payload.startsWith('Hello')"/>
Custom Filters:
public class MySelector implements MessageSelector{ public Boolean accept(Message<?> msg){ if(msg.getPayload() instanceof String && msg.getPayload().startsWith("Hello")) return true;return false; }}
Above class configuration:
<int:filter input-channel="inboundChannel" output-channel = "outboundChannel" discard-channel="relookChannel" ref="selector"/><bean id="selector" class="com.dpq.MySelector"/>