[英]Niolocker is not working in spring boot application
通过启动指向指向包含10个文件的相同源路径的同一应用程序的两个实例,尝试在Spring Boot应用程序中测试文件锁定机制。 预期只有一个实例应该处理文件,并且一旦处理,它将从源中删除。 同一文件不应被其他实例处理,因此将Niolocker添加到了扫描仪中。 在Windows和Linux环境中都经过测试。但是在Windows中,两个实例都面临以下异常...在Linux中,两个实例都轮询/处理相同的文件。 在Linux中没有影响。 实现了以下逻辑获取锁。
请对此提出建议。
Windows例外:
java.io.IOException:该进程无法访问文件,因为另一个进程已锁定文件的一部分
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>
NioLocker
是取决于操作系统的,并不保证对该文件的独占访问。 好吧,只有Windows才能为我们正确地做到这一点。
我什至开始考虑弃用它并将其从框架中完全删除。 它给目标用户造成太多混乱。
您需要考虑使用基于共享的ConcurrentMetadataStore
的FileSystemPersistentAcceptOnceFileListFilter
而不是文件柜。 因此,这样一来,实际上只有一个实例可以提取文件进行处理。 所有其他人都将跳过它,然后继续下一个文件。
有关更多信息,请参见参考手册 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.