title: Spring JMSListener Merging the messages
body: <p>Implemented Spring JMS with Listener  with concurrency  '5' threads ,</p>
  
  <pre><code>&lt;jms:listener-container container-type="default" concurrency="5-10" connection-factory="cachingConnectionFactory"   &gt;
      &lt;jms:listener destination="TEST.FOO" ref="messageListener" /&gt;
   &lt;/jms:listener-container&gt;
  </code></pre>
  
  <p>When I drop 5 messages  , 5 threads are listening and I can able to read the messages .</p>
  
  <p>My question is how to merge all the 5 messages ,Is it possible to write some builder where builder can wait for sometime ,so when any message received within that time ,so I can merge all the messages?</p>
  
  <p>Code:</p>
  
  <pre><code>    long startTime = 0;
                  if (messageCount == 0) {
                      startTime = System.currentTimeMillis();
                  }    
  messageCount ++;
              if (messageCount &lt; batchSize &amp;&amp; (startTime &gt; (System.currentTimeMillis() - maximumBatchWaitTime))) { // Or if some batch timelimit say
  
                          line += stringMessage;
                          reached = true; // this is volatile variable ,messageCount also volatile variable
                      }
  
                      System.out.println(line);
  
                      try {
                          if (reached) {
                              messageCount = 0;
                              line = "";
                              execService.createExecFile(line);
  
                          }
                      } catch (final Exception e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
  </code></pre>
  
  <p>Regards,
  Rj</p>