Spring Integration Filters (Message Selectors) :Lab-6
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.
Not all messages are of interest. That is, there is often a need to filter out some messages
that enter a channel. Applications may only want to get messages formatted in a certain
way (XML or JSON). Applications may only be interested in certain types of data (new sales
messages but not messages about shipments). Other application may be interested in
messages with certain message headers (for example when a message was created – only
wanting to look at messages created after 5pm).
In this lab, you explore Spring Integration (SI) filters to examine messages in a channel and
accept those of interest and discard the others.
Specifically, in this lab you will:
- Implement the SI MessageSelector interface and configure an SI filter.
- Explore and configure a built-in SI XPath filter to sort XML messages.
- Work with a built-in SI Validation filter to week out non-validating XML messages
Above 3 points will be covered in 3 labs and in this lab we will focus on MessageSelector and SI Filter
Here we will filter File messages (messages with File payload) looking for
those File names that begin with a specified string. You provide the filter’s logic by
implementing the MessageSelector interface and configuring the filter in XML
spring-integration-message-selector-filter.xml.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:int="http://www.springframework.org/schema/integration" xmlns:int-file="http://www.springframework.org/schema/integration/file" xmlns:int-mail="http://www.springframework.org/schema/integration/mail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int-stream="http://www.springframework.org/schema/integration/stream" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd"> <!-- Adapter for reading files --> <int-file:inbound-channel-adapter id="producer-file-adapter" channel="inboundChannel" directory="/Users/dpq/Desktop/techWorrk/fileInbound" prevent-duplicates="true"> <int:poller fixed-rate="5000" /> </int-file:inbound-channel-adapter> <int:channel id="inboundChannel" /> <int:filter input-channel="inboundChannel" output-channel="outboundChannel" ref="selector" /> <bean id="selector" class="com.dpq.spring.integration.helpers.FileSelector" /> <!-- a direct channel --> <int:channel id="outboundChannel" /> <!-- Adapter for writing files --> <int-file:outbound-channel-adapter channel="outboundChannel" id="consumer-file-adapter" directory="/Users/dpq/Desktop/techWorrk/fileOutbound" /> <int:poller id="defaultPoller" default="true" max-messages-per-poll="5" fixed-rate="200" /></beans>
This Filter class will implement Message selector and it will check condition which is inside accept method
FileSelector.java
package com.dpq.spring.integration.helpers;import java.io.File;import org.springframework.integration.core.MessageSelector;import org.springframework.messaging.Message;public class FileSelector implements MessageSelector { @Override public boolean accept(Message<?> message) { if (message.getPayload() instanceof File && ((File) message.getPayload()).getName().startsWith("msg")) { return true; } return false; }}
Startup.java
package com.dpq.filter.containers;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.integration.support.MessageBuilder;import org.springframework.messaging.Message;import org.springframework.messaging.MessageChannel;public class MessageSelectorFilterStartup { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "spring-integration-message-selector-filter.xml"); System.out.println("Contanier started....."); while(true) { } }}
Here messages starting with ‘msg’ will be considered and same will be processed, so if we push xyz.txt,msg.txt and msg123.xml inside file inbound directory ‘/Users/dpq/Desktop/techWorrk/fileInbound‘ then only msg.txt and msg123.xml will be filtered and pushed to outbound directory “/Users/dpq/Desktop/techWorrk/fileOutbound“