anu pallavi
anu pallavi

Reputation: 23

spring integration sftp channel

In sftp remote - I have 2 folder [ready] and [process] , What I need to do is first I have to move file from ready to process then I move that file to local directory using single channel . Please check my code is this correct ? my code works fine but I have doubt that first it moves to remote process or local folder which happening first ?

@Bean
public IntegrationFlow remoteToLocal() {
    return IntegrationFlows
            .from(Sftp.inboundAdapter(sftpSessionFactory())
                            .remoteDirectory(sftpProperties.getRemoteRootDir() + "/ready")
                            .regexFilter(FILE_PATTERN_REGEX)
                            .deleteRemoteFiles(true)
                            .localDirectory(new File(mmFileProperties.getMcfItes()+ mmFileProperties.getInboundDirectory()))
                            .preserveTimestamp(true)
                            .temporaryFileSuffix(".tmp"),
                    e -> e.poller(Pollers.fixedDelay(sftpProperties.getPollerIntervalMs()))
                            .id("sftpInboundAdapter"))
            .handle(Sftp.outboundAdapter(mmSftpSessionFactory())
                    .remoteDirectory(sftpProperties.getRemoteRootDir() + "/process")
                    .temporaryFileSuffix(".tmp"))

            .get();
}

Please check the new code but it it is not working

private StandardIntegrationFlow remoteToLocalFlow(final String localDirectory, final String remoteDirectoryProcessing, final String adapterName) {
        return IntegrationFlows
            .from(Sftp.inboundAdapter(mmSftpSessionFactory())
                            .remoteDirectory(remoteRootDir + remoteDirectoryProcessing)
                            .regexFilter(FILE_PATTERN_REGEX)
                            .deleteRemoteFiles(true)
                            .localDirectory(Paths.get(localDirectory).toFile())
                            .preserveTimestamp(true)
                            .temporaryFileSuffix(".tmp"),
                    e -> {
                        e.poller(Pollers.fixedDelay(mmSftpProperties.getPollerIntervalMs()))
                                .id(adapterName);
                    })
            .handle(m -> logger.trace("File received from sftp interface: {}", m))
            .handleWithAdapter(h -> h.sftpGateway(sftpSessionFactory(),AbstractRemoteFileOutboundGateway.Command.MV, "payload")
            .renameExpression(remoteRootDir + ready) 
            .localDirectoryExpression(remoteRootDir + process)).get(); }

Upvotes: 0

Views: 551

Answers (1)

Gary Russell
Gary Russell

Reputation: 174709

It looks ok, but it's not the best way to do it; you are copying the file, deleting it and sending it back with another name.

Use an SftpOutboundGateway with a MV (move) command instead.

You can also use a gateway to list and get files.

Upvotes: 0

Related Questions