简体   繁体   English

Niolocker在Spring Boot应用程序中不起作用

[英]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. 通过启动指向指向包含10个文件的相同源路径的同一应用程序的两个实例,尝试在Spring Boot应用程序中测试文件锁定机制。 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. 同一文件不应被其他实例处理,因此将Niolocker添加到了扫描仪中。 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. 在Windows和Linux环境中都经过测试。但是在Windows中,两个实例都面临以下异常...在Linux中,两个实例都轮询/处理相同的文件。 No impact in Linux. 在Linux中没有影响。 Have implemented the below logic to acquire lock. 实现了以下逻辑获取锁。

Please suggest on this. 请对此提出建议。

Windows exception: Windows例外:

java.io.IOException: The process cannot access the file because another process has locked a portion of the file java.io.IOException:该进程无法访问文件,因为另一个进程已锁定文件的一部分

Linux: Both instance poller picks the same file and processing it Linux:两个实例轮询器都选择相同的文件并进行处理

<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. NioLocker是取决于操作系统的,并不保证对该文件的独占访问。 Well, only Windows does that for us properly. 好吧,只有Windows才能为我们正确地做到这一点。

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 . 您需要考虑使用基于共享的ConcurrentMetadataStoreFileSystemPersistentAcceptOnceFileListFilter而不是文件柜。 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. 有关更多信息,请参见参考手册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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