我正在编写一个文件轮询实现,并尝试确定是否需要使用 AcceptOnceFileListFilter。 FileProcessor 将执行的第一步是将文件移动到另一个目录。
轮询器“batchFilePoller”在轮询时是否使用多个线程?当多个线程读取文件时是否会出现竞争条件?在本例中,我假设我需要使用 AcceptOnceFileListFilter。
但是,如果轮询器仅使用池中的一个线程。 然后,如果文件在下一次轮询时间之前移动并且成功,我认为该文件不可能被处理两次?
<int-file:inbound-channel-adapter id="batchFileInAdapter" directory="/somefolder" auto-create-directory="true" auto-startup="false" channel="batchFileInChannel" >
<int:poller id="batchFilePoller" fixed-rate="6000" task-executor="batchTaskExecutor" max-messages-per-poll="1" error-channel="batchPollingErrorChannel" />
</int-file:inbound-channel-adapter>
<int:channel id="batchFileInChannel"/>
<int:service-activator input-channel="batchFileInChannel" >
<bean class="com.foo.FileProcessor" />
</int:service-activator>
<task:executor id="batchTaskExecutor" pool-size="5" queue-capacity="20"/>
最佳答案
<int-file:inbound-channel-adapter>
有prevent-duplicates
选项是 true
默认情况下,这是您的情况,因为您没有提供任何其他选项来阻止 prevent-duplicates
成为true
.
是的:任何轮询适配器都是多线程的,如果您使用 fixed-rate
。在这种情况下,新的轮询任务可以在前一个轮询任务完成之前运行。
即使它是单线程的(使用 fixed-delay
), AcceptOnceFileListFilter
必须在那里,因为新的轮询任务不知道文件是否已被处理。然后它再次读取相同的文件。
AcceptOnceFileListFilter
正是针对那些您不喜欢再次读取同一文件的情况。您可以使用 <int:transactional synchronization-factory=""/>
来克服这个问题对于<poller>
<int-file:inbound-channel-adapter>
的:
<int:transaction-synchronization-factory id="txSyncFactory">
<int:after-commit expression="payload.delete()"/>
</int:transaction-synchronization-factory>
和PseudoTransactionManager
.
您可以在 Spring Integration 引用手册中找到更多信息。
关于multithreading - Spring 集成文件轮询。如果移动文件是否需要使用 AcceptOnceFileListFilter ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28320154/