IntegrationFlows with FileReadingMessageSource triggering job twice for same input file

笑着哭i 提交于 2019-12-25 03:26:21

问题


I have a Spring Integration Batch job which is triggerred when the files are arrived:

@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;
}

It has been pretty smooth for long but recently I faced issue where the jobs ran twice for the same input file. Following is a snippet from log files:

2018-12-06 13:52:32,595 INFO [task-scheduler-7] o.s.i.f.FileReadingMessageSource:380 - Created message: [GenericMessage [payload=/local/MY_INPUT_FILE.DAT, headers={id=ba1ad258-72cf-ed07-1175-3d1cc36f9bc1, timestamp=1544122352595}]]

2018-12-06 13:52:32,599 INFO [task-scheduler-10] o.s.i.f.FileReadingMessageSource:380 - Created message: [GenericMessage [payload=/local/MY_INPUT_FILE.DAT, headers={id=ab8e9fbc-72a1-43e0-2ca3-8b8dcb3d91e5, timestamp=1544122352599}]]

If you see there are two threads marked in bold which have picked up the same files and triggered same job twice. Can someone please guide me how to avoid these cases?


回答1:


Looks like the same file is picked up from the sourceFileDirectory directory on the next polling cycle.

You need to consider to use an AcceptOnceFileListFilter alongside with that RegexPatternFileListFilter as a composition in the CompositeFileListFilter or ChainFileListFilter: https://docs.spring.io/spring-integration/docs/current/reference/html/files.html#file-reading



来源:https://stackoverflow.com/questions/53671336/integrationflows-with-filereadingmessagesource-triggering-job-twice-for-same-inp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!