title: Determine the end of a cyclic workflow in spring integration (inbound-channel => service-activator)
body: <p>We have the following simple int-jpa based workflow:</p>
  
  <p>[inbound-channel-adapter] -> [service-activator]</p>
  
  <p>The config is like this:</p>
  
  <pre><code>&lt;int:channel id="inChannel"&gt;  &lt;int:queue/&gt; &lt;/int:channel&gt;
  &lt;int:channel id="outChannel"&gt; &lt;int:queue/&gt; &lt;/int:channel&gt;
  &lt;int-jpa:inbound-channel-adapter id="inChannelAdapter"  channel="inChannel"
       jpa-query="SOME_COMPLEX_POLLING_QUERY"
       max-results="2"&gt;
          &lt;int:poller max-messages-per-poll="2" fixed-rate="20" &gt;
          &lt;int:advice-chain synchronization-factory="txSyncFactory"   &gt;
              &lt;tx:advice transaction-manager="transactionManager" &gt;
                  &lt;tx:attributes&gt;
                      &lt;tx:method name="*" timeout="30000" /&gt;
                  &lt;/tx:attributes&gt;
              &lt;/tx:advice&gt;
              &lt;int:ref bean="pollerAdvice"/&gt;
          &lt;/int:advice-chain&gt;
      &lt;/int-jpa:inbound-channel-adapter&gt;
  
  &lt;int:service-activator input-channel="inChannel" ref="myActivator"
   method="pollEntry" output-channel="outChannel" /&gt;
  
  &lt;bean id="myActivator" class="com.company.myActivator" /&gt;    
  &lt;bean id="pollerAdvice" class="com.company.myPollerAdvice" /&gt;
  </code></pre>
  
  <p>The entry point for processing is a constantly growing table against which the <code>SOME_COMPLEX_POLLING_QUERY</code> is run. The current flow is :</p>
  
  <ol>
  <li><em>[Thread-1]</em> The <code>SOME_COMPLEX_POLLING_QUERY</code> will only return entries that has <code>busy</code> set to <code>false</code> (we set <code>busy</code> to <code>true</code> as soon as polling is done using <code>txSyncFactory</code>)</li>
  <li><em>[Thread-2]</em> These entries will pass through the <code>myActivator</code> where it might take anywhere from 1 min to 30 mins.</li>
  <li><em>[Thread-2]</em> Once the processing is done, we set back the <code>busy</code> from <code>true</code> to <code>false</code></li>
  </ol>
  
  <p><strong>Problem</strong>: We need to trigger a notification even when the processing of all the entries that were present in the table is done. </p>
  
  <p><strong>Approach tried</strong>: We used the <code>afterReturning</code> of <code>pollerAdvice</code> to find out if the <code>SOME_COMPLEX_POLLING_QUERY</code> returned any results or not. However this method will start returning "No Entries" way before the <code>Thread-2</code> is done processing all the entries.</p>
  
  <p>Note: </p>
  
  <ul>
  <li>The same entries will be processes again after 24hrs. But this time it will have more entries.</li>
  <li>We are not using <code>outbound-channel-adapter</code>, since we dont have any requirement for it. However, we are open to use it, if that is a part of the solution proposed.</li>
  </ul>