java - 如何通过 ftp 入站 channel 适配器轮询本地 ftp 目录?

标签 java spring ftp spring-integration

我是 spring 集成的新手。如果我问的是普通问题,请原谅我。我确实有一个简单的用例。我已经如下配置了 ftp 入站 channel 适配器。使用它我可以在本地目录中提取远程检查文件。即 D:/出站。现在我想轮询本地目录,即 i.e.D:/Outbound 以添加更多业务逻辑。即根据检查文件状态从远程下载原始文件并将相关检查文件移动到其他目录位置。我应该怎么做?任何示例快速代码或引用指南/链接将不胜感激。

    <?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 的评论,我已经使用了服务激活器并能够下载原始源文件。即*。TXT。现在我想在下载原始源文件后将关联的检查文件移动到同一文件夹。即 *.chk 到 D:/outBound/original。有什么想法吗?

最佳答案

所做的只是将文件转储到队列 channel 中 - 我假设您是从示例应用程序中获得的。

参见 spring integration reference .

一个简单的应用程序可能会执行此操作(从 channel 中删除 <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) {
        ...
    }
}

编辑

我不完全确定你的用例是什么,但听起来你只想获取 .txt .chk 时的文件文件存在。

入站 channel 适配器并不真正适合于此 - 它所做的只是将本地目录同步到远程目录,听起来您需要这样的东西,将 GET 命令与出站 channel 适配器一起使用...

<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) 文件并获取您感兴趣的文件。

关于java - 如何通过 ftp 入站 channel 适配器轮询本地 ftp 目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34047634/

相关文章:

java - 选择文件路径未在android studio中转换为文件

eclipse - 更新sts或安装扩展时出现问题

java - 如何将 "filter"对象传递给 spring 数据 jpa 存储库以创建查询

Python流从FTP服务器到Flask服务器进行下载

Python FTP "chunk"迭代器(不将整个文件加载到内存中)

java - 如何获得通配符通用字段的上限?

java - Xerces-J 对未解析的 xsi :type? 的宽松验证失败是否正确

java - 使用外部登录页面的 Spring Security OAuth2 登录

java - 无法 Autowiring 。未找到 'NoteRepository' 类型的 beans

php - ftp_login() 错误