2015-12-02 78 views
3

我是春季整合的新手,所以请原谅我,如果我问一些普通的东西。我有一个简单的用例。我已经配置了ftp入站通道适配器,如下所示。使用它我能够在本地目录中提取远程检查文件。 i.e.D:/出站。现在我想轮询本地目录,即D:/ Outbound来添加更多业务逻辑。即基于检查文件状态从远程下载原始文件并将相关检查文件移动到其他目录位置。我应该怎么做?任何示例快速代码或参考指南/链接将不胜感激。如何通过ftp入站通道适配器轮询本地ftp目录?

<?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-ftp="http://www.springframework.org/schema/integration/ftp" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/integration/ftp http://www.springframework.org/schema/integration/ftp/spring-integration-ftp.xsd"> 

    <context:property-placeholder location="/ftp/ftp.properties"/> 
    <context:component-scan base-package="com.sandbox.ftp"/> 

    <!-- FTP inbound channel adapter for reading the input check files --> 
    <int-ftp:inbound-channel-adapter id="ftpInbound" 
            channel="ftpInboudChannel" 
            session-factory="ftpClientFactory" 
            auto-create-local-directory="true" 
            local-directory="D:/outBound" 
            remote-directory="." 
            filename-regex=".*\.chk$"> 
      <int:poller fixed-rate="5000" /> 
    </int-ftp:inbound-channel-adapter> 

    <int:channel id="ftpInboudChannel"/> 

    <!--Service Activator which will intiates the download of original source file --> 
    <int:service-activator input-channel="ftpInboudChannel" ref="myPojo" method="processFile" /> 
    <bean id="myPojo" class="com.sandbox.ftp.CheckFileProcessor" /> 

    <!-- FTP inbound channel adapter for downloading the original remote files --> 
    <int-ftp:inbound-channel-adapter id="ftpInboundDownload" 
            channel="ftpInboudDownloadChannel" 
            session-factory="ftpClientFactory" 
            auto-create-local-directory="true" 
            local-directory="D:/outBound/original" 
            remote-directory="." 
            filename-regex=".*\.txt$"> 
      <int:poller fixed-rate="5000"/> 
    </int-ftp:inbound-channel-adapter> 

    <int:channel id="ftpInboudDownloadChannel"/> 


</beans> 

现在基于Gary的评论,我使用了服务激活器,并能够下载原始源文件.i.e。 *。文本。现在,我想在下载原始源文件后将关联的检查文件移动到同一文件夹。即* .chk到D:/ outBound/original。有什么想法吗 ?

+0

你能澄清你所说的“进一步的业务逻辑”是什么意思? SO不建议参考指南,但这不是你的问题。 – Teepeemm

+0

@Teepeemm感谢您的及时回复。在这里,通过“更进一步的商业逻辑”,对于这种用例,我的意思是“根据检查文件状态从远程下载原始文件并将相关检查文件移动到其他目录位置”。 – road2victory

回答

1

所做的就是将文件转储到队列通道中 - 我假设你从示例应用程序获取了该文件。

查看spring integration reference

一个简单的应用程序可以这样做(除去<queue/>从通道)...

<int:channel id="ftpInboudChannel"/> 

<int:service-activator input-channel="ftpInboudChannel" 
    ref="myPojo" method="processFile" /> 

<bean id="myPojo" class="foo.MyPojo" /> 

public class MyPojo { 

    public void processFile(File fileToProcess) { 
     ... 
    } 
} 

编辑

我不知道完全你的使用情况是什么,但它听起来像当.chk文件存在时,您只想获取.txt文件。

一个入站通道适配器并不适用于此 - 它只是将本地目录同步到远程目录,听起来像你需要类似的东西,使用带出站通道适配器的GET命令...

<int:channel id="ftpInboudChannel"/> 

<int:transformer input-channel="ftpInboudChannel" output-channel="ftpGet" 
    expression="payload.name.replace('chk$', 'txt')" /> 

<int:ftp-outbound-gateway request-channel="ftpGet" command="get" 
    ... 
    replyChannel="ftpGot" /> 

<int:service-activator input-channel="ftpGot" ... /> 

换句话说,按需提取文件而不是轮询它。

您还可以使用网关列表(LS)的文件,并获取那些你感兴趣的内容。

+0

@Gary非常感谢您的回复。这正是我想要的。我已经进一步更新了有关诉讼的问题。请您分享您的看法。 – road2victory

+1

看我的编辑; [ftp示例](https://github.com/spring-projects/spring-integration-samples/tree/master/basic/ftp)还有使用网关(LS,GET,RM等)的其他示例。 –