【问题标题】:Spring Integration: Move file in remote SFTP location after processingSpring Integration:处理后将文件移动到远程 SFTP 位置
【发布时间】:2014-12-03 12:03:47
【问题描述】:

我有一个条件,我应该通过以下步骤从 SFTP 远程位置读取文件:

  1. 从 SFTP 位置读取文件,例如 input 文件夹。

  2. 处理文件并使用文件内容调用 REST API。

  3. 如果调用成功,将远程 SFTP 文件移动到archive 文件夹,否则将远程 SFTP 文件移动到 error 文件夹。

我正在考虑两种方法。我不知道哪些是可能的。

首先,从 SFTP 远程位置读取文件到本地并从远程删除它。然后调用 REST API。根据 REST 调用的响应,将文件上传到远程文件夹成功或错误。

其次,将文件从 SFTP 远程位置读取到本地。然后调用 REST API。根据 REST 调用的响应,将文件上传到远程文件夹成功或错误。

谁能告诉我哪种方法可行且方便?如果您也可以提及通道适配器,我将不胜感激。

到目前为止,我可以调用 REST API。

<int-sftp:inbound-channel-adapter id="sftpInbondAdapter"
        auto-startup="true" 
        channel="receiveChannel" 
        session-factory="sftpSessionFactory"
        local-directory="${local.dir}" 
        remote-directory="${sftp.dir.input}"
        auto-create-local-directory="true" 
        delete-remote-files="true"
        filename-pattern="*.txt">
        <int:poller fixed-rate="60000" max-messages-per-poll="1" />
    </int-sftp:inbound-channel-adapter>
    <int:channel id="receiveChannel"/>

    <int:splitter input-channel="receiveChannel" output-channel="singleFile"/>
    <int:channel id="singleFile"/>

    <int:service-activator input-channel="singleFile"
        ref="sftpFileListenerImpl" method="processMessage" output-channel="costUpdate" />
    <int:channel id="costUpdate" /> 

     <int:header-enricher input-channel="costUpdate" output-channel="headerEnriched">
         <int:header name="content-type" value="application/json" />
     </int:header-enricher>
     <int:channel id="headerEnriched" />

     <int-http:outbound-gateway
        url="${cost.center.add.rest.api}" request-channel="headerEnriched"
        http-method="POST" expected-response-type="java.lang.String" reply-channel="costAdded" >
    </int-http:outbound-gateway>
    <int:publish-subscribe-channel id="costAdded" />

在评估 API 调用响应后,一旦 API 调用成功,我想将远程文件移动到远程文件夹中的另一个位置。我的问题是如何根据http:outbound-gateway的响应将远程文件移动到另一个远程位置?

【问题讨论】:

  • 抱歉,你的场景看起来不错。你面临什么问题?删除或不删除远程文件对您没有意义,因为默认情况下AcceptOnceFileListFilter 用于本地文件。
  • 实际上,在评估 API 调用响应后,一旦 API 调用成功,我想将远程文件移动到远程文件夹中的另一个位置。我的问题是如何根据http:outbound-gateway的响应将远程文件移动到另一个远程位置?
  • 现在很清楚了。您之前没有具体说明问题……Gary 的回答适合您吗?
  • http:outbound-gateway 响应后,我如何从singleFile 频道获取文件?并根据文件名如何将 sftp 位置的文件从一个文件夹移动到另一个文件夹?
  • 我编辑了我的答案;以后请尽量详细说明您的问题。

标签: java spring spring-integration


【解决方案1】:

查看retry-and-more sample,特别是Expression Evaluating Advice Demo - 它显示了如何根据上传的成功/失败采取不同的操作。

编辑:

回应您的 cmets 对 Artem 的回答;由于您有delete-remote-files="true",因此没有要移动的远程文件;您必须先将其设置为 false

然后,使用我建议的建议来处理成功或失败 - 使用ftp:outbound-gatewaymv 远程文件。请参阅gateway documentation for the mv command

【讨论】:

  • 我可以通过 SFTP 使用 ftp:outbound-gateway 吗?
  • 是的;有一个SFTP version。在提问之前查阅文档通常是个好主意。
  • 谢谢。不过,我还有最后一个困惑。在该频道之后,我有特定文件到singleFile 频道,其他后续频道中不会有文件消息。当远程位置有多个文件时,如何根据http:outbound-gateway 的响应知道要从远程文件夹中移动哪个文件?
  • 只需将原始文件存储在邮件头中即可。而且你已经使用&lt;header-enricher&gt;
猜你喜欢
  • 2013-10-01
  • 1970-01-01
  • 2018-09-17
  • 2022-11-28
  • 2016-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多