title: ThreadPoolExecutor : : TaskRejectedException from Executor
body: <p>My application is reading messages through Jms MessageListener class and at some point of time it is throwing <strong>TaskRejectedException</strong>. I know most of you will say that the number of threads is exceeded by <em>maxPoolSize</em> and queue is also full.</p>
  
  <p>But I observed something. The number of messages sent to the queue from which the MessageListener class is fetching messages is 10353 and my spring property for threadPoolExecutor is below : </p>
  
  <pre><code>&lt;bean id="ticketReaderThreadPool" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" scope="singleton" destroy-method="destroy"&gt;
      &lt;property name="corePoolSize" value="10" /&gt;
      &lt;property name="maxPoolSize" value="150" /&gt;
      &lt;property name="queueCapacity" value="11000" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Now according to me, the maxPoolSize is more than enough to handle these many requests. So if anyone of you can give a reason apart from maxPoolSize breach then please do so. </p>
  
  <p>We are facing this issue for the second time now, previously we already tried increasing the maxPoolSize but again after 15 days we are experiencing this exception for around 5000 to 8000 times a day.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>This is the full stack trace of the exception: </p>
  
  <blockquote>
    <p>General Exception occurred while reading from Queue/Processing the message
    org.springframework.core.task.TaskRejectedException: Executor [java.util.concurrent.ThreadPoolExecutor@408b9775] did not accept task: com.batman.rapid.rapidserver.sla.TicketHandler@1be5e598
            at org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor.execute(ThreadPoolTaskExecutor.java:244)
            at com.batman.rapid.rapidserver.sla.JmsTicketReceiver.onMessage(JmsTicketReceiver.java:58)
            at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:560)
            at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:498)
            at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:467)
            at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:325)
            at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:263)
            at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:1058)
            at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:1050)
            at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:947)
            at java.lang.Thread.run(Thread.java:662)
    Caused by: java.util.concurrent.RejectedExecutionException
            at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1774)
            at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:768)
            at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:656)
            at org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor.execute(ThreadPoolTaskExecutor.java:241)
            ... 10 more</p>
  </blockquote>
  
  <p>This is the relevant code: </p>
  
  <pre><code>if (message instanceof TextMessage)
  {
      textMessage = (TextMessage) message;
      ticketReaderThreadPool.execute(new TicketHandler(textMessage.getText()));
  }
  </code></pre>
  
  <p>Below is the configuration requested : </p>
  
  <p>
                  
                  
                  
          </p>
  
  <pre><code>    &lt;!-- End of JMS Queue Support --&gt;
  
      &lt;bean id="ticketReaderThreadPool" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" scope="singleton" destroy-method="destroy"&gt;
      &lt;property name="corePoolSize" value="10" /&gt;
      &lt;property name="maxPoolSize" value="150" /&gt;
      &lt;property name="queueCapacity" value="11000" /&gt;
  &lt;/bean&gt;
  
      &lt;bean id="notificationThreadPool" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" scope="singleton" destroy-method="destroy"&gt;
              &lt;property name="corePoolSize" value="10" /&gt;
              &lt;property name="maxPoolSize" value="100" /&gt;
              &lt;property name="queueCapacity" value="10000" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="notificationManager" class="com.batman.rapid.rapidserver.sla.scheduler.NotificationManager" scope="singleton"&gt;
              &lt;property name="defaultPercent" value="80"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="dbUpdateThreads" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" scope="singleton" destroy-method="destroy"&gt;
              &lt;property name="corePoolSize" value="1" /&gt;
              &lt;property name="maxPoolSize" value="100" /&gt;
              &lt;property name="queueCapacity" value="10000" /&gt;
      &lt;/bean&gt;
  </code></pre>