title: Keep messages in ActiveMQ queue if ThreadPoolTaskExecutor has no free capacity
body: <p>I have two Java processes, the first one of them produces messages and puts
  them onto an ActiveMQ queue. The second process (consumer) uses Spring
  Integration to get messages from the queue and processes them in threads.</p>
  
  <p>I have two requirements:</p>
  
  <ol>
  <li><p>The consumer should have 3 processing threads. If I have 10 messages
  coming in through the queue, I want to have 3 threads processing the first 3
  messages, and the other 7 messages should be buffered.</p></li>
  <li><p>When the consumer stops while some messages are not yet processed, it
  should continue processing the messages after a restart.</p></li>
  </ol>
  
  <p>Here's my config:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;bean id="messageActiveMqQueue" class="org.apache.activemq.command.ActiveMQQueue"&gt;
      &lt;constructor-arg value="example.queue" /&gt;
  &lt;/bean&gt;
  
  &lt;int-jms:message-driven-channel-adapter
      destination="messageActiveMqQueue" channel="incomingMessageChannel" /&gt;
  
  &lt;int:channel id="incomingMessageChannel"&gt;
      &lt;int:dispatcher task-executor="incomingMessageChannelExecutor" /&gt;
  &lt;/int:channel&gt;
  
  &lt;bean id="incomingMessageChannelExecutor"
      class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"&gt;
      &lt;property name="daemon" value="false" /&gt;
      &lt;property name="maxPoolSize" value="3" /&gt;
  &lt;/bean&gt;
  
  &lt;int:service-activator input-channel="incomingMessageChannel"
      ref="myMessageProcessor" method="processMessage" /&gt;
  </code></pre>
  
  <p>The first requirement works as expected. I produce 10 messages and 3
  myMessageProcessors start processing a message each. As soon as the 1st message has
  finished, the 4th message is processed.</p>
  
  <p>However, when I kill the consumer before all messages are processed, those
  messages are lost. After a restart, the consumer does not get those messages
  again.</p>
  
  <p>I think in the above configuration that's because the threads generated by the
  ThreadPoolTaskExecutor queue the messages. So the messages are already removed
  from the incomingMessageChannel. Hence I tried setting the queue capacity of
  the incomingMessageChannelExecutor:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;property name="queueCapacity" value="0" /&gt;
  </code></pre>
  
  <p>But now I get error messages when I have more than 3 messages:</p>
  
  <pre><code>2013-06-12 11:47:52,670 WARN [org.springframework.jms.listener.DefaultMessageListenerContainer] - Execution of JMS message listener failed, and no ErrorHandler has been set.
  org.springframework.integration.MessageDeliveryException: failed to send Message to channel 'incomingMessageChannel'
  </code></pre>
  
  <p>I also tried changing the <code>message-driven-channel-adapter</code> to an <code>inbound-gateway</code>,
  but this gives me the same error. </p>
  
  <p>Do I have to set an error handler in the <code>inbound-gateway</code>, so that the errors go back to the ActiveMQ queue? How do I have to configure the queue so that the messages are kept in the queue if the ThreadPoolTaskExecutor doesn't have a free thread?</p>
  
  <p>Thanks in advance,</p>
  
  <p>Benedikt</p>