【问题标题】:FileReadingMessageSource.WatchServiceDirectoryScanner: turn off recursive descent into sub-directories?FileReadingMessageSource.WatchServiceDirectoryScanner:关闭递归下降到子目录?
【发布时间】:2021-11-02 23:13:56
【问题描述】:

版本:

Spring: 5.2.16.RELEASE
Spring Integrations: 5.3.9.RELEASE
macOS Big Sur: 11.6

我正在使用 XML 来设置目录扫描器FileReadingMessageSource.WatchServiceDirectoryScanner,如下所示:

<int-file:inbound-channel-adapter id="channelIn" directory="${channel.dir}" auto-create-directory="false" use-watch-service="true" filter="channelFilter" watch-events="CREATE,MODIFY">
  <int-file:nio-locker ref="channelLocker"/>
  <int:poller fixed-delay="${channel.polling.delay}" max-messages-per-poll="${channel.polling.maxmsgs}"></int:poller>
</int-file:inbound-channel-adapter>

具有以下 bean 定义:

<bean id="channelLocker" class="org.springframework.integration.file.locking.NioFileLocker"/>
<bean id="channelFilter" class="org.springframework.integration.file.filters.ChainFileListFilter">
  <constructor-arg>
    <list>
      <bean class="org.springframework.integration.file.filters.SimplePatternFileListFilter">
        <constructor-arg value="SpreadSheets*.xls" />
      </bean>
      <bean id="filter" class="org.springframework.integration.file.filters.LastModifiedFileListFilter">
        <property name="age" value="${channel.filter.age}" />
      </bean>
      <ref bean="persistentFilter" />
    </list>
  </constructor-arg>
</bean>

<bean id="persistentFilter" class="org.springframework.integration.file.filters.FileSystemPersistentAcceptOnceFileListFilter">
  <constructor-arg index="0" ref="metadataStore" />
  <constructor-arg index="1" name="prefix" value="" />
  <property name="flushOnUpdate" value="false" />
</bean>

如果我查看org.springframework.integration.file.FileReadingMessageSource 的日志,我注意到我们同时注册了指定目录(即${channel.dir})及其任何子目录。也就是说,我看到这样的日志:

15:44:45.706 [main] DEBUG org.springframework.integration.file.FileReadingMessageSource - registering: /Users/kc/scan.here for file events
15:44:45.711 [main] DEBUG org.springframework.integration.file.FileReadingMessageSource - registering: /Users/kc/scan.here/and.here for file events

我查看了 Spring 文档以及相关软件模块的 API 文档(例如,FileReadingMessageSource),但我没有看到任何用于关闭递归下降到子目录的属性或配置选项。

这里推荐的做法是只扫描指定目录中的文件,但不进行更深的递归?

【问题讨论】:

    标签: spring spring-integration


    【解决方案1】:

    如果您不递归并扫描整个文件树,请不要使用该监视服务!

    对于创建和修改事件,您可以配置一个FileSystemPersistentAcceptOnceFileListFilter 来检查file.lastModified()。我看你还是那样做,所以不清楚你为什么需要手表服务?

    请参阅此处讨论的一些相关内容:https://github.com/spring-projects/spring-integration/issues/3557

    如果您仍然有一些合理的理由认为仅对根目录使用监视服务,请在该问题中添加评论,我们将分别对其进行修改。

    【讨论】:

    • 你是说我可以设置use-watch-service=false,但仍然可以在指定目录中看到与新文件和/或修改相对应的消息事件?我同意这应该足以满足我的需要;我会试试的。不过,我确实认为默认情况下对 watch-service 进行批发递归似乎有点过头了。
    • 正如我所说:在该问题中添加这样的评论 - 我们将在未来对其进行修改。
    猜你喜欢
    • 2012-12-24
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    相关资源
    最近更新 更多