0
votes

I'm using Spring Integration to read files from a SFTP Server and everything works fine using an InboundChannelAdapter with Java Configuration.

Now, i want to modify my process in order to remove all processed files from SFTP Server. Therefore I want to use an SFTP OutboundGateway with Java Configuration. This is my new code with few modifications based on https://docs.spring.io/spring-integration/docs/5.0.0.BUILD-SNAPSHOT/reference/html/sftp.html#sftp-outbound-gateway:

@Configuration
public class SftpConfiguration {

    @Value("${sftp.host}")
    String sftpHost = "";

    @Value("${sftp.user}")
    String sftpUser = "";

    @Value("${sftp.pass}")
    String sftpPass = "";

    @Value("${sftp.port}")
    Integer sftpPort = 0;

    @Bean
    public SessionFactory<LsEntry> sftpSessionFactory() {
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
        factory.setHost(sftpHost);
        factory.setPort(sftpPort);
        factory.setUser(sftpUser);
        factory.setPassword(sftpPass);
        factory.setAllowUnknownKeys(true);
        return new CachingSessionFactory<LsEntry>(factory);
    }

    @Bean
    public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
        SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
        fileSynchronizer.setDeleteRemoteFiles(false);
        fileSynchronizer.setRemoteDirectory("/upload/");
        return fileSynchronizer;
    }

    @Bean
    @InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(cron = "0 * * * * ?"))
    public MessageSource<File> sftpMessageSource() {
        SftpInboundFileSynchronizingMessageSource source =
                new SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer());
        source.setLocalDirectory(new File("sftp-folder"));
        source.setAutoCreateLocalDirectory(true);
        return source;
    }

    @Bean
    @ServiceActivator(inputChannel = "sftpChannel")
    MessageHandler handler() {
        return new MessageHandler() {

            @Override
            public void handleMessage(Message<?> message) throws MessagingException {
                File f = (File) message.getPayload();
                try {
                    myProcessingClass.processFile(f);
                    SftpOutboundGateway sftpOG = new SftpOutboundGateway(sftpSessionFactory(), "rm",
                        "'/upload/" + f.getName() + "'");
                } catch(QuoCrmException e) {
                    logger.error("File [ Process with errors, file won't be deleted: " + e.getMessage() + "]");
                }
            }

        };
    }

}

Modifications are:

  • I defined my fileSynchronizer to setDeleteRemoteFiles(false), in order to remove files manually according to my process.
  • In my MessageHandler, I added my SFTPOutboundGateway and if there is no exception, it means the processing was successful and removes the file (but if there is an exception won't delete the file).

This code is not removing any file. Any suggestions?

1

1 Answers

1
votes
  1. You shouldn't create a new gateway for each request (which is what you are doing).

  2. You are not doing anything with sftpOG after you create it anyway; you need to send a message to the gateway.

You can create a reply-producing handler and wire it's output channel to the gateway (which should be its own @Bean).

Or, you can simply use an SftpRemoteFileTemplate to remove the file - but again, you only need one, you don't need to create a new one for each request.