Spring Integration-File Adapter使用

onlyring的文档库 2012-05-31

File Adapter主要应用于企业应用程序之间共享文件系统的情况,一个应用写入,而其他应用通过轮询文件系统进行读取。File Adapter从不同的文件系统提取文件再转变成框架的Message并发布至通道中,或者从通道中提取Message再转变为文件并写入文件系统中。

一、配置环境

1.下载Spring Integration

http://s3.amazonaws.com/dist.springframework.org/release/INT/spring-integration-2.1.0.RELEASE-dist.zip

2.配置classpath依赖的jar

 

spring-integration-core-2.1.0.RELEASE.jar 

spring-integration-file-2.1.0.RELEASE.jar

org.springframework.asm-3.1.0.RELEASE.jar

org.springframework.aop-3.1.0.RELEASE.jar

org.springframework.beans-3.1.0.RELEASE.jar

org.springframework.core-3.1.0.RELEASE.jar

org.springframework.context-3.1.0.RELEASE.jar

org.springframework.context.support-3.1.0.RELEASE.jar

org.springframework.expression-3.1.0.RELEASE.jar

aopalliance-1.0.jar

commons-logging-1.1.1.jar

二、XML配置文件:spring-integration-file-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:int-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-2.1.xsd
        http://www.springframework.org/schema/integration/file
		http://www.springframework.org/schema/integration/file/spring-integration-file-2.1.xsd">
	<!-- 定义通道 -->
	<int:channel id="file-channel"/>
	
	<!-- 文件类型-输入通道适配器 -->
	<int-file:inbound-channel-adapter
		directory="D:/spring-integration-samples/input" channel="file-channel">
	    <int:poller fixed-rate="1000"/>
	</int-file:inbound-channel-adapter>
	
	<!-- 文件类型-输出通道适配器 -->
	<int-file:outbound-channel-adapter directory="D:/spring-integration-samples/output" channel="file-channel"/>
</beans>

三、编写测试类

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestFileSupport {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
		"spring-integration-file-context.xml");
	}
}

四、示例说明

实现功能:每秒间隔轮询D:/spring-integration-samples/input目录,如果发现该目录有文件,就依次复制到D:/spring-integration-samples/output目录中。实际上就是文件copy的功能

该示例使用了Spring Integration提供的File适配器。

深入File Adapter

1.防止重复读取:prevent-duplicates="true"

<int-file:inbound-channel-adapter
        directory="D:/spring-integration-samples/input" 
        channel="file-channel" prevent-duplicates="true">

2.文件过滤:filename-pattern="*.doc"或者filename-regex="[ABC]_positions.doc"

<int-file:inbound-channel-adapter
        directory="D:/spring-integration-samples/input" 
        channel="file-channel" filename-pattern="*.doc">
或者
<int-file:inbound-channel-adapter
        directory="D:/spring-integration-samples/input" 
        channel="file-channel" filename-regex="[ABC]_positions.doc">

3.文件锁:<file:nio-locker/>

<int-file:inbound-channel-adapter
        directory="D:/spring-integration-samples/input" 
        channel="file-channel" filename-regex="[ABC]_positions.doc">
    <file:nio-locker/>
    ……
</int-file:inbound-channel-adapter>

4.删除源文件:delete-source-files="true"

<int-file:outbound-channel-adapter 
        directory="D:/spring-integration-samples/output" 
        channel="file-channel" delete-source-files="true"/>

实际上还有很多属性用于设置共享文件系统出现的各种情况。这里不再赘述。

相关推荐