rabbitmq 消费者拿到消息不干活问题

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zhong1129116952/article/details/80662904

第一次写博客,拿碰到的坑,来写写,看看有没有人访问我的博客。

背景: 一个队列对应20个消费者。有很多个队列,channel 数量200左右, 一个消费者对应一个channel.

问题:生产者往一个队列同时写入了10个消息。 理论来说,这个队列肯定是有10消费者拿到消息,去干活了。

但发现只有8个左右消费者立马去干活了,2个消费者拿到消息不立马干活,要等一些时间才去。




方法: rabbitmq 自带的共享线程池默认是50个线程。对于很大的业务需求, 50个是明显不够用的,得增大。

ExecutorService service = Executors.newFixedThreadPool(Integer.parseInt(threadNum));
factory.setSharedExecutor(service);

参考文章:

https://blog.csdn.net/weinianjie1/article/details/50611379

展开阅读全文

RabbitMQ生产者和消费者启动顺序问题

11-16

我有两个消费者,先将两个消费者启动,再启动生产者发送消息,两个消费者都能收到消息。如果先启动生产者发送100条消息,再一前一后启动两个消费者,为什么只有先启动的消费者能消费呢(消息未消费完)?rn生产者代码如下:rnpublic class SenderByFanout rn private static final String EXCHANGE_NAME = "TestFanoutExchange";rn private final static String QUEUE_NAME = "testExchangeTpye";rn private final static String ROUTING_KEY = "routingKey";rn public static void main(String[] argv) throws java.io.IOException,TimeoutException rn Connection con = RabbitMQUtils.getConnection(RabbitMQUtils.getFactory());rn Channel channel = RabbitMQUtils.getChannel(con);rn channel.exchangeDeclare(EXCHANGE_NAME, "fanout");rn channel.queueDeclare(QUEUE_NAME, false, false, false, null);rn channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");rn String message = "注意了:Fanout广播一条消息,注意火灾!";rn for(int i = 0 ; i < 100 ; i++)rn channel.basicPublish(EXCHANGE_NAME, "", null, (message+String.valueOf(i)).getBytes());rn System.out.println(" send message [" + message + "]" + i);rn rn channel.close();rn con.close();rn rnrnrnrn消费者代码如下:两个消费者代码相同。rnpublic class RecvByFanout01 rn private static final String EXCHANGE_NAME = "TestFanoutExchange";rn private final static String QUEUE_NAME = "testExchangeTpye";rn private final static String ROUTING_KEY = "routingKey";rn public static void main(String[] argv) throws IOException, TimeoutException, ShutdownSignalException, ConsumerCancelledException, InterruptedException rn ConnectionFactory factory = RabbitMQUtils.getFactory();rn Connection connection = RabbitMQUtils.getConnection(factory);rn Channel channel = connection.createChannel();rn channel.exchangeDeclare(EXCHANGE_NAME, "fanout");rn channel.queueDeclare(QUEUE_NAME, false, false, false, null);rn channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");rn QueueingConsumer consumer = new QueueingConsumer(channel);rn boolean autoAck = false;rn channel.basicConsume(QUEUE_NAME, autoAck, consumer);rn while (true) rn QueueingConsumer.Delivery delivery = consumer.nextDelivery();rn String message = new String(delivery.getBody());rn channel.basicAck(delivery.getEnvelope().getDeliveryTag() , false); rn Thread.sleep(500);rn System.out.println(" received1 message[" + message + "]");rn rn rn 论坛

没有更多推荐了,返回首页