如何使用Spring Integration中的File Watcher仅用于等待,而不用于移动?

问题描述 投票:1回答:1

我正在Spring Boot应用程序中学习有关Spring Integration的知识,我需要制作一个应用程序以等待文件夹中的.txt文件,然后进行一些处理,然后将生成的.txt文件放入另一个文件夹中。

因此,应用程序应等待输入文件夹中的inputxxx.txt文件,然后进行一些处理,然后将生成的outputxxx.txt文件放入输出文件夹。 inputxxx.txt文件应保留在输入文件夹中,而输出文件夹中应仅是outputxxx.txt文件。

@Configuration
@EnableIntegration
public class ApplicationConfig {

    @Bean
    public MessageChannel fileChannel() {
        return new DirectChannel();
    }

    @Bean
    @InboundChannelAdapter(value = "fileChannel", poller = @Poller(fixedDelay = "1000"))
    public MessageSource<File> fileReadingMessageSource() {

        SimplePatternFileListFilter filter = new SimplePatternFileListFilter(FILE_PATTERN);
        return new FilePoller(directory, filter);
    }

    @Bean
    @ServiceActivator(inputChannel = "fileChannel")
    public MessageHandler fileWritingMessageHandler() {
        return new FileHandler(OUTPUT_LOCATION);
    }
}

FilePoller类:

public class FilePoller extends FileReadingMessageSource {

    public FilePoller(File directory, FileListFilter<File> filter) {
        super.setDirectory(directory);
        super.setFilter(filter);
    }
}

所以我不想使用move()方法,因为JAXB会做我想要的事情,但是如果我删除move(),我的应用程序将不会停止。

谢谢!任何反馈将不胜感激!

java spring-boot jaxb spring-integration file-watcher
1个回答
2
投票

您的问题在这里:

    SimplePatternFileListFilter filter = new SimplePatternFileListFilter(FILE_PATTERN);
    return new FilePoller(directory, filter);

您只检查特定的文件类型,但不保护您的程序在下一个轮询周期拾取相同的文件。

[请考虑使用ChainFileListFilter,其中该模式第一个为拳头,但第二个模式必须为AcceptOnceFileListFilter

这样,您完全可以忽略原始文件,而只专注于输出文件。在下一个轮询周期中不会再次选择原始文件。

对于程序重新启动之间的永久性无知,您应该考虑使用FileSystemPersistentAcceptOnceFileListFilter

[在文档中查看更多内容:

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

© www.soinside.com 2019 - 2024. All rights reserved.