Spring integration - SFTP rename or move file in r

2019-07-19 15:07发布

I am trying to move or rename remote file instead of deleting the remote file after download, I found that it can be done by outbound gateway move command, but could not find proper way to do it.

Please help in rename the file after download.

@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public SessionFactory<LsEntry> sftpSessionFactory(
        final DataloadServiceProperties DataloadServiceProperties) {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
    factory.setHost(DataloadServiceProperties.getSftpHost());
    factory.setPort(DataloadServiceProperties.getSftpPort());
    factory.setUser(DataloadServiceProperties.getSftpUser());
    if (DataloadServiceProperties.getSftpPrivateKey() != null) {
        factory.setPrivateKey(DataloadServiceProperties.getSftpPrivateKey());
        factory.setPrivateKeyPassphrase(
                DataloadServiceProperties.getSftpPrivateKeyPassphrase());
    }
    else {
        factory.setPassword(DataloadServiceProperties.getSftpPasword());
    }
    factory.setAllowUnknownKeys(true);
    return new CachingSessionFactory<LsEntry>(factory);
}

@Bean
@Order(Ordered.HIGHEST_PRECEDENCE - 1)
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer(
        final SessionFactory<LsEntry> sftpSessionFactory,
        final DataloadServiceProperties DataloadServiceProperties) {
    SftpInboundFileSynchronizer fileSynchronizer =
            new SftpInboundFileSynchronizer(sftpSessionFactory);
    fileSynchronizer.setDeleteRemoteFiles(false);
    fileSynchronizer.setRemoteDirectory(
            DataloadServiceProperties.getSftpRemoteDirectoryDownload());
    fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter(
            DataloadServiceProperties.getSftpRemoteDirectoryDownloadFilter()));
    return fileSynchronizer;
}

Inbound channel for looking file in SFTP server

@Bean
@Order(Ordered.HIGHEST_PRECEDENCE - 2)
@InboundChannelAdapter(
        channel = "fromSftpChannel",
        poller = @Poller(
                cron = "${sftp.poller.cron}"))
public MessageSource<File> sftpMessageSource(
        final SftpInboundFileSynchronizer sftpInboundFileSynchronizer,
        final DataloadServiceProperties DataloadServiceProperties) {
    SftpInboundFileSynchronizingMessageSource source =
            new SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer);
    source.setLocalDirectory(
            new File(DataloadServiceProperties.getSftpLocalDirectoryDownload()));
    source.setAutoCreateLocalDirectory(true);
    source.setLocalFilter(new AcceptOnceFileListFilter<File>());
    return source;
}

Processing the file after downloaded to local folder

@Bean
@Inject
@ServiceActivator(
        inputChannel = "fromSftpChannel")
public MessageHandler resultFileHandler() {
    return new MessageHandler() {
        @Override
        public void handleMessage(final Message<?> message) throws MessagingException {
            String payload = String.valueOf(message.getPayload());
            if (!StringUtils.isEmpty(payload) && payload.endsWith("gz")) {
                LOGGER.info("toRequest : {}", message.getPayload());
            }
        }
    };
}

Thank you Artem Bilan, I have added below code for moving the file to uat folder after download. Its working as expected now.

private static final SpelExpressionParser PARSER = new SpelExpressionParser();

@Bean(name="fromSftpChannel")
 public MessageChannel fromSftpChannel() {
     return new PublishSubscribeChannel();
 }

 @Bean
@Inject
@ServiceActivator(inputChannel = "fromSftpChannel")
@Order(Ordered.LOWEST_PRECEDENCE)
public MessageHandler moveFile() {
    SftpOutboundGateway sftpOutboundGateway = new  SftpOutboundGateway(sftpSessionFactory(), Command.MV.getCommand(), "'/test/'.concat(" + PARSER.parseExpression("payload.getName()").getExpressionString() + ")");
    sftpOutboundGateway.setRenameExpressionString("'/test/uat/'.concat(" + PARSER.parseExpression("payload.getName()").getExpressionString() + ")");
    sftpOutboundGateway.setRequiresReply(false);
    sftpOutboundGateway.setOutputChannelName("nullChannel");
    sftpOutboundGateway.setOrder(Ordered.LOWEST_PRECEDENCE);
    sftpOutboundGateway.setAsync(true);
    return sftpOutboundGateway;
}    

1条回答
在下西门庆
2楼-- · 2019-07-19 15:47

You need to make that fromSftpChannel as a PublishSubscribeChannel and have a second subscriber with the SftpOutboundGateway. That one you really configure for the Command.MV and that's it! Don't forget to configure setRenameExpression() to specify a remote path for moving!

查看更多
登录 后发表回答