【问题标题】:spring integration sftp outbound gateway removespring 集成 sftp 出站网关删除
【发布时间】:2016-10-12 10:04:07
【问题描述】:

我正在尝试使用以下方法删除 sftp 远程文件夹中的过期文件:

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

在进入网关之前,有一个过滤器可以只选择过期的文件:

@Override
@Filter
public boolean accept(Message<?> message) {
    if (message.getPayload() instanceof FileInfo) {
        final FileInfo fileInfo = (FileInfo) message.getPayload();
        final DateTime lastModified = new DateTime(fileInfo.getModified());

        boolean accept = lastModified.plusDays(this.days).isBeforeNow();
        return accept;
    }
    return false;
}

问题是:

  1. 为什么没有自动创建标头“file_remoteFile”?
  2. 当远程文件夹为空且没有可删除的内容时,程序无法停止。我应该如何解决这个问题?

【问题讨论】:

    标签: spring-integration sftp


    【解决方案1】:

    FileHeaders.REMOTE_FILEFileHeaders.REMOTE_DIRECTORYprducing 组件自动创建。由于您要手动删除远程文件,因此您还必须手动指定这些标头。或者使用任何其他属性来构建远程路径以在该expression 中删除。

    你的另一个问题不清楚。

    当没有要删除的远程文件时,我刚刚测试并结束了这个:

    org.springframework.core.NestedIOException: Failed to remove file.; nested exception is 2: /junk
    
        at org.springframework.integration.sftp.session.SftpSession.remove(SftpSession.java:83)
    ...
    Caused by: 2: /junk
        at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2846)
        at com.jcraft.jsch.ChannelSftp.rm(ChannelSftp.java:1958)
        at org.springframework.integration.sftp.session.SftpSession.remove(SftpSession.java:79)
    

    因此,如果没有要删除的内容,则会引发异常。

    请详细说明当您尝试删除不存在的删除文件时您的程序如何无法停止或其他情况。

    【讨论】:

    • 感谢您的回答!于是我用header-enricher解决了这个问题。
    • 第二个问题,我的测试如下:
    • 对垃圾邮件感到抱歉......测试:public void can_call_sftp_bean() throws Exception { try { final Message&lt;?&gt; message = MessageBuilder.withPayload("test").build(); sftpHousekeepingInputChannel.send(message); final Message&lt;FileInfo&gt; received = (Message&lt;FileInfo&gt;) sftpHousekeepingOutputChannel.receive(); } catch (Exception e) { e.getStackTrace(); } } 并且测试无法停止。
    • 好吧,在这种情况下是真的,因为删除失败并出现异常,并且没有人会向出站通道发送回复。
    猜你喜欢
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    相关资源
    最近更新 更多