Spring Integration-FTP应与本地文件夹同步

时间:2016-06-13 12:52:03

标签: spring-integration spring-integration-sftp

我有ftp位置文件并有本地文件夹,第一次将文件复制到本地并重新启动服务器(目前它正在复制已复制的文件到本地文件夹)它不应该查找已经存在的文件存在于本地,它应该只查找新文件。请让我知道是否可以使用Spring-Integration ftp实现它? 我已经添加了过滤器,但仍然无法正常工作,请让我知道我哪里出错了,

@Bean
@InboundChannelAdapter(value = "inputChannel", poller = @Poller(fixedDelay = "1000", maxMessagesPerPoll = "1"))
public MessageSource<?> receive() {
    FtpInboundFileSynchronizingMessageSource messageSource = new FtpInboundFileSynchronizingMessageSource(synchronizer());
    PropertiesPersistingMetadataStore metadataStore = new PropertiesPersistingMetadataStore();
    FileSystemPersistentAcceptOnceFileListFilter acceptOnceFilter = new FileSystemPersistentAcceptOnceFileListFilter(metadataStore,"*.xml");

    File Temp = new File(TEMP_FOLDER);

    metadataStore.setBaseDirectory(TEMP_FOLDER);
    messageSource.setLocalDirectory(Temp);
    messageSource.setAutoCreateLocalDirectory(false);

    messageSource.setLocalFilter(acceptOnceFilter);

    return messageSource;
}

private AbstractInboundFileSynchronizer<FTPFile> synchronizer() {
    folderCleanUp();
    AbstractInboundFileSynchronizer<FTPFile> fileSynchronizer = new FtpInboundFileSynchronizer(sessionFactory());
    fileSynchronizer.setRemoteDirectory(ftpFileLocation);
    fileSynchronizer.setDeleteRemoteFiles(false);
    Pattern pattern = Pattern.compile(".*\\.xml$");
    FtpRegexPatternFileListFilter ftpRegexPatternFileListFilter = new FtpRegexPatternFileListFilter(pattern);
    fileSynchronizer.setFilter(ftpRegexPatternFileListFilter);
    return fileSynchronizer;
}

2 个答案:

答案 0 :(得分:0)

是的,确实如此。请查看local-filter媒体资源,FileSystemPersistentAcceptOnceFileListFilter可让您通过外部MetadataStore跟踪本地文件,例如Redis,MongoDb或任何其他使系统重新启动数据的方法。

答案 1 :(得分:0)

为了澄清有关实施自定义FileListFilter的{​​{3}},以下是此类过滤器的示例(旨在过滤掉比给定时刻更早的文件):

@Component
public class OldFilesFilter extends AbstractFileListFilter<FTPFile> {
  // (oldFilesTimestamp field declaration and its source)

  @Override
  protected boolean accept(FTPFile file) {
    String fileName = file.getName();
    long fileTimestamp = file.getTimestamp().getTimeInMillis();
    ZonedDateTime fileModTimestamp = ZonedDateTime.ofInstant(Instant.ofEpochMilli(fileTimestamp), ZoneId.systemDefault());
    boolean isFileAcceptable = fileModTimestamp.isAfter(oldFilesTimestamp);
    if (log.isTraceEnabled()) {
      log.trace("File {}:\n" +
          "file timestamp : {};\n" +
          "given timestamp: {};\n" +
          "file is new    : {}",
          fileName, fileModTimestamp, oldFilesTimestamp, isFileAcceptable);
    }
    return isFileAcceptable;
  }
}

另请注意,Spring Integration允许同时将多个过滤器应用于单个文件源。这可以通过CompositeFileListFilter

来实现
  private CompositeFileListFilter<FTPFile> remoteFileFilter() {
    FtpPersistentAcceptOnceFileListFilter persistentFilter =
                            new FtpPersistentAcceptOnceFileListFilter(metadataStore, "remoteProcessedFiles.");
    return new CompositeFileListFilter<>(Arrays.asList(new FtpSimplePatternFileListFilter("*.zip"),
                                                       persistentFilter,
                                                       oldFilesFilter /*known from previous example*/));
  }