Java Articles

Advertisement

Spring Integration File Adapter Example

Share

In this article, we will see an example of spring integration’s file adapter. We will see a simple example of copying a properties file from one directory to another. We only have to copy the properties file so will also add a filter on the file extension. Let’s begin with our example.

Dependencies

Add the following dependencies:

  1. spring-core
  2. spring-context
  3. spring-integration-core
  4. spring-integration-file - This is necessary to access the file adapter

pom.xml:

Advertisement
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javarticles.spring.integration.jms</groupId>
<artifactId>springintegrationjms</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-file</artifactId>
<version>4.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>

<properties>
<spring.version>4.1.4.RELEASE</spring.version>
</properties>

</project>

Copying File using spring integration file adapter

If you want to move the files instead of copying, modify outbound-channel-adapter, add attribute delete-source-files with its value set to true.
Inbound file adapter inbound-channel-adapter reads the property files from output folder. Attribute filter takes care of filtering the files, only files with *.properties are allowed. The files are copied, encapsulated in form of Messages and sent to channel filesIn. The outbound file adapter listening on filesIn sends it to the outbound channel filesOut. It also has a reference to a file handler, in case you want to do something before copying, like tweak the file, this is where you can. The file handler can be any POJO.

fileCopyApplicationContext.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"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<context:property-placeholder />

<file:inbound-channel-adapter id="filesIn"
directory="file:${user.dir}/input" filter="onlyPropertyFiles"
auto-startup="true">
<int:poller id="poller" fixed-delay="5000" />
</file:inbound-channel-adapter>

<int:service-activator input-channel="filesIn"
output-channel="filesOut" ref="handler" />

<file:outbound-channel-adapter id="filesOut"
directory="file:${user.dir}/output"/>

<bean id="handler" class="com.javarticles.spring.integration.file.FileHandler" />
<bean id="onlyPropertyFiles"
class="org.springframework.integration.file.config.FileListFilterFactoryBean"
p:filenamePattern="*.properties" />
</beans>
File Adapter
File Adapter
Advertisement

FileHandler:

package com.javarticles.spring.integration.file;

import java.io.File;

public class FileHandler {
public File handleFile(File input) {
System.out.println("Copying file: " + input.getAbsolutePath());
return input;
}
}

SpringIntegrationFileCopyExample:

package com.javarticles.spring.integration.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringIntegrationFileCopyExample {

public static void main(String[] args) throws InterruptedException, IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"fileCopyApplicationContext.xml");
try {
Thread.sleep(2000);
Properties prop = new Properties();
FileInputStream is = new FileInputStream(new File(System.getProperty("user.dir") + "/output/fileToCopy.properties"));
prop.load(is);
System.out.println(prop);
} finally {
context.close();
}
}

}

fileToCopy.properties

site=javarticles.com

Output:

Copying file: C:\javarticles_ws\springintegrationFile\input\fileToCopy.properties
{site=javarticles.com}

Download the source code

This was an example about spring integration file adapter. You can download the source code here: springintegrationFile.zip

Advertisement

Share

Related

Advertisement

Latest

Advertisement