我正在使用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‘。本地文件可能在其他进程中很忙.
有没有办法让两个线程不应该互相干扰?
我用的是以下代码-
@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());
}
};
}发布于 2020-08-03 12:53:03
您只有这一项用于过滤:
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
https://stackoverflow.com/questions/63229482
复制相似问题