[英]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.