springspring-integrationspring5

Spring 5: can ChainFileListFilter process files in specified sorted order?


Versions:

Spring: 5.2.16.RELEASE
Spring Integrations: 5.3.9.RELEASE
Ubuntu: focal [20.04.3 LTS]
Java (build and runtime): 11

I am using ChainFileListFilter as follows:

<int-file:inbound-channel-adapter id="channelIn" directory="${channel.dir}" auto-create-directory="false" use-watch-service="false" filter="channelFilter" watch-events="CREATE,MODIFY" auto-startup="${channel.autoStart}">
  <int:poller trigger="channelTrigger" max-messages-per-poll="${channel.polling.maxmsgs}"></int:poller>
</int-file:inbound-channel-adapter>

<bean id="channelTrigger" class="org.springframework.scheduling.support.PeriodicTrigger">
  <constructor-arg index="0" name="period" value="${channel.polling.delay}" />
  <property name="fixedRate" value="false" />
  <!-- Delay inception of polling by one period. -->
  <property name="initialDelay" value="${channel.polling.delay}" />
</bean>

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

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

There must be some way to impose an ordering scheme on the sequence of files admitted through the ChainFileListFilter. The documentation I referenced, however, does not mention any such sorting scheme.

My ultimate objective is that the files admitted through ChainFileListFilter be processed in descending order of the last-modification timestamp of the files. Can this be done with the ChainFileListFilter, or perhaps with its parent, the CompositeFileListFilter?


Solution

  • The ordering is not a filter responsibility. See comparator option if the FileReadingMessageSource: https://docs.spring.io/spring-integration/docs/current/reference/html/file.html#directory-scanning-and-polling. The xml option for you is this:

    <xsd:attribute name="comparator" type="xsd:string">
    
                <xsd:annotation>
    
                    <xsd:documentation><![CDATA[
    
    Specify a Comparator to be used when ordering Files. If none is provided, the
    
    order will be determined by the java.io.File implementation of Comparable.  MUTUALLY EXCLUSIVE with queue-size.
    
                    ]]></xsd:documentation>
    
                </xsd:annotation>
    
            </xsd:attribute>