首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring :无法重命名.writing文件

Spring :无法重命名.writing文件
EN

Stack Overflow用户
提问于 2020-08-03 12:30:44
回答 1查看 418关注 0票数 1

我正在使用Spring集成来传输文件,而且很多时候我都遇到了这个错误。看起来两个线程试图传输同一个文件,并且相互冲突。

2020-08-03 08:31:55,766 INF任务-调度程序-8 o.s.i.ftp.session.FtpSession -文件已从./abc.ext.200803成功传输

2020-08-03 08:31:55,849 INF任务-调度程序-7 o.s.i.ftp.session.FtpSession -文件已从./abc.ext.200803成功传输

2020-08-03 08:31:55,850 INF任务-调度程序-7 .s.i.f.i.FtpInboundFileSynchronizer -不能重命名'/ local /download/abc.ext.200803‘在删除后写入本地文件’/local/download/abc.ext.200803‘。本地文件可能在其他进程中很忙.

有没有办法让两个线程不应该互相干扰?

我用的是以下代码-

代码语言:javascript
复制
@Bean
public SftpInboundFileSynchronizer ftpInboundFileSynchronizer() {
    isFTPSessionOK();
    SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());

    fileSynchronizer.setPreserveTimestamp(true);
    fileSynchronizer.setRemoteDirectory(remoteDirectory);
    fileSynchronizer.setDeleteRemoteFiles(false);

    fileSynchronizer.setFilter(new SFTPLastModifiedFileFilter(remoteFileFilter));
    return fileSynchronizer;
}

private boolean isFTPSessionOK() {
    try {
        SessionFactory<LsEntry> ftpSessionFactory = sftpSessionFactory();
        boolean open = ftpSessionFactory.getSession().isOpen();
        LOG.info("FTPSession is good ? " + open);
        return open;
    } catch (Exception e) {
        LOG.error("FTPSession is not good because of error : " + e);
    }
    return false;
}

@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
    DefaultSftpSessionFactory sf = new DefaultSftpSessionFactory();
    sf.setHost(server);
    sf.setPort(port);
    sf.setUser(username);
    sf.setPassword(password);
    sf.setAllowUnknownKeys(true);
    return new CachingSessionFactory<LsEntry>(sf);
}

@Bean
@InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(fixedDelay = "${${project.name}.ftp.poller.delay:600000}", maxMessagesPerPoll = "1"))
public MessageSource<File> ftpMessageSource() {
    SftpInboundFileSynchronizingMessageSource source = new SftpInboundFileSynchronizingMessageSource(ftpInboundFileSynchronizer());
    source.setLocalDirectory(new File(localFtpDirectory));
    source.setAutoCreateLocalDirectory(true);       
    return source;
}

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

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            LOG.info("File '{}' is ready for reading after SFTP", message.getPayload());
        }
    };
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-03 12:53:03

您只有这一项用于过滤:

代码语言:javascript
复制
fileSynchronizer.setFilter(new SFTPLastModifiedFileFilter(remoteFileFilter));

但是,在接下来的投票中,防止重复的过滤器又该怎么办呢?

AcceptOnceFileListFilter。和那个SFTPLastModifiedFileFilter一起使用一个ChainFileListFilter

有关更多信息,请参见文档:

https://docs.spring.io/spring-integration/docs/current/reference/html/sftp.html#sftp-inbound

https://docs.spring.io/spring-integration/docs/current/reference/html/file.html#file-reading

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63229482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档