1

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.

Eddy Bayonne
  • 2,448
  • 1
  • 17
  • 23

1 Answers1

4

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

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • I want to move it to another folder and I can do it. but the problem is that the FileLocker locks the file before I can do anything. I mean I want to lock the file, move it to another folder and read. If I have 2 diff process the one who picks the file first should lock it and move to another folder. Help please – Eddy Bayonne Mar 18 '18 at 16:23