【问题标题】:Spring integration - SFTP rename or move file in remote server after copyingSpring集成-复制后SFTP重命名或移动远程服务器中的文件
【发布时间】:2018-10-03 16:09:43
【问题描述】:

我正在尝试移动或重命名远程文件,而不是在下载后删除远程文件,我发现可以通过出站网关移动命令完成,但找不到正确的方法。

下载后请帮忙重命名文件。

@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;
}

在 SFTP 服务器中查找文件的入站通道

@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;
}

下载到本地文件夹后处理文件

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

感谢 Artem Bilan,我添加了以下代码,用于在下载后将文件移动到 uat 文件夹。它现在按预期工作。

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

【问题讨论】:

    标签: spring spring-integration spring-integration-sftp


    【解决方案1】:

    您需要将fromSftpChannel 设为PublishSubscribeChannel,并拥有SftpOutboundGateway 的第二个订阅者。您真正为Command.MV 配置的那个就是这样!不要忘记配置setRenameExpression()指定移动的远程路径!

    【讨论】:

    • 谢谢,我认为这是改变频道发布订阅频道的方式,但不知道如何设置重命名表达式。@Bean(name="fromSftpChannel") public MessageChannel fromSftpChannel() {返回新的 PublishSubscribeChannel(); }
    • 代码在 cmets 中不可读。请考虑使用更多信息编辑您的问题。也将分享执行结果,但不要等待我这样的问题。
    • 我已经完成了移动文件的建议更改,但没有抛出 output-channel 或 replyChannel 标头可用异常,我添加了有问题的代码更改。你能帮忙解决这个问题吗?
    • 由于SftpOutboundGateway确实是一个request-reply组件并且没有send-only模式,所以需要添加sftpOutboundGateway.setOutputChannelName("nullChannel");之后停止处理。
    猜你喜欢
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    • 2022-08-08
    • 2016-05-22
    • 2023-03-19
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    相关资源
    最近更新 更多