georgeos
georgeos

Reputation: 2501

Remove file via SftpOutboundGateway after processing file

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:

This code is not removing any file. Any suggestions?

Upvotes: 0

Views: 1057

Answers (1)

Gary Russell
Gary Russell

Reputation: 174574

  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.

Upvotes: 1

Related Questions