如何让我的应用程序在Java中等待文件夹中的txt文件?

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

我正在学习Java和Spring Boot,我需要做一个应用程序来处理txt文件。所以我有一个文件夹,在这个文件夹中我收到了txt文件。这个应用程序将在一个文件夹中等待txt文件,并将处理后的txt文件放在另一个文件夹中。一旦在特定的文件夹中接收到一个txt文件,应用程序就会对其进行处理,然后它会等待另一个txt文件的处理。我知道如何做业务逻辑,如何在处理完txt文件后导出它们,但我如何才能完成这个等待过程?任何反馈都将被感激! 谢谢你!我正在学习Java和Spring。

java xml spring-boot jaxb wait
1个回答
0
投票

由于你使用的是Spring Boot,我会使用他们的调度服务来检查新文件的时间表。你必须添加 @EnableScheduling 标签到你的配置中。然后定义一个方法来运行在时间表上。

@Scheduled(fixedDelay = 60000)
public void checkForNewProductFiles() {

更多内容请看这里。https:/spring.ioguidesgsscheduling-tasks


0
投票

查看spring集成,它可以实现你想要实现的目标。

下面是示例代码,它可以监听远程位置的任何新文件,如果有的话就进行处理。

@Bean
public IntegrationFlow sftpInboundFlow() throws IOException {

    if (update.isClearLocalDir) {
        FileUtils.clearLocalDir(update.localPath);
    }

    return IntegrationFlows
            .from(
                    Sftp.inboundAdapter(sftpSessionFactory)
                            .preserveTimestamp(true)
                            .deleteRemoteFiles(true)
                            .regexFilter(update.filterExpr)
                            .localDirectory(new File(update.localPath))
                            .autoCreateLocalDirectory(true)
                            .remoteDirectory(update.remotePath),
                    e -> e.id("sftpInboundAdapter")
                            .autoStartup(true)
                            .poller(Pollers.fixedRate(update.pollingFrequency)
                                    .maxMessagesPerPoll(1)))
            .channel(updateFileChannel())
            .get();
}

@Bean
public DirectChannel updateFileChannel() {
    return new DirectChannel();
}

@Bean
public IntegrationFlow orderUpdateFlow() {
    return IntegrationFlows.from(updateFileChannel())
            .handle(fileProcessor, "handleUpdate")
            .get();
}

请参考 https:/spring.ioguidesgsintegration。

参考 春天的intergartion

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