SFTP Spring 集成 rm 命令

SFTP Spring Integrations rm command

我想知道是否有人可以提供帮助,我知道我做错了而且我正在撕毁我的头发。我的目标是在 Spring 批处理作业中使用 Spring Integrations SFTP 删除远程目录中任何扩展名为 .txt 的文件。据我了解,我不必通过 ls 远程文件来删除它们,只需对给定目录的 *.txt 发出 rm 命令即可,但我可能不正确?

我有以下 SFTP 配置

<bean id="sftpSessionFactory"
    class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
    <property name="host" value="${host}" />
    <property name="port" value="${port}" />
    <property name="user" value="${user}" />        
    <property name="privateKey" value="file:${privateKey}" />
    <property name="password" value="${password}" />
</bean>

<int:channel id="rmChannel"/>

<int-sftp:outbound-gateway
        session-factory="sftpSessionFactory"
        request-channel="rmChannel"
        remote-file-separator="/"
        command="rm"
        expression="headers['file_remoteDirectory'] + headers['file_remoteFile']" />

<bean id="cleanRemoteDirectoryTasklet" class="com.example.batch.job.integration.SFTPRmTasklet" scope="step">
    <property name="channel" ref="rmChannel" />
    <property name="filePatternToDelete" value="*.txt" />
    <property name="targetDirectory" value="${remoteDirectory}"/> // edit removed 'file:' notation
</bean>

我相信我在这一点上没问题,我的问题是在 SFTPRmTasklet 的 Java 实现中执行此流程,我不确定如何构建消息以启动 sftp 删除。目前我有这样的东西只是为了启动它我知道我的有效负载是错误的。

Message<String> rmRequest = MessageBuilder.withPayload("/my/target/dir")
                .setHeader("file_remoteDirectory", targetDirectory)
                .setHeader("file_remoteFile", filePatternToDelete)
                .build();

channel.send(rmRequest)

最终这会产生一个异常

org.springframework.integration.MessagingException: org.springframework.core.NestedIOException: Failed to remove file: 2: No such file

更新 1

所以我决定只针对一个远程文件并将 filePatternToDelete 更改为 test.txt。经过一些调试后,我意识到 AbstractRemoteFileOutBoundGateway 正在将我的 remoteFilePath 评估为 /my/target/dirtest.txt 并将远程文件名评估为 dirtest.txt,这显然不是我想要的,所以我添加了一个尾随 /到我的属性文件中的目标目录,这很好地解决了这个错误!

我现在可以按照自己的意愿从远程服务器上删除文件,但是我收到关于没有回复通道的错误,所以我添加了以下通道

<int:channel id="end"/>

并修改了我的出站网关

<int-sftp:outbound-gateway
        session-factory="sftpSessionFactory"
        request-channel="rmChannel"
        reply-channel="end"
        remote-file-separator="/"
        command="rm"
        expression="headers['file_remoteDirectory'] + headers['file_remoteFile']" />

现在收到有关此频道没有订阅者的错误消息。至少有进步,以防你没有猜到我是 Spring!

的新手

如果您对 rm 的结果不感兴趣,请将 reply-channel 设置为 nullChannel

这就像 unix 上的 /dev/null