title: Spring CachingConnectionFactory limiting channels & causing Thread Blocking
body: <p>I have a simple message publisher publishing to rabbitmq (local) at very high rate.</p>
  
  <pre><code>ExecutorService threadPool = Executors.newFixedThreadPool(5, factory);
  
  for (int i = 1; i &lt;= 1000000; i++) {
      int finalI = i;
      threadPool.submit(new Runnable() {
          @Override
          public void run() {
              messagePublisher.publishMessage("testExchange", "testRoutingKey1", message + String.valueOf(finalI));
          }
      });
  }
  </code></pre>
  
  <p>Here messagePublisher is an instance of the following class</p>
  
  <pre><code>public class ChannelCachedMessagePublisher {
  
      private static CachingConnectionFactory CACHING_CONNECTION_FACTORY = new CachingConnectionFactory("localhost");
      private static RabbitTemplate RABBIT_TEMPLATE;
  
      private void init() {
          if (!INITIALIZED) {
              synchronized (ConnectionCachedMessagePublisher.class) {
                  CACHING_CONNECTION_FACTORY.setCacheMode(CachingConnectionFactory.CacheMode.CHANNEL);
                  CACHING_CONNECTION_FACTORY.setChannelCacheSize(100);
                  CACHING_CONNECTION_FACTORY.setVirtualHost("testVHost");
                  CACHING_CONNECTION_FACTORY.setConnectionNameStrategy(CACHING_CONNECTION_FACTORY -&gt; "arpit-cached-connection");
                  CACHING_CONNECTION_FACTORY.setPublisherConfirms(false);
                  RABBIT_TEMPLATE = new RabbitTemplate(CACHING_CONNECTION_FACTORY);
              }
              INITIALIZED = true;
          }
      }
  
      public void publishMessage(String exchange, String routingKey, String message) {
  
          RABBIT_TEMPLATE.convertAndSend(exchange, routingKey, message, new MessagePostProcessor() {
              @Override
              public Message postProcessMessage(Message message) throws AmqpException {
                  MessageProperties properties = message.getMessageProperties();
                  properties.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
                  return new org.springframework.amqp.core.Message(message.getBody(),properties);
              }
          });
      }
  }
  </code></pre>
  
  <p>Upon execution of this code, the 5 threads creates only 5 channels over a single connection.</p>
  
  <p><strong>EDIT 1</strong>
  1). Why is connection factory not providing more than 5 threads here and associating threads per thread?
  This part is clear now as connection factory can not provide more channels (or connections) then the number of running threads itself.</p>
  
  <p>Next, immediately all the threads starts getting blocked over the monitor object in SocketFrameHandler.</p>
  
  <blockquote>
    <p>"arpit-rmq-5" #1019 prio=5 os_prio=31 tid=0x00007fb90c5b8800
    nid=0x83303 waiting for monitor entry [0x0000700047bca000]<br>
    java.lang.Thread.State: BLOCKED (on object monitor)
            at com.rabbitmq.client.impl.SocketFrameHandler.writeFrame(SocketFrameHandler.java:170)</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/PFgFP.png" rel="nofollow noreferrer">Here's a view of the threads post execution exit</a></p>
  
  <p>2). Why exactly are these threads getting blocked and what can be the workarounds to resolve the same?</p>
  
  <p>NOTE: This leads to moving my rabbit connection in flow control state, with which I am fine. My main concern is the thread blocking in JVM.</p>