Spring integration; Copying files picked up by service before copy is completed
<p><strong>Application background</strong></p>

<p>There are 2 services in application. Lets call them<br>
1) service A<br>
2) service B</p>

<p><strong>Service A</strong> basically copies files that match a particular criteria from the source directory to the destination directory. Using spring integration here.
The destination directory for service A is the source directory of service B.</p>

<p><strong>Service B</strong> is constantly polling the directory for files and processes them and then moves them to another subdirectory called "processed".</p>

<p><strong>Problem:</strong><br>
The original problem was that while service A is copying files to the destination directory, service B picks up the half copied files and processes them.</p>

<p><strong>Tried solution</strong><br>
Refer to the <strong>service B</strong> <code>integration-context.xml</code> below. I attached a composite filter to the inbound channel. I added a custom filter called <code>LastCreatedFileListFilter</code> to this composite filter. This filter is based on lines of <code>LastModifiedFileListFilter</code> provided by <code>spring-integration-file</code> and this basically discards any file whose age (by created time) is less than 30 seconds.</p>

<p>This filter works perfectly and does not pick a file until its 30 seconds old. But the problem now is that I am using <code>prevent-duplicates="true"</code>. So what happens is the first time <strong>service B</strong> polls the folder and the age of the file is less than 30 seconds it filters out the file but after 30 seconds, the filter does not filter out the file which is correct behavior but by now the service has marked this as a duplicate and rejects it.</p>

<p>So, my problem here is that I do want to keep a prevent duplicate check and also not process a file until its fully copied.</p>

<p>I am not very familiar with spring-integration and the two approaches I am looking to realize is:<br>
1) not mark files as duplicate in the above scenario until it is processed by the application? is this possible? is so, is this advised?<br>
2) In service A if I could first create files with a temporary name and then rename them after the copy is complete? the thing here is that service B will not pick up the files until they start with a configured name. This way, I do not have to trust the age property which could not be 100% decisive as the source and destination are on a different network and copy times could be large (#MurphysLaw). 
But the problem here is that, I am having trouble conceptualizing how to best achieve this solution with spring integration. Any guidance or suggestions?   </p>

<p>Please refer to integration context for service A for current implementation. Let me know if any thing needs clarification. </p>

<p><strong>Service A</strong></p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:int-file="http://www.springframework.org/schema/integration/file"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/integration
    http://www.springframework.org/schema/integration/spring-integration.xsd
    http://www.springframework.org/schema/integration/file
    http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&gt;

    &lt;context:property-placeholder location="classpath:application.properties"/&gt;


    &lt;bean name="redisMetaDataStore" class="org.springframework.integration.redis.metadata.RedisMetadataStore"&gt;
        &lt;constructor-arg ref="redisConnectionFactory" /&gt;
    &lt;/bean&gt;

    &lt;bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"&gt;
        &lt;property name="port" value="6379" /&gt;
    &lt;/bean&gt;

    &lt;int-file:inbound-channel-adapter id = "filesIn"
                                      channel="fileChannel"
                                      directory="file:${input.directory}"
                                      filter="incomingCompositeFilter"&gt;

        &lt;int:poller id="fileInboudPoller" fixed-rate="${in.interval}" time-unit="SECONDS"  /&gt;
    &lt;/int-file:inbound-channel-adapter&gt;

    &lt;bean id="incomingCompositeFilter" class="org.springframework.integration.file.filters.CompositeFileListFilter"&gt;
        &lt;constructor-arg&gt;
            &lt;list&gt;
                &lt;bean id="acceptOnceFilter" class="org.springframework.integration.file.filters.FileSystemPersistentAcceptOnceFileListFilter"&gt;
                    &lt;constructor-arg ref="redisMetaDataStore"/&gt;
                    &lt;constructor-arg value="*"/&gt;
                &lt;/bean&gt;
                &lt;bean id="notOlderThanDateFilter" class="com.fexco.bgeadmin.file.filter.NotOlderThanDateFilter"&gt;
                    &lt;constructor-arg value="${file.lastModified.ignoreBeforeDate}"/&gt;
                &lt;/bean&gt;
                &lt;bean id="documentConfigFilter" class="com.fexco.bgeadmin.file.filter.DocumentConfigFilter"&gt;
                &lt;/bean&gt;
            &lt;/list&gt;
        &lt;/constructor-arg&gt;&lt;/bean&gt;

    &lt;int:channel id="fileChannel"/&gt;

    &lt;int-file:outbound-channel-adapter id="save-as-file"
                                       auto-create-directory="true"
                                       channel="fileChannel"
                                       directory="file:${output.directory}"/&gt;
&lt;/beans&gt;
</code></pre>

<p><strong>Service B</strong></p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:int-file="http://www.springframework.org/schema/integration/file"
       xmlns:batch-int="http://www.springframework.org/schema/batch-integration"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/integration
         http://www.springframework.org/schema/integration/spring-integration.xsd
         http://www.springframework.org/schema/integration/file
         http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
         http://www.springframework.org/schema/batch-integration
         http://www.springframework.org/schema/batch-integration/spring-batch-integration.xsd"&gt;

    &lt;int:channel id="inboundFileChannel"/&gt;
    &lt;int:channel id="outboundJobRequestChannel"/&gt;
    &lt;int:channel id="jobLaunchReplyChannel"/&gt;

    &lt;int-file:inbound-channel-adapter id="filePoller"
                                      channel="inboundFileChannel"
                                      directory="${app.file.source}"
                                      auto-create-directory="true"
                                      prevent-duplicates="true"
                                      filter="incomingCompositeFilter"&gt;
        &lt;int:poller fixed-rate="5000"/&gt;
    &lt;/int-file:inbound-channel-adapter&gt;

    &lt;bean id="incomingCompositeFilter" class="org.springframework.integration.file.filters.CompositeFileListFilter"&gt;
        &lt;constructor-arg&gt;
            &lt;list&gt;
                &lt;bean id="fileNameFilter" class="org.springframework.integration.file.filters.RegexPatternFileListFilter"&gt;
                    &lt;constructor-arg value=".*\.(xls|xlsx|csv)$" /&gt;
                &lt;/bean&gt;
                &lt;bean id="ageFilter" class="com.fexco.bgeadmin.integration.filter.LastCreatedFileListFilter"&gt;
                    &lt;property name="age" value="30"/&gt;
                &lt;/bean&gt;
            &lt;/list&gt;
        &lt;/constructor-arg&gt;&lt;/bean&gt;

    &lt;int:transformer input-channel="inboundFileChannel"
                     output-channel="outboundJobRequestChannel" method="toRequest"&gt;
        &lt;bean class="com.fexco.bgeadmin.integration.FileMessageToJobRequest"/&gt;
    &lt;/int:transformer&gt;

    &lt;batch-int:job-launching-gateway request-channel="outboundJobRequestChannel"
                                     reply-channel="jobLaunchReplyChannel"/&gt;

    &lt;int:logging-channel-adapter channel="jobLaunchReplyChannel"/&gt;

&lt;/beans&gt;
</code></pre>