对于相同的输入文件,具有FileReadingMessageSource触发作业的IntegrationFlows两次

时间:2018-12-07 14:16:21

标签: java spring multithreading spring-integration spring-batch

我有一个Spring Integration Batch作业,该作业在文件到达时触发:

@Bean
public IntegrationFlow fileTriggeredIntegrationFlow() {
    return IntegrationFlows.from(fileReadingMessageSource(),
            c -> c.poller(Pollers.fixedRate(filePollerFrequency, filePollerInitialDelay)))
            .transform(toJobLaunchRequest())
            .handle(jobLaunchingGateway)
            .handle("jobCompletionHandler", "afterJob")
            .get();
}

@Bean
public FileReadingMessageSource fileReadingMessageSource() {
    FileReadingMessageSource sourceReader = new FileReadingMessageSource();
    log.info("Listening for {} files at {}", sourceFilePattern, sourceFileDirectory);
    sourceReader.setDirectory(new File(sourceFileDirectory));
    sourceReader.setFilter(new RegexPatternFileListFilter(sourceFilePattern));
    return sourceReader;
}

已经很长时间了,但是最近我遇到了一个问题,即作业针对同一输入文件运行了两次。以下是日志文件的摘要:

  

2018-12-06 13:52:32,595信息[任务调度程序-7 ] osifFileReadingMessageSource:380-创建的消息:[GenericMessage [payload = / local / MY_INPUT_FILE.DAT,headers = {id = ba1ad258-72cf-ed07-1175-3d1cc36f9bc1,timestamp = 1544122352595}]]

     

2018-12-06 13:52:32,599信息[任务调度程序-10 ] osifFileReadingMessageSource:380-创建的消息:[GenericMessage [payload = / local / MY_INPUT_FILE.DAT,headers = {id = ab8e9fbc-72a1-43e0-2ca3-8b8dcb3d91e5,timestamp = 1544122352599}]]

如果看到有两个用粗体标记的线程拾取了相同的文件并触发了相同的作业两次。有人可以指导我如何避免这些情况吗?

1 个答案:

答案 0 :(得分:1)

好像在下一个轮询周期从sourceFileDirectory目录中拾取了同一文件。

您需要考虑在AcceptOnceFileListFilterRegexPatternFileListFilter中将CompositeFileListFilterChainFileListFilter一起使用:https://docs.spring.io/spring-integration/docs/current/reference/html/files.html#file-reading