javaspringspring-integrationniointegration-patterns

Spring Integration File Support


In Spring Integration how to prevent two different machines to work in the same file? I know that nio library(javaSE) provides a mechanism to lock File FileLocker but how can I implement it using Spring Integration? A concrete example would be appreciated.


Solution

  • There is indeed a org.springframework.integration.file.locking.NioFileLocker implementation which can be used for the FileReadingMessageSource option like:

    /**
     * Optional. Sets a {@link FileLocker} to be used to guard files against
     * duplicate processing.
     * <p>
     * <b>The supplied FileLocker must be thread safe</b>
     * @param locker a locker
     */
    public void setLocker(FileLocker locker) {
    

    From the XML perspective it is like this:

    <file:inbound-channel-adapter>
        <file:nio-locker/>
    </file:inbound-channel-adapter>
    

    More info is in the Reference Manual.

    Another option to avoid concurrent access and duplication in differen JVMs is like using FileSystemPersistentAcceptOnceFileListFilter with the shared persistent ConcurrentMetadataStore: https://docs.spring.io/spring-integration/docs/5.0.3.RELEASE/reference/html/files.html#file-reading