Spring Integration to迭代文件或记录列表

时间:2013-07-31 09:55:55

标签: spring-integration

我正在使用spring集成来下载文件并进行处理。

<int-sftp:inbound-channel-adapter channel="FileDownloadChannel"
                      session-factory="SftpSessionFactory"
                      remote-directory="/home/sshaji/from_disney/files"
                      filter = "modifiedFileListFilter"
                      local-directory="/home/sshaji/to_disney/downloads" 
                      auto-create-local-directory="true" >                       
       <integration:poller cron="*/10 * * * * *" default="true"/>   
</int-sftp:inbound-channel-adapter>

 <integration:transformer  input-channel="FileDownloadChannel"
                            ref="ErrorTransformer"
                            output-channel="EndChannel"/>

   <integration:router input-channel="FileErrorProcessingChannel"
                        expression="payload.getErrorCode() > 0">   
        <integration:mapping value="true" channel="ReportErrorChannel"/>
        <integration:mapping value="false" channel="FilesBackupChannel"/>
   </integration:router>

int-sftp:inbound-channel-adapter用于从sftp服务器下载文件。 它下载了大约6个文件。所有xml文件。

变换器迭代所有6个文件并检查它们是否有错误标记。 如果有错误标记,则将其错误代码设置为1.否则将设置为0。

当它从变压器出来并进入路由器时, 我想将错误代码设置为1的文件发送到特定文件夹(错误) 以及将错误代码设置为0以移动到另一个文件夹(NoError)的那些。

目前,变换器返回一个“list fileNames”,其中包含所有6个文件的errorcode和fileNames。

如何使用路由器检查每个文件的错误代码?然后将该特定文件映射到路由器。

我的问题的通用C逻辑

for (int i =0; i<fileNames.lenght();i++) {
     if(fileNames[i].getErrorCode == 1) {
     moveToErrorFolder(fileNames[i].getName());
     } else {
     moveToNoErrors(fileNames[i].getName());
     }
}

如何使用弹簧集成实现这一目标? 如果不可能,是否有任何解决方法? 我希望现在很清楚。我很抱歉上次没有提供足够的细节。

同样在int-sftp:inbound-channel-adapter中,我将“remote-directory”和“local-directory”字段硬编码到系统中的特定文件夹。我可以从bean属性或常量值中引用它们吗? 我需要根据config.xml文件配置这些值,这可能吗?。

我是Spring Integration的新手。请帮我。 在此先感谢。

1 个答案:

答案 0 :(得分:0)

我仍然不清楚你的意思是“变压器迭代了所有6个文件”。

每个文件都会在一条消息中传递给变换器,所以我看不出它是如何发出一个6的列表。

听起来你需要<aggregator/&gt;使用correlation-strategy-expression="'foo'"release-strategy-expression="size() == 6"。这会将每个File聚合成File列表并将其传递给您的变换器。然后它将其转换为包含文件名和错误代码的状态对象列表。

最后,您将添加<splitter/>,将列表拆分为单独的FileName消息以发送到路由器。

您可以使用常规Spring属性占位符作为目录属性${some.property}或SpEL来使用另一个bean #{someBean.remoteDir}的属性。