1
votes

I'm trying to remove out-of-date files in an sftp remote folder by using :

<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>

before getting into gateway there is a filter to select only the files out of date :

@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;
}

The questions are:

  1. why the header 'file_remoteFile' are not created automatically ?
  2. when the remote folder is empty and there is nothing to remove, the program can't stop. How should I solve this ?
1

1 Answers

0
votes

The FileHeaders.REMOTE_FILE, as well as FileHeaders.REMOTE_DIRECTORY, is created automatically by prducing components. Since you are going to remove remote files manually, you have to specify those headers manually as well. Or use any other properties to build remote path for removal in that expression.

Another your question isn't clear.

I have just tested and ended up with this when there is no remote file to delete:

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)

Therefore there is an exception thrown if nothing to remove.

Please, elaborate more how your program can't stop or what else happens around when you try to remove nonexistent remove file.