Spring3 Examples

Spring 3 Integration File Example
Author : Ishtek






In this page, I shall try to show an example using Spring 3 Integration feature with the file
related extension.

Let us try to create a setup using Maven and appropriate POM file as shown somewhere below lines
of code.

Objective of this example is to move any file placed in a particular folder to another folder
as configured in the spring context application XML file.

I shall be using Spring 3 Integration module, and a folder structure as suggested by any Maven
project.
You can start with any folder in your file system.
You can create two main folders such as 'src' and 'target',
place the POM XML file as shown in this page into the root folder.

Execute -> mvn package

After successfully executing above command, either you can prefer to use these files 
as shown in this page, or just create main files such as 'springintegration-example.xml' and
'TestSpring3Integration.java' under appropriate folder structure.

.classpath and .project can be created by using following command:

mvn eclipse:eclipse

and you have to create a variable with name 'M2_REPO' in the Preference -> Java compiler ->
user variable option in Eclipse IDE, after importing the project created using above command and files,
in order to have a compilation free workspace and project.

If everything is setup properly, and on executing the Main program, on console there will be a polling
for the folder configured with configured delays.

Now place any file in the source folder to see that this file is moved to the destination folder,
and is deleted from the source folder. 
How interesting, isn't it!!!

If you come across any errors or difficulties, please use below form to express your queries,
I shall try to answer or solve your queries... no timebound response of course.

Spring3-Integration-File-Example/.classpath

<?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/> <classpathentry kind="src" path="src/main/java" including="**/*.java"/> <classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/> <classpathentry kind="output" path="target/classes"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/> <classpathentry kind="var" path="M2_REPO/org/springframework/spring-core/3.2.6.RELEASE/spring-core-3.2.6.RELEASE.jar"/> <classpathentry kind="var" path="M2_REPO/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar"/> <classpathentry kind="var" path="M2_REPO/org/springframework/spring-expression/3.2.6.RELEASE/spring-expression-3.2.6.RELEASE.jar"/> <classpathentry kind="var" path="M2_REPO/org/springframework/spring-beans/3.2.6.RELEASE/spring-beans-3.2.6.RELEASE.jar"/> <classpathentry kind="var" path="M2_REPO/org/springframework/spring-aop/3.2.6.RELEASE/spring-aop-3.2.6.RELEASE.jar"/> <classpathentry kind="var" path="M2_REPO/aopalliance/aopalliance/1.0/aopalliance-1.0.jar"/> <classpathentry kind="var" path="M2_REPO/org/springframework/spring-context/3.2.6.RELEASE/spring-context-3.2.6.RELEASE.jar"/> <classpathentry kind="var" path="M2_REPO/org/springframework/spring-context-support/3.2.6.RELEASE/spring-context-support-3.2.6.RELEASE.jar"/> <classpathentry kind="var" path="M2_REPO/cglib/cglib/3.0/cglib-3.0.jar"/> <classpathentry kind="var" path="M2_REPO/org/ow2/asm/asm/4.0/asm-4.0.jar"/> <classpathentry kind="var" path="M2_REPO/asm/asm-util/2.2.3/asm-util-2.2.3.jar"/> <classpathentry kind="var" path="M2_REPO/asm/asm-tree/2.2.3/asm-tree-2.2.3.jar"/> <classpathentry kind="var" path="M2_REPO/asm/asm/2.2.3/asm-2.2.3.jar"/> <classpathentry kind="var" path="M2_REPO/org/springframework/integration/spring-integration-core/3.0.0.RELEASE/spring-integration-core-3.0.0.RELEASE.jar"/> <classpathentry kind="var" path="M2_REPO/org/springframework/retry/spring-retry/1.0.3.RELEASE/spring-retry-1.0.3.RELEASE.jar"/> <classpathentry kind="var" path="M2_REPO/org/springframework/spring-tx/3.2.6.RELEASE/spring-tx-3.2.6.RELEASE.jar"/> <classpathentry kind="var" path="M2_REPO/org/springframework/integration/spring-integration-file/3.0.0.RELEASE/spring-integration-file-3.0.0.RELEASE.jar"/> <classpathentry kind="var" path="M2_REPO/commons-io/commons-io/2.4/commons-io-2.4.jar"/> </classpath>


Spring3-Integration-File-Example/.project

<?xml version="1.0" encoding="UTF-8"?> <projectDescription> <name>Spring3IntegrationExample</name> <comment>NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment> <projects/> <buildSpec> <buildCommand> <name>org.eclipse.jdt.core.javabuilder</name> </buildCommand> </buildSpec> <natures> <nature>org.eclipse.jdt.core.javanature</nature> </natures> </projectDescription>



Spring3-Integration-File-Example/pom.xml

<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.techiecommunity</groupId> <artifactId>Spring3IntegrationExample</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>Spring3IntegrationExample</name> <url>http://maven.apache.org</url> <properties> <spring3.version>3.2.6.RELEASE</spring3.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring3.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring3.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring3.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring3.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring3.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring3.version}</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.0</version> </dependency> <dependency> <groupId>asm</groupId> <artifactId>asm-util</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-core</artifactId> <version>3.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-file</artifactId> <version>3.0.0.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>


Spring3-Integration-File-Example/src/main/java/net/techiecommunity/TestSpring3Integration.java

package net.techiecommunity; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestSpring3Integration { /** * @param args */ public static void main(String[] args) { try { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("springintegration-example.xml"); context.start(); } catch (Exception ex) { ex.printStackTrace(); } } }


Spring3-Integration-File-Example/src/main/resources/springintegration-example.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" 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-3.0.xsd http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd" xmlns:integration="http://www.springframework.org/schema/integration" xmlns:integration-file="http://www.springframework.org/schema/integration/file"> <integration:channel id="filein"></integration:channel> <integration-file:inbound-channel-adapter id="filesIn" directory="d:/test/" channel="filein" > <integration:poller id="fileInPooler" fixed-delay="5000"/> </integration-file:inbound-channel-adapter> <integration-file:outbound-channel-adapter id="filesOut" directory="d:/test/tmp/" channel="filein" delete-source-files="true"/> </beans>



If interested in commenting related to Spring 3 Integration File Example
Click Here

Name (To be shown on this Page:*

Email (Not to be shown):*

Website (To be shown on this Page)

Your Comments here (To be shown on this Page):*

(You can use <a> Tag in your comments)

Please enter same number as shown here 775

Name: RameshDated: 2013-12-23 01:10:39
Thanks for this simple to understand and it worked easily in my machine.    
Name: GirishDated: 2013-12-28 23:28:18
How to apply certain business logic, like parsing certain strings from the input text file,
creating XML files based on validation rules.

And then sending modified data onto another output file.
Reply
Click Here

Name (To be shown on Page):*

Email (Optional): (never shown)

Website (To be shown on this Page)

Your Comments here (To be shown on Page):*

Please enter same number as shown here 367

















   
The content provided in this page is NOT warranted and/or NOT guaranteed by techiecommunity.net .
techiecommunity.net is not liable for any negative consequences that may result/arise from 
implementing directly/indirectly any information covered in these pages/articles/tutorials.

For any of the content, if you would like to bring it to notice for removal from this web site, 
please write to this web site administrator @ EMAIL-ID (please remove space before and after @),
with appropriate concern and supporting proof(s). After thorough review and if found genuine concern,
we would take appropriate action and remove disputed content from this web site within 24 hours 
starting from the time it has brought to techiecommunity.net Administrator notice.

While using this web site, you agree to have read and accepted techiecommunity.net terms of use and privacy policy.