This example explains how to modify, rename and move the file to destination folder using Sprig Integration(SI) file:inbound adapter and file:outbound adapter.
SI provides Inbound and outbound adapter for many systems like File system, JMS, UDP , TCP, Mail Web Services, JDBC and RMI.
Here we are going to see the example of File System.
Step 1. create a spring context file. spring-context.xml
<?xml version ="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:file="http://www.springframework.org/schema/integration/file"
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/file
http://www.springframework.org/schema/integration/file/spring-integration-file.xsd">
<file:inbound-channel-adapter id="incomingFiles"
directory="file:C:\Temp\InboundLocation>
<int:poller id="poller" fixed-delay="5000"/>
</file:inbound-channel-adapter>
<file:outbound-channel-adapter id="outgoingFiles"
directory="file:C:\Temp\OutboundLocation" delete-source-files="true" filename-generator ="filenameGenerator"/>
<bean id="filenameGenerator" class="com.test.si.FilenameGenerator"/>
</beans>
The poller look into in InboundLocation for each 5 seconds, if any new files found then modify the file content if required and rename the file to ' NewFile' moves to OutboundLocation then deletes the original file.
prevent-duplicates=true by default this property is false, enabling this property it prevents moving duplicates.
filename-pattern="*.TXT" filters to only specific extension files.
Add these properties to In-bound adapter.
Step 2. Create file name generator class
package com.test.si;
import java.io.File;
import org.springframework.integration.Message;
import org.springframework.integration.file.DefaultFileNameGenerator;
public class FilenameGenerator extends DefaultFileNameGenerator
{
public FilenameGenerator() {
super();
}
@Override
public String generateFileName(Message<?> message) {
File inputFile = (File)message.getPayload();
String originalFileName = super.generateFileName(message);
System.out.println("File Name: " + originalFileName);
//TODO: Read the file
//TODO: Write the file
String newFileName = "NewFile";
return newFileName;
}
}
Step 3. run the example.
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testSpringIntegration {
public testSpringIntegration() {
super();
}
public static void main(String[] args) {
testSpringIntegration testSpringIntegration = new testSpringIntegration();
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
}
}
SI provides Inbound and outbound adapter for many systems like File system, JMS, UDP , TCP, Mail Web Services, JDBC and RMI.
Here we are going to see the example of File System.
Step 1. create a spring context file. spring-context.xml
<?xml version ="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:file="http://www.springframework.org/schema/integration/file"
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/file
http://www.springframework.org/schema/integration/file/spring-integration-file.xsd">
<file:inbound-channel-adapter id="incomingFiles"
directory="file:C:\Temp\InboundLocation>
<int:poller id="poller" fixed-delay="5000"/>
</file:inbound-channel-adapter>
<file:outbound-channel-adapter id="outgoingFiles"
directory="file:C:\Temp\OutboundLocation" delete-source-files="true" filename-generator ="filenameGenerator"/>
<bean id="filenameGenerator" class="com.test.si.FilenameGenerator"/>
</beans>
The poller look into in InboundLocation for each 5 seconds, if any new files found then modify the file content if required and rename the file to ' NewFile' moves to OutboundLocation then deletes the original file.
prevent-duplicates=true by default this property is false, enabling this property it prevents moving duplicates.
filename-pattern="*.TXT" filters to only specific extension files.
Add these properties to In-bound adapter.
Step 2. Create file name generator class
package com.test.si;
import java.io.File;
import org.springframework.integration.Message;
import org.springframework.integration.file.DefaultFileNameGenerator;
public class FilenameGenerator extends DefaultFileNameGenerator
{
public FilenameGenerator() {
super();
}
@Override
public String generateFileName(Message<?> message) {
File inputFile = (File)message.getPayload();
String originalFileName = super.generateFileName(message);
System.out.println("File Name: " + originalFileName);
//TODO: Read the file
//TODO: Write the file
String newFileName = "NewFile";
return newFileName;
}
}
Step 3. run the example.
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testSpringIntegration {
public testSpringIntegration() {
super();
}
public static void main(String[] args) {
testSpringIntegration testSpringIntegration = new testSpringIntegration();
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
}
}