【问题标题】:SFTP Spring Integrations rm commandSFTP Spring 集成 rm 命令
【发布时间】:2015-07-07 11:43:07
【问题描述】:

我想知道是否有人可以提供帮助,我知道我做错了,我正在撕扯我的头发。我的目标是在 Spring Batch 作业中使用 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 和远程文件名到 ditest.txt,这显然不是我想要的,所以我添加了一个尾随 / 到目标目录在我的属性文件中,这很好地解决了这个错误!

我现在可以按照我的意愿从远程服务器中删除文件,但是我收到了关于 no reply-channel 的错误,所以我添加了以下频道

<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 还很陌生!

【问题讨论】:

  • 粘贴整个日志。另外,您是否有权以用户身份删除文件?服务器在您尝试删除文件的机器上记录什么。
  • 我很好奇你为什么在targetDirectory 属性中有一个file: 前缀;您能否展示该 setter 的定义方式以及标题的 targetDirectory 值在哪里获得。
  • 嗨,Gary,好地方,我相信“文件:”符号有误,我正在处理一些现有的未完成代码。这些值位于 JAVA_OPTS 中指定的属性文件中,然后选取应用程序,例如-DpropertiesFile=/my/dir/batch.properties

标签: spring spring-batch spring-integration


【解决方案1】:

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

这就像 unix 上的 /dev/null

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    相关资源
    最近更新 更多