简体   繁体   中英

Niolocker is not working in spring boot application

Trying to test file lock mechanism in spring boot application by starting two instances of same application pointing to same source path which contains 10 files. Expecting only one instance should process a file and once processed it will be deleted from source. Same file should not be processed by other instance.So added Niolocker to the scanner. Tested in both windows and linux environments.But in windows, facing below exception in both the instances... In Linux, same file is polled/processed by both instances. No impact in Linux. Have implemented the below logic to acquire lock.

Please suggest on this.

Windows exception:

java.io.IOException: The process cannot access the file because another process has locked a portion of the file

Linux: Both instance poller picks the same file and processing it

<file:inbound-channel-adapter id="filesInChannel" directory="file:${base.path}" auto-startup="false" scanner="recursiveScanner" auto-create-directory="true">
  <integration:poller id="poller" max-messages-per-poll="${max.messages.per.poll}" fixed-rate="${message.read.frequency}" task-executor="pollingExecutor">
    <integration:transactional transaction-manager="transactionManager" />
  </integration:poller>
  </file:inbound-channel-adapter>


<bean id="inboundFilter" class="org.springframework.integration.file.filters.CompositeFileListFilter">
     <constructor-arg>
         <list>
            <bean class="org.springframework.integration.file.filters.AcceptOnceFileListFilter"/>
            <bean class="org.springframework.integration.file.filters.RegexPatternFileListFilter">
                <constructor-arg value="${file.type}"/>
           </bean>
         </list>
    </constructor-arg>
</bean>

<bean id="inboundChannelNioLocker" class="org.springframework.integration.file.locking.NioFileLocker" /> 

<bean id="recursiveScanner" class="org.springframework.integration.file.RecursiveDirectoryScanner">
        <property name="filter" ref="inboundFilter" />
        <property name="locker" ref="inboundChannelNioLocker"/>
</bean>

The NioLocker is really operation system dependent and doesn't guarantee exclusive access to the file. Well, only Windows does that for us properly.

I even start considering to deprecate and remove it altogether from the Framework. It causes too much confuses for target users...

Instead of file locker you need to consider to use a FileSystemPersistentAcceptOnceFileListFilter based on the shared ConcurrentMetadataStore . So, this way really only one instance will pick up the file for processing. All others will skip it and move on to the next files.

See Reference Manual for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM