onlyring的文档库 2012-05-31
File Adapter主要应用于企业应用程序之间共享文件系统的情况,一个应用写入,而其他应用通过轮询文件系统进行读取。File Adapter从不同的文件系统提取文件再转变成框架的Message并发布至通道中,或者从通道中提取Message再转变为文件并写入文件系统中。
一、配置环境
1.下载Spring Integration
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"/>
实际上还有很多属性用于设置共享文件系统出现的各种情况。这里不再赘述。
前几篇介绍了设计模式的特性并且详细讲解了4种创建型模式,创建型模式是负责如何产生对象实例的,接下来讲讲结构型模式。结构型模式是解析类和对象的内部结构和外部组合,通过优化程序结构解决模块之间的耦合问题。