`
Dead_knight
  • 浏览: 1204376 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
博客专栏
752c8642-b795-3fe6-946e-a4e845bffdec
Spring Securi...
浏览量:241183
33caa84e-18a6-3036-a82b-6e2106a4de63
clojure专题
浏览量:49255
E17ca077-44df-3816-a3fe-471c43f6e1e5
WebLogic11g
浏览量:237374
社区版块
存档分类
最新评论

Spring Integration-File Adapter使用

 
阅读更多

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"/>

 

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

4
1
分享到:
评论

相关推荐

    spring-integration-samples:Spring 集成示例

    在 "spring-integration-samples" 压缩包中,我们可以期待找到一系列示例项目,这些项目将深入演示如何使用 Spring Integration 来解决实际问题。 1. **Spring 集成核心概念** - **Channel(通道)**:Spring ...

    Spring-Integration-Example-1:该项目包括使用入站和出站网关,变压器,增强器和文件生成器。 使用Maven构建

    例如,一个File Outbound Channel Adapter 可以接收消息并将它们写入指定的文件路径。 6. **Maven**:Maven 是一个流行的Java项目管理工具,用于构建、依赖管理和项目信息管理。在这个项目中,Maven配置文件(pom....

    ftpserver配置方式

    在Spring中,我们可以使用`org.springframework.integration.ftp`包来实现FTP功能。首先,引入Spring Integration的FTP依赖: ```xml &lt;groupId&gt;org.springframework.integration &lt;artifactId&gt;spring-integration...

    Spring中文帮助文档

    2.6.4. 将Spring 应用程序上下文部署为JCA adapter 2.6.5. 计划任务 2.6.6. 对Java 5 (Tiger) 支持 2.7. 移植到Spring 2.5 2.7.1. 改变 2.8. 更新的样例应用 2.9. 改进的文档 I. 核心技术 3. IoC(控制反转)...

    Spring API

    2.6.4. 将Spring 应用程序上下文部署为JCA adapter 2.6.5. 计划任务 2.6.6. 对Java 5 (Tiger) 支持 2.7. 移植到Spring 2.5 2.7.1. 改变 2.8. 更新的样例应用 2.9. 改进的文档 I. 核心技术 3. IoC(控制反转)...

    restful restful所需要的jar包

    * Extensive integration with popular Spring IoC framework. * Deployment to Oracle 11g embedded JVM supported by special extension. Security * Supports HTTP Basic and Digest authentication (client ...

    程序员英语手册

    - **应用场景**:面向对象编程中定义接口或规范时使用。 **3. Abstract Base Class (ABC) 抽象基类** - **定义**:一种特殊的类,不能被实例化,通常包含一个或多个抽象方法,作为其他类的基础。 - **应用场景**:...

Global site tag (gtag.js) - Google Analytics