title: Spring integration - Appropriate pattern for collating/batching service calls
body: <p>I have a remote service that I'm calling to load pricing data for a product, when a specific event occurs.  Once loaded, the product pricing is then broadcast for another consumer to process elsewhere.</p>
  
  <p>Rather than call the remote service on every event, I'd like to batch the events into small groups, and send them in one go.</p>
  
  <p>I've cobbled together the following pattern based on an Aggregator.  Although it works,  lots of it 'smells' -- especially my <code>SimpleCollatingAggregator</code>.   I'm new to Spring Integration, and EIP in general, and suspect I'm misusing components.</p>
  
  <h2>The Code</h2>
  
  <p>My code is triggered elsewhere in code by calling a method on the below <code>@Gateway</code>:</p>
  
  <pre><code>public interface ProductPricingGateway {    
      @Gateway(requestChannel="product.pricing.outbound.requests")
      public void broadcastPricing(ProductIdentifer productIdentifier);
  }
  </code></pre>
  
  <p>This is then wired to an aggregator, as follows:</p>
  
  <pre><code>&lt;int:channel id="product.pricing.outbound.requests" /&gt;
  &lt;int:channel id="product.pricing.outbound.requests.batch" /&gt;
  &lt;int:aggregator input-channel="product.pricing.outbound.requests"
  output-channel="product.pricing.outbound.requests.batch" release-strategy="releaseStrategy"
      ref="collatingAggregator" method="collate"
      correlation-strategy-expression="0"
      expire-groups-upon-completion="true" 
      send-partial-result-on-expiry="true"/&gt;  
  &lt;bean id="collatingAggregator" class="com.mangofactory.pricing.SimpleCollatingAggregator" /&gt;
  &lt;bean id="releaseStrategy" class="org.springframework.integration.aggregator.TimeoutCountSequenceSizeReleaseStrategy"&gt;
      &lt;!-- Release when: 10 Messages ... or ... --&gt;
      &lt;constructor-arg index="0" value="10" /&gt;
      &lt;!-- ... 5 seconds since first request --&gt;
      &lt;constructor-arg index="1" value="5000" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Here's the aggregator implementation:</p>
  
  <pre><code>public class SimpleCollatingAggregator {
  
      public List&lt;?&gt; collate(List&lt;?&gt; input)
      {
          return input;
      }
  </code></pre>
  
  <p>}</p>
  
  <p>Finally, this gets consumed on the following <code>@ServiceActivator</code>:</p>
  
  <pre><code>@ServiceActivator(inputChannel="product.pricing.outbound.requests.batch")
  public void fetchPricing(List&lt;ProductIdentifer&gt; identifiers)
  {
          // omitted
  }
  </code></pre>
  
  <p>Note: In practice, I'm also using @Async, to keep the calling code as quick-to-return as possible.  I have a bunch of questions about that too, which I'll move to a seperate question.</p>
  
  <p><strong>Question 1:</strong>
  Given what I'm trying to acheive, is an aggregator pattern an appropriate choice here?  This feels like a <strong><em>lot</em></strong> of boilerplate -- is there a better way?</p>
  
  <p><strong>Question 2:</strong>
  I'm using a fixed collation value of <code>0</code>, to effectively say : 'It doesn't matter how you group these messages, take 'em as they come.'</p>
  
  <p>Is this an appropriate way of achieving this?</p>
  
  <p><strong>Question 3:</strong>
  <code>SimpleCollatingAggregator</code> simply looks wrong to me.  </p>
  
  <p>I want this to receive my individual inbound <code>ProductIdentifier</code> objects, and group them into batches, and then pass them along.  This works, but is it appropriate?  Are there better ways of acheiving the same thing?</p>