RabbitMQ消费者线程池随着每个请求而增加

时间:2017-01-24 09:40:11

标签: java multithreading rabbitmq

我使用RabbitMQ 3.6.6(AMQP Client 4.0.2)开发了Java 7应用程序。我的消费者类扩展了DefaultConsumer。我通过RabbitMQ管理控制台发送的每个请求都根据日志启动一个新线程( pool-2-thread-n ):

info: pool-2-thread-10 - NumberConsumer - Number request
info: pool-2-thread-11 - NumberConsumer - Number request
info: pool-2-thread-12 - NumberConsumer - Number request
info: pool-2-thread-13 - NumberConsumer - Number request
info: pool-2-thread-14 - NumberConsumer - Number request
info: pool-2-thread-15 - NumberConsumer - Number request
info: pool-2-thread-16 - NumberConsumer - Number request
info: pool-2-thread-17 - NumberConsumer - Number request
info: pool-2-thread-18 - NumberConsumer - Number request
info: pool-2-thread-19 - NumberConsumer - Number request

我的消费者类:

public class NumberConsumer extends DefaultConsumer
{
   private static final Logger LOG = Logger.getLogger( NumberConsumer.class );

   public NumberConsumer(final Channel channel)
   {
      super( channel );
   }


   @Override
   public void handleDelivery(final String consumerTag, final Envelope envelope,
         final AMQP.BasicProperties properties, final byte[] body)
      throws IOException
   {
      final String request = new String( body, "UTF-8" );
      NumberConsumer.LOG.info( "Number request" );

      // ... do work and publish response ...
   }
}

预计会出现这种情况吗? 我很确定每个线程在完成处理请求时都会关闭,但为什么它们的数量不受限制?这可以配置吗?

1 个答案:

答案 0 :(得分:0)

AMQP Java客户端为ConnectionFactory中的消费者提供了自己的线程池。如果不使用此功能,则必须提供自己的ExecutorService作为newConnection(..)方法的参数。