0

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(); }

1 Answers1

0

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.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Please check this is correct or not {{{ .handleWithAdapter(h -> h.sftpGateway(sftpSessionFactory(), AbstractRemoteFileOutboundGateway.Command.MV, "payload") .renameExpression(remoteRootDir + ready) .localDirectoryExpression(remoteRootDir + process)) }}} – anu pallavi Jan 14 '19 at 18:05
  • Don't put code in comments; it's too hard to read; edit your question instead and add a comment indicating you have done so, so we get a notification. – Gary Russell Jan 14 '19 at 18:18
  • "not working" is not enough information; `payload` is not correct there; the payload is the local file; you need to build the remote file name in the expression. See the [sftp sample for an example](https://github.com/spring-projects/spring-integration-samples/blob/master/basic/sftp/src/test/resources/META-INF/spring/integration/SftpOutboundGatewaySample-context.xml); it uses XML config instead of the DSL, but the same info applies; it is using the RM command to remove the remote file. You may want to use that technique anyway (LS, GET, MV gateways) rather than the inbound adapter. – Gary Russell Jan 14 '19 at 19:34